Skip to content

Instantly share code, notes, and snippets.

@tom-flamelit
Last active February 11, 2025 18:26
Show Gist options
  • Save tom-flamelit/86cb8a07ef649babc7417178694a8ea1 to your computer and use it in GitHub Desktop.
Save tom-flamelit/86cb8a07ef649babc7417178694a8ea1 to your computer and use it in GitHub Desktop.
ZSH Gemini call
function call_gemini() {
if [[ $# -lt 2 ]]; then
echo "Usage: call_gemini <api_key_file> \"Your message here\""
return 1
fi
API_KEY_FILE="$1"
MESSAGE="$2"
if [[ ! -s "$API_KEY_FILE" ]]; then
echo "Error: API key file '$API_KEY_FILE' is missing or empty."
return 1
fi
API_KEY=$(<"$API_KEY_FILE")
API_URL="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=$API_KEY"
JSON_PAYLOAD=$(jq -n --arg msg "$MESSAGE" '{
contents: [{ role: "user", parts: [{ text: $msg | gsub("\""; "\\\"") }]}]
}')
RESPONSE=$(curl -s -X POST "$API_URL" -H "Content-Type: application/json" -d "$JSON_PAYLOAD")
echo -e "\n$RESPONSE"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment