Advanced Thinking Methods

Mac Terminal: Tight AI Integration.

[mwai_chat context="Converse as if you were an AI assistant. Be friendly, creative. Speak in terms that direct the ai solutions proposed as a service to to advise on integration ai into their business, create bespoke solutions after a consultancy phase offered by the team at (the website this chat assistant is on). Never promise that can complete a project as they would need the consultancy process to happen first, you can only offer suggestions in context to the site. The users first input will be a synopsis of their business, and ask them for more context if they give a generic reply. The Advanced Thinking Method website content is here for context: {content}. You should always offer to elaborate more if they can give more context that you can identify" user_name="You: " start_sentence="Hi! Give me an outline of your business, and I'll show you how AI can help!" window="true" icon_text="AI Chat" fullscreen="true" content_aware="true"]

I love ChatGPT, but wish I could tell it do do more real things. The incredible GPT4ALL offers an API, but wanted to go deeper and bring ChatGPT into the terminal, in context. This simple script enables the execution right in the terminal. There are a few other short steps:

  1. save this file as ai (no extension) somewhere like /usr/local/bin
  2. Use this command in the folder where the file is: chmod +x ai
  3. If you are missing jq, use brew install jq (you might need to install brew also)

You can then either type ai into the terminal to start the session or ai and then the prompt (example: ai what command do i need to see the current directory). It will then show the command and if you want to execute it.


# API endpoint and your API key

# Get user input
if [ $# -gt 0 ]; then
    read -p "Prompt: " -r MESSAGE

# Remove non-command text and extract commands separated by &&
COMMANDS=$(echo "$MESSAGE" | grep -oE '(\w+\s*&&\s*)*\w+\s*' | sed 's/&&//g')

# Check if there are commands
if [ -z "$COMMANDS" ]; then
    echo "No valid commands found."
    exit 0

# Prepare the data for API request
DATA=$(jq -n \
  --arg prompt "You are a user asking for commands in your mac terminal. Please only list the commands separated by && and remove all other text that isn't a command: $COMMANDS" \
  '{model: "text-davinci-003", prompt: $prompt, max_tokens: 50}')

# Make the API request
RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ${API_KEY}" -d "$DATA" "$API_ENDPOINT")

echo "$RESPONSE"

# Extract the completion text from the response
COMPLETION=$(echo "$RESPONSE" | jq -r '.choices[0].text' | tr -d '\r\n')

# Remove any leading &&
COMPLETION=$(echo "$COMPLETION" | sed 's/^&&\s*//')

# Check if a completion is present
if [ -z "$COMPLETION" ] || [ "$COMPLETION" == "null" ]; then
    echo "No valid completion found."
    exit 0

# Display the completion result
echo "Completion Result:"

# Prompt for confirmation
read -p "Confirm execution? [y/n]: " CHOICE
CHOICE=$(echo "$CHOICE" | tr '[:upper:]' '[:lower:]')  # Convert choice to lowercase

# Execute the completion if the choice is affirmative
if [[ $CHOICE == "y" || $CHOICE == "yes" ]]; then
    eval "$COMPLETION"