Route Agent D.R.E chatbot through admin-ai (dedicated dre-chatbot virtual key, $5/day budget) instead of native DeepSeek key
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# Idempotent: ensure a single dre-chatbot virtual key exists on admin-ai.
|
||||
# LiteLLM facts (learned 2026-08-26):
|
||||
# - /key/list -> {"keys": [hash...], total_count, total_pages} (paginated)
|
||||
# - /key/info?key=<hash> -> {"key": "<hash>", "info": {key_alias, models, ...}}
|
||||
# - /key/generate -> {"key": "sk-...", "token": "<hash>", "token_id": "<hash>"}
|
||||
# - raw "sk-" key is ONLY visible in the /key/generate response; never stored
|
||||
# in plaintext afterward (key_name is masked). So we must regenerate to get it.
|
||||
set -euo pipefail
|
||||
|
||||
APP1_KEY=/root/.ssh/itpp-infra
|
||||
APP1_HOST=root@152.53.36.131
|
||||
ENV_FILE=/opt/dre-portal/.env
|
||||
ALIAS=dre-chatbot
|
||||
MODEL=deepseek-v4-flash
|
||||
BUDGET=5.0
|
||||
BASE="https://admin-ai.itpropartner.com"
|
||||
|
||||
MK=$(ssh -i "$APP1_KEY" "$APP1_HOST" "grep LITELLM_MASTER_KEY /root/docker/litellm/.env | cut -d= -f2" 2>/dev/null)
|
||||
[ -n "$MK" ] || { echo "ERROR: no master key"; exit 1; }
|
||||
|
||||
# --- 1. Delete any existing keys carrying the dre-chatbot alias (junk from bad runs)
|
||||
curl -s -H "Authorization: Bearer $MK" "$BASE/key/list" > /tmp/dre_keylist.json
|
||||
HASHES=$(python3 -c "import json; print('\n'.join(json.load(open('/tmp/dre_keylist.json')).get('keys', [])))")
|
||||
DEL=""
|
||||
for h in $HASHES; do
|
||||
[ -z "$h" ] && continue
|
||||
alias=$(curl -s -H "Authorization: Bearer $MK" "$BASE/key/info?key=$h" \
|
||||
| python3 -c "import sys,json; print((json.load(sys.stdin).get('info') or {}).get('key_alias',''))" 2>/dev/null || echo "")
|
||||
if [ "$alias" = "$ALIAS" ] || [ "$alias" = "dre-chatbot-inspect" ]; then
|
||||
DEL="$DEL $h"
|
||||
fi
|
||||
done
|
||||
if [ -n "$DEL" ]; then
|
||||
curl -s -X POST "$BASE/key/delete" -H "Authorization: Bearer $MK" -H "Content-Type: application/json" \
|
||||
-d "$(python3 -c "import json,sys; print(json.dumps({'keys': sys.argv[1].split()}))" "$DEL")" >/dev/null
|
||||
echo "deleted stale keys:$DEL"
|
||||
fi
|
||||
|
||||
# --- 2. Generate a fresh key, capture the RAW sk- token from the 'key' field
|
||||
RESP=$(curl -s -X POST "$BASE/key/generate" -H "Authorization: Bearer $MK" -H "Content-Type: application/json" \
|
||||
-d "{\"key_alias\":\"$ALIAS\",\"models\":[\"$MODEL\"],\"max_budget\":$BUDGET,\"budget_duration\":\"daily\",\"metadata\":{\"purpose\":\"Agent DRE public FAQ chatbot\",\"service\":\"dre-chatbot\"}}")
|
||||
KEY=$(echo "$RESP" | python3 -c "import sys,json; print(json.load(sys.stdin).get('key',''))")
|
||||
case "$KEY" in
|
||||
sk-*) ;;
|
||||
*) echo "ERROR: generated key not sk- (got '${KEY:0:8}')"; echo "$RESP" | head -c 500; exit 1;;
|
||||
esac
|
||||
|
||||
# --- 3. Write to .env (never print the key)
|
||||
if grep -q '^DRE_CHATBOT_API_KEY=' "$ENV_FILE"; then
|
||||
sed -i "s|^DRE_CHATBOT_API_KEY=.*|DRE_CHATBOT_API_KEY=$KEY|" "$ENV_FILE"
|
||||
else
|
||||
echo "DRE_CHATBOT_API_KEY=$KEY" >> "$ENV_FILE"
|
||||
fi
|
||||
grep -q '^DRE_CHATBOT_BASE_URL=' "$ENV_FILE" || echo "DRE_CHATBOT_BASE_URL=https://admin-ai.itpropartner.com/v1" >> "$ENV_FILE"
|
||||
grep -q '^DRE_CHATBOT_MODEL=' "$ENV_FILE" || echo "DRE_CHATBOT_MODEL=$MODEL" >> "$ENV_FILE"
|
||||
|
||||
echo "DONE: alias=$ALIAS model=$MODEL budget=\$$BUDGET/day keylen=${#KEY}"
|
||||
Reference in New Issue
Block a user