-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask
More file actions
executable file
·102 lines (82 loc) · 2.98 KB
/
ask
File metadata and controls
executable file
·102 lines (82 loc) · 2.98 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Option 2: Read the API Key from an environment variable (recommended)
API_KEY="${GEMINI_API_KEY}"
# The specific Gemini model to use
MODEL_NAME="gemini-2.0-flash" # Or "gemini-pro", "gemini-1.5-pro-latest", etc.
API_ENDPOINT="https://generativelanguage.googleapis.com/v1beta/models/${MODEL_NAME}:generateContent?key=${API_KEY}"
# --- Pre-flight Checks ---
# Check if API Key is set
if [[ -z "${API_KEY}" ]]; then
echo "Error: Gemini API Key is not set." >&2
echo "Please set the GEMINI_API_KEY environment variable:" >&2
echo " export GEMINI_API_KEY='YOUR_API_KEY'" >&2
# Uncomment the line below if you chose Option 1 above and want to check that
# echo "Or set the API_KEY variable directly in the script." >&2
exit 1
fi
# Check for curl
if ! command -v curl &> /dev/null; then
echo "Error: curl command not found. Please install curl." >&2
exit 1
fi
# Check for jq
if ! command -v jq &> /dev/null; then
echo "Error: jq command not found. Please install jq." >&2
exit 1
fi
# Check if a question was provided
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <your question>" >&2
exit 1
fi
# Combine all command-line arguments into a single question string
QUESTION="$*"
# --- Prepare API Request ---
# Construct the JSON payload safely using printf and jq
# This handles quotes and special characters within the question
JSON_PAYLOAD=$(printf '{
"contents": [{
"parts":[{
"text": %s
}]
}]
}' "$(printf "%s" "$QUESTION" | jq -sR .)")
# --- Make API Call and Process Response ---
# Send the request using curl, capture the response
# -s : Silent mode (don't show progress)
# -X POST : Use POST method
# -H : Add header (Content-Type)
# -d : Data payload
API_RESPONSE=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"$API_ENDPOINT")
# Check if curl encountered an error (e.g., network issue)
if [[ $? -ne 0 ]]; then
echo "Error: curl command failed. Check network connection or API endpoint." >&2
exit 1
fi
# --- Extract and Display Answer ---
# Check if the API returned an error object
if echo "$API_RESPONSE" | jq -e '.error' > /dev/null; then
echo "Error received from Gemini API:" >&2
# Print the error details formatted by jq
echo "$API_RESPONSE" | jq '.error' >&2
exit 1
fi
# Extract the answer text using jq
# -r : Raw output (removes quotes)
# -e : Set exit code if path doesn't exist (for error checking)
# Path: .candidates[0].content.parts[0].text
# // "..." : Provide a default value if the path is null or not found
ANSWER=$(echo "$API_RESPONSE" | jq -r -e '.candidates[0].content.parts[0].text // "Error: Could not parse answer from API response."')
# Check if jq failed to extract the text
if [[ $? -ne 0 || "$ANSWER" == "Error: Could not parse answer from API response." ]]; then
echo "$ANSWER" >&2 # Print the specific jq error message
echo "--- Full API Response for Debugging ---" >&2
echo "$API_RESPONSE" >&2
exit 1
fi
# Print the final answer
echo "$ANSWER"
exit 0