Skip to content

Instantly share code, notes, and snippets.

@eastlondoner
Last active January 10, 2025 07:59
Show Gist options
  • Save eastlondoner/ada4c429808a961077df42e859bac719 to your computer and use it in GitHub Desktop.
Save eastlondoner/ada4c429808a961077df42e859bac719 to your computer and use it in GitHub Desktop.
Ask Gemini
#!/usr/bin/env bash
# make bash play nice
set -euo pipefail
# Load environment variables
source .env
# Check required environment variables
check_env_var() {
if [ -z "${!1:-}" ]; then
echo "Error: $1 environment variable is not set"
echo "Please add $1=$2 to your .env file"
exit 1
fi
}
check_env_var "GEMINI_API_KEY" "your-api-key"
function g() {
# Run repomix and capture output
repomix_output="$(npx -y repomix@latest --ignore '**/*.pbxproj,**/node_modules/**,**/dist/**,**/build/**,**/compile/**,**/.*/**')"
local repo_context
repo_context="$(cat repomix-output.txt)"
local cursorrules
cursorrules="$(cat .cursorrules)"
local response
response=$(jq -n \
--arg content "$*" \
--arg context "$repo_context" \
--arg cursorrules "$cursorrules" \
'{
"contents": [
{
"parts": [
{
"text": ($cursorrules)
},
{
"text": ($context)
},
{
"text": ($content)
}
]
}
]
}' | curl --silent \
--request POST \
--url "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=${GEMINI_API_KEY}" \
--header 'Content-Type: application/json' \
--data @-)
if [[ -z "$response" ]]; then
echo "Error: Empty response from API"
exit 1
fi
if echo "$response" | jq -e 'has("error")' > /dev/null; then
echo "API Error: $(echo "$response" | jq -r '.error.message')"
exit 1
fi
echo "$response" | jq --raw-output '.candidates[0].content.parts[0].text'
}
g "$*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment