forked from google-gemini/gemini-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_gemini_request.sh
More file actions
executable file
·105 lines (93 loc) · 3.4 KB
/
Copy pathsend_gemini_request.sh
File metadata and controls
executable file
·105 lines (93 loc) · 3.4 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
103
104
105
#!/bin/bash
# -----------------------------------------------------------------------------
# Gemini API Replay Script
# -----------------------------------------------------------------------------
# Purpose:
# This script is used to replay a Gemini API request using a raw JSON payload.
# It is particularly useful for debugging the exact requests made by the
# Gemini CLI.
#
# Prerequisites:
# 1. Export your Gemini API key:
# export GEMINI_API_KEY="your_api_key_here"
#
# 2. Generate a request payload from the Gemini CLI:
# Inside the CLI, run the `/chat debug` command. This will save the most
# recent API request to a file named `gcli-request-<timestamp>.json`.
#
# Usage:
# ./scripts/send_gemini_request.sh --payload <path_to_json> --model <model_id> [--stream]
#
# Options:
# --payload <file> Path to the JSON request payload.
# --model <id> The Gemini model ID (e.g., gemini-3-flash-preview).
# --stream (Optional) Use the streaming API endpoint. Defaults to non-streaming.
#
# Example:
# ./scripts/send_gemini_request.sh --payload gcli-request.json --model gemini-3-flash-preview
# -----------------------------------------------------------------------------
set -e -E
# Load environment variables from .env if it exists
if [[ -f ".env" ]]; then
echo "Loading environment variables from .env file..."
set -a # Automatically export all variables
# shellcheck source=/dev/null
source .env
set +a
fi
# Function to print usage
usage() {
echo "Usage: $0 --payload <path_to_json_file> --model <model_id> [--stream]"
echo "Ensure GEMINI_API_KEY environment variable is set."
exit 1
}
STREAM_MODE=false
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--payload) PAYLOAD_FILE="${2}"; shift ;;
--model) MODEL_ID="${2}"; shift ;;
--stream) STREAM_MODE=true ;;
*) echo "Unknown parameter passed: ${1}"; usage ;;
esac
shift
done
# Validate inputs
if [[ -z "${PAYLOAD_FILE}" ]] || [[ -z "${MODEL_ID}" ]]; then
echo "Error: Missing required arguments."
usage
fi
if [[ -z "${GEMINI_API_KEY}" ]]; then
echo "Error: GEMINI_API_KEY environment variable is not set."
exit 1
fi
if [[ ! -f "${PAYLOAD_FILE}" ]]; then
echo "Error: Payload file '${PAYLOAD_FILE}' does not exist."
exit 1
fi
# API Endpoint definition
if [[ "${STREAM_MODE}" = true ]]; then
GENERATE_CONTENT_API="streamGenerateContent"
echo "Mode: Streaming"
else
GENERATE_CONTENT_API="generateContent"
echo "Mode: Non-streaming (Default)"
fi
echo "Sending request to model: ${MODEL_ID}"
echo "Using payload from: ${PAYLOAD_FILE}"
echo "----------------------------------------"
# Make the cURL request. If non-streaming, pipe through jq for readability if available.
if [[ "${STREAM_MODE}" = false ]] && command -v jq &> /dev/null; then
# Invoke curl separately to avoid masking its return value
output=$(curl -s -X POST \
-H "Content-Type: application/json" \
"https://generativelanguage.googleapis.com/v1beta/models/${MODEL_ID}:${GENERATE_CONTENT_API}?key=${GEMINI_API_KEY}" \
-d "@${PAYLOAD_FILE}")
echo "${output}" | jq .
else
curl -X POST \
-H "Content-Type: application/json" \
"https://generativelanguage.googleapis.com/v1beta/models/${MODEL_ID}:${GENERATE_CONTENT_API}?key=${GEMINI_API_KEY}" \
-d "@${PAYLOAD_FILE}"
fi
echo -e "\n----------------------------------------"