-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-answer.sh
More file actions
executable file
·82 lines (70 loc) · 2.79 KB
/
generate-answer.sh
File metadata and controls
executable file
·82 lines (70 loc) · 2.79 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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# Generate a grounded AI answer to a natural-language query via the blocking
# POST /ai/answer endpoint.
#
# Usage:
# ./generate-answer.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>"
# Stable identifier for your integration -- appears as the channel name in
# usage reports. Pick something unique to your integration.
CLIENT_NAME="acme-portal"
CLIENT_VERSION="1.0.0"
QUERY="How do I reset my password?"
# ---------------------------------------------------------------------------
# Step 1: Authenticate
# ---------------------------------------------------------------------------
echo "Authenticating as '${USERNAME}'..."
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."
echo "${LOGIN_RESPONSE}" | jq .
exit 1
fi
# ---------------------------------------------------------------------------
# Step 2: Build the sabio-client header value
# ---------------------------------------------------------------------------
SABIO_CLIENT=$(jq -nc \
--arg name "${CLIENT_NAME}" \
--arg version "${CLIENT_VERSION}" \
'{name: $name, version: $version}')
# ---------------------------------------------------------------------------
# Step 3: Request the answer
# ---------------------------------------------------------------------------
echo "Asking: ${QUERY}"
echo
RESPONSE=$(curl -s -X POST \
"${BASE_URL}/ai/answer" \
-H "Content-Type: application/json" \
-H "sabio-auth-token: ${TOKEN}" \
-H "sabio-client: ${SABIO_CLIENT}" \
-d "$(jq -nc --arg query "${QUERY}" '{query: $query}')")
# ---------------------------------------------------------------------------
# Step 4: Render
# ---------------------------------------------------------------------------
echo "--- Answer ---"
echo "${RESPONSE}" | jq -r '.data.result.answer'
echo
echo "--- Sources ---"
echo "${RESPONSE}" | jq -r '.data.result.sources[] | " \(.id) \(.resource) \(.title)"'
echo
echo "--- success ---"
echo "${RESPONSE}" | jq '.data.result.success'