Skip to content

Instantly share code, notes, and snippets.

@scastillo
Created May 20, 2023 17:14
Show Gist options
  • Save scastillo/861ae96fd2ad15edd5c2cdb01f433528 to your computer and use it in GitHub Desktop.
Save scastillo/861ae96fd2ad15edd5c2cdb01f433528 to your computer and use it in GitHub Desktop.
Bash llm counseling
#!/bin/bash
# Define the command_not_found_handle function
command_not_found_handle() {
local command="$1"
echo "Command not found: $command"
# You can perform custom actions or error handling here
# Check if the command ends with '?'
if [[ $command == *'?' ]]; then
echo "Detected question command: $command"
# Call a separate script for question commands and capture the outputs
read -r man_pages output_h output_help question_command user_question < <(/path/to/your/question_script.sh "$command" "$2")
# Call a function to interact with OpenAI GPT API using the captured information
interact_with_openai_api "$man_pages" "$output_h" "$output_help" "$question_command" "$user_question"
else
echo "Regular command: $command"
# Call a separate script for regular commands
/path/to/your/regular_script.sh "$command"
fi
}
# Set the command_not_found_handle function as the command_not_found handler
echo 'command_not_found_handle() {' >> ~/.bash_profile
echo ' command_not_found_handle "$1"' >> ~/.bash_profile
echo '}' >> ~/.bash_profile
echo "Installation complete. The command_not_found_handle hook is set."
# Function to interact with OpenAI GPT API
interact_with_openai_api() {
local man_pages="$1"
local output_h="$2"
local output_help="$3"
local question_command="$4"
local user_question="$5"
# Set up the OpenAI GPT API parameters
local api_key="YOUR_OPENAI_API_KEY"
local api_endpoint="https://api.openai.com/v1/engines/davinci-codex/completions"
# Compose the context and user question
local context="$man_pages $output_h $output_help"
local prompt="$question_command\n\nQ: $user_question\nA:"
# Make the API request
local response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d '{
"prompt": "'"$prompt"'",
"max_tokens": 100
}' \
"$api_endpoint")
# Parse the API response
local completion=$(echo "$response" | jq -r '.choices[0].text')
# Process and display the completion
echo "OpenAI GPT API completion:"
echo "$completion"
}
#!/bin/bash
question_command="$1"
user_question="$2"
echo "Executing question command: $question_command"
# Store man pages of the command if available
man_pages=$(man "$question_command" 2>/dev/null)
echo "Man pages for $question_command:"
echo "$man_pages"
# Try the command with '-h'
output_h=$("$question_command" -h 2>&1)
echo "Output of '$question_command -h':"
echo "$output_h"
# Try the command with '--help'
output_help=$("$question_command" --help 2>&1)
echo "Output of '$question_command --help':"
echo "$output_help"
# Pass the captured information to the main script
echo "$man_pages"
echo "$output_h"
echo "$output_help"
echo "$question_command"
echo "$user_question"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment