-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.sh
executable file
·77 lines (61 loc) · 2.08 KB
/
chatbot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# Ensure that OpenAI API key is set
if [[ -z "$OPENAI_API_KEY" ]]; then
echo "Error: OpenAI API key not set. Please set it in the OPENAI_API_KEY environment variable."
exit 1
fi
# Function to escape JSON special characters
escape_json() {
echo "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\//\\\//g; s/\b/\\b/g; s/\f/\\f/g; s/\n/\\n/g; s/\r/\\r/g; s/\t/\\t/g'
}
# Function to get AI response from OpenAI API
get_ai_response() {
local prompt="$1"
# Escape the prompt for JSON
escaped_prompt=$(escape_json "$prompt")
# Create a valid JSON payload
json_payload=$(cat <<EOF
{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "$escaped_prompt"}],
"max_tokens": 50,
"n": 1,
"temperature": 0.7
}
EOF
)
# Send request to OpenAI's chat API using curl
response=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$json_payload")
# Debug: Print the full API response for troubleshooting
echo "Full API Response: $response"
# Check if there's an error in the API response
if echo "$response" | jq -e .error > /dev/null; then
echo "Error: $(echo "$response" | jq -r .error.message)"
return
fi
# Parse the AI response using jq and trim leading/trailing spaces
ai_response=$(echo "$response" | jq -r '.choices[0].message.content' | sed 's/^ *//; s/ *$//')
# If no response, provide a default message
if [[ -z "$ai_response" ]]; then
ai_response="Sorry, I didn't get that. Could you please ask again?"
fi
echo "$ai_response"
}
# Chat loop
echo "Welcome to the Chatbot! Type 'exit' to quit."
while true; do
# Get user input
read -p "You: " user_input
# Exit if the user types 'exit'
if [[ "$user_input" == "exit" ]]; then
echo "Goodbye!"
break
fi
# Get the AI's response from OpenAI API
ai_response=$(get_ai_response "$user_input")
# Display the AI response
echo "AI: $ai_response"
done