Last active
February 11, 2025 18:26
-
-
Save tom-flamelit/86cb8a07ef649babc7417178694a8ea1 to your computer and use it in GitHub Desktop.
ZSH Gemini call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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