-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_prompt.sh
executable file
·64 lines (57 loc) · 1.34 KB
/
query_prompt.sh
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
#!/bin/bash
print_ticket=false
consider_full_ticket=false
nb_tickets=5
while [[ $# -gt 0 ]]; do
case $1 in
-n|--limit)
nb_tickets="$2"
shift
shift
;;
-p|--print-ticket)
print_ticket=true
shift
;;
-f|--consider-full-ticket)
consider_full_ticket=true
shift
;;
-*|--*)
echo "Unknown option $1" >&2
exit 1
;;
*)
if [ ! -z "$prompt" ] ; then
echo "Only one positional argument expected" >&2
exit 1
fi
prompt="$1"
shift
;;
esac
done
if [ -z "$prompt" ] ; then
echo "One positional argument expected" >&2
exit 1
fi
readonly prompt nb_tickets print_ticket consider_full_ticket
readonly db="otrs"
readonly role="postgres"
readonly psql="psql -h localhost -U "$role" -d "$db" --tuples-only --no-align"
${psql[@]} --set prompt="$prompt" <<EOF
SET search_path=aide,public;
SELECT openai.vector(:'prompt')::vector as prompt_embedding \gset
SELECT t.id, t.tn $("$print_ticket" && echo ", tc.conversation")
FROM ticket t
JOIN ticket_embeddings te ON te.ticket_id = t.id
JOIN ticket_conversations tc ON tc.id = te.ticket_id
$(
if "$consider_full_ticket" ; then
echo "ORDER BY te.conversation_embedding <-> :'prompt_embedding'"
else
echo "ORDER BY te.first_article_embedding <-> :'prompt_embedding'"
fi
)
LIMIT $nb_tickets;
EOF