-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpublic_api_shell_test.sh
More file actions
executable file
·102 lines (89 loc) · 2.97 KB
/
Copy pathpublic_api_shell_test.sh
File metadata and controls
executable file
·102 lines (89 loc) · 2.97 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
#!/usr/bin/env bash
set -euo pipefail
# ================================
# Public Evaluation API quickstart
# - Loads .env safely (zsh/bash)
# - Submits an inline JSON blueprint
# - Supports async (default) and inline (no-persist) modes
# - Async: polls status until complete, then fetches compact result
# - Inline: returns monolithic result immediately in POST response
# ================================
# 1) Load .env (zsh-safe): export only KEY=VALUE lines, ignore comments/blank
if [ -f .env ]; then
while IFS='=' read -r key value; do
[[ "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
export "$key=$value"
done < <(grep -E '^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*=' .env | sed -E 's/^[[:space:]]*//')
fi
# 2) Require env vars
: "${BASE_URL:?set BASE_URL in .env (e.g., http://localhost:3172)}"
: "${PUBLIC_API_KEY:?set PUBLIC_API_KEY in .env}"
# 3) Minimal inline JSON blueprint
BLUEPRINT='{"title":"Quick API Test","models":["openai:gpt-4o-mini"],"prompts":[{"id":"hello","prompt":"Say hello in one short sentence."}]}'
# 4) Choose response mode (env var): RESPONSE_MODE=inline for no-persist
MODE_HEADER=()
if [ "${RESPONSE_MODE:-}" = "inline" ]; then
MODE_HEADER+=( -H "X-Response-Mode: inline" )
echo "Using inline response mode (no persistence)."
fi
# 5) Submit run
RESP=$(curl -s -X POST \
-H "Authorization: Bearer $PUBLIC_API_KEY" \
-H "Content-Type: application/json" \
"${MODE_HEADER[@]}" \
--data "$BLUEPRINT" \
"$BASE_URL/api/v1/evaluations/run")
# Inline mode: print monolithic result and exit
if [ "${RESPONSE_MODE:-}" = "inline" ]; then
echo "$RESP" > /tmp/_inline_result.json
echo "Inline result saved to /tmp/_inline_result.json"
cat /tmp/_inline_result.json
exit 0
fi
# 6) Extract runId (no jq); bail if missing
RUN_ID=$(printf '%s' "$RESP" | sed -n 's/.*"runId":"\([^"]*\)".*/\1/p')
if [ -z "$RUN_ID" ]; then
echo "Failed to start run. Server response:"
echo "$RESP"
exit 1
fi
echo "runId=$RUN_ID"
# Extract and show the view URL
VIEW_URL=$(printf '%s' "$RESP" | sed -n 's#.*"viewUrl":"\([^"]*\)".*#\1#p')
if [ -n "$VIEW_URL" ]; then
echo "View run progress at: $VIEW_URL"
fi
# 7) Poll status until completed/failed
while true; do
STATUS_JSON=$(curl -s "$BASE_URL/api/v1/evaluations/status/$RUN_ID")
STATUS=$(printf '%s' "$STATUS_JSON" | sed -n 's/.*"status":"\([^"]*\)".*/\1/p')
echo "status=$STATUS"
[ "$STATUS" = "completed" ] && break
[ "$STATUS" = "failed" ] && { echo "run failed"; exit 1; }
sleep 2
done
# 8) Fetch result (compact payload) with small buffer and retries
sleep 2
TRIES=10
while : ; do
HTTP=$(curl -s -o /tmp/_res.json -w "%{http_code}" "$BASE_URL/api/v1/evaluations/result/$RUN_ID")
if [ "$HTTP" = "200" ]; then
cat /tmp/_res.json
echo
break
fi
if [ "$HTTP" = "404" ]; then
echo "Result not found (404)."
cat /tmp/_res.json
echo
break
fi
TRIES=$((TRIES-1))
if [ $TRIES -le 0 ]; then
echo "Result not ready after retries."
cat /tmp/_res.json
echo
break
fi
sleep 1
done