-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-answer-sse.sh
More file actions
executable file
·81 lines (71 loc) · 2.46 KB
/
generate-answer-sse.sh
File metadata and controls
executable file
·81 lines (71 loc) · 2.46 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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# Generate a grounded AI answer and stream the response as Server-Sent Events
# via POST /ai/answer/sse. Each event is printed as it arrives.
#
# Usage:
# ./generate-answer-sse.sh
#
# Prerequisites:
# - curl and jq must be installed
# - The user must have read access to at least some knowledge content
# ---------------------------------------------------------------------------
set -euo pipefail
# ---------------------------------------------------------------------------
# Configuration -- replace placeholders with values for your environment.
# ---------------------------------------------------------------------------
BASE_URL="https://<your-instance>/sabio-web/services"
USERNAME="<username>"
PASSWORD="<password>"
CLIENT_NAME="acme-portal"
CLIENT_VERSION="1.0.0"
QUERY="How do I reset my password?"
# ---------------------------------------------------------------------------
# Step 1: Authenticate
# ---------------------------------------------------------------------------
LOGIN_RESPONSE=$(curl -s -X POST \
"${BASE_URL}/authentication/credentials" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{
\"login\": \"${USERNAME}\",
\"key\": \"${PASSWORD}\"
}")
TOKEN=$(echo "${LOGIN_RESPONSE}" | jq -r '.data.key')
if [ -z "${TOKEN}" ] || [ "${TOKEN}" = "null" ]; then
echo "ERROR: Authentication failed." >&2
exit 1
fi
SABIO_CLIENT=$(jq -nc \
--arg name "${CLIENT_NAME}" \
--arg version "${CLIENT_VERSION}" \
'{name: $name, version: $version}')
# ---------------------------------------------------------------------------
# Step 2: Open the SSE stream and parse events as they arrive
#
# SSE format:
# event: <name>
# data: <json>
# <blank line marks end of event>
# ---------------------------------------------------------------------------
echo "Asking: ${QUERY}"
echo
curl -sN -X POST \
"${BASE_URL}/ai/answer/sse" \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-H "sabio-auth-token: ${TOKEN}" \
-H "sabio-client: ${SABIO_CLIENT}" \
-d "$(jq -nc --arg query "${QUERY}" '{query: $query}')" \
| awk '
BEGIN { event=""; data="" }
/^event:/ { event=substr($0, 8); next }
/^data:/ { data=data substr($0, 7); next }
/^[[:space:]]*$/ {
if (event != "") {
print "--- " event " ---"
print data
event=""; data=""
}
next
}
'