#!/bin/bash # Run as "GROQ_API_KEY=gsk_... ./groq.sh 'Your query'", or set GROQ_API_KEY in # .bashrc MAX_TURNS=20 HIST_FILE="groq_hist.json" HIST=$(cat "$HIST_FILE" 2>/dev/null) # 1. Encode arguments into a JSON string PAYLOAD=$(jq -n --arg prompt "$*" --argjson hist "${HIST:-[]}" '{ messages: ([{role: "system", content: "Output raw, plain text or markdown only."}] + $hist + [{ role: "user", content: $prompt }]), model: "openai/gpt-oss-20b", temperature: 1, max_completion_tokens: 2048, top_p: 1, stream: true, reasoning_effort: "medium", stop: null }') # 2. Post the query, and fetch the response TMP_RESP=$(mktemp) curl -s "https://api.groq.com/openai/v1/chat/completions" \ -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${GROQ_API_KEY}" \ -d "$PAYLOAD" | \ grep --line-buffered '^data: {' | \ sed -u 's/^data: //' | \ jq -j --unbuffered '.choices[0].delta.content // empty' | \ tee "$TMP_RESP" echo "" # Add final newline # 3. Save turn to history (last X turns = 2*X messages) if [ -s "$TMP_RESP" ]; then jq -n --argjson hist "${HIST:-[]}" --arg prompt "$*" --rawfile resp "$TMP_RESP" --argjson max "$((MAX_TURNS * 2))" \ '($hist + [{role: "user", content: $prompt}, {role: "assistant", content: $resp}]) | if length > $max then .[-$max:] else . end' > "$HIST_FILE" fi rm -f "$TMP_RESP"