-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini.sh
74 lines (60 loc) · 1.99 KB
/
gemini.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
#!/bin/bash
# Load the configuration file
source /etc/AIkonsole.conf
# Check if API_KEY is set
if [ -z "$API_KEY" ]; then
echo "API_KEY is not set. Please set the environment variable API_KEY."
exit 1
fi
# Prompt
read -p "Enter your prompt: " PROMPT
# Making a request to the API
for ((i=1; i<=$RETRY_COUNT; i++)); do
RESPONSE=$(curl -s -H 'Content-Type: application/json' \
-m "$API_TIMEOUT" \
-d "{\"contents\":[{\"parts\":[{\"text\":\"$PROMPT\"}]}]}" \
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=$API_KEY")
# Capture curl's exit status
CURL_STATUS=$?
if [ $CURL_STATUS -eq 0 ]; then
# If successful, extract and display the answer
ANSWER=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text')
break
elif [ $CURL_STATUS -eq 28 ]; then
# If curl timed out
echo "Error: API request timed out after $API_TIMEOUT seconds."
else
# Handle other errors (like connection issues)
echo "Error: API request failed (curl status: $CURL_STATUS)."
fi
# If it's not the last attempt, wait before retrying
if [ $i -lt $RETRY_COUNT ]; then
echo "Retrying in $RETRY_DELAY seconds..."
sleep "$RETRY_DELAY"
fi
done
# If all retries fail, notify the user
if [ $i -eq $RETRY_COUNT ] && [ $CURL_STATUS -ne 0 ]; then
echo "Error: All attempts to connect to the API failed."
fi
# Logging
if [ "$LOGGING" = "yes" ]; then
# Extract the directory from the LOG_PATH
LOG_DIR=$(dirname "$LOG_PATH")
# Check if the directory exists, if not, create it
if [ ! -d "$LOG_DIR" ]; then
mkdir -p "$LOG_DIR"
fi
# Append the log with the current prompt and response
echo "[$(date)] Prompt: $PROMPT | Response: $ANSWER" >> "$LOG_PATH"
fi
### output formatt detailed or plain
if [ "$OUTPUT_FORMAT" = "detailed" ]; then
echo "Full Response: $RESPONSE"
else
echo "Response: $ANSWER"
fi
###Language
if [ "$DEFAULT_LANGUAGE" != "" ]; then
PROMPT="$PROMPT (Language: $DEFAULT_LANGUAGE)"
fi