Repository navigation
Expand file tree
/
Copy pathai_ask_doc.sql
More file actions
61 lines (52 loc) · 2.06 KB
/
Copy pathai_ask_doc.sql
File metadata and controls
61 lines (52 loc) · 2.06 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
-- Search for the text closest to the question as a prompt from the vector results
-- stored in doc_table, and obtain an answer through openai.
create function ai_ask_doc(doc_table varchar, question varchar, max_token integer = 1024, mode varchar = 'v2')
returns varchar
as
$$
import openai
top_n = 3
documentation_length = 7800
# Get question text embedding vector
qst_emb_plan = plpy.prepare('select ai_embedding_text($1)', ['varchar'])
qst_vector = plpy.execute(qst_emb_plan, [question], 1)[0]['ai_embedding_text']
# Get the section record with the closest cosine distance
nearest_dist_plan = plpy.prepare(f'select * from {doc_table} order by embedding <=> $1 limit {top_n}', ['vector'])
nearest_sections = plpy.execute(nearest_dist_plan, [qst_vector])
# Merge background documents
documentation = ''
for section in nearest_sections:
documentation += section['content'] + '\n'
documentation = documentation[:documentation_length]
# v1: Answer the question with the Completion model.
def bot_v1():
prompt = f'Documentation:\n{documentation}\nQuestion: {question}\nAnswer:'
rsp = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=max_token
)
answer = rsp['choices'][0]['text']
return answer
# v2: Answer the question with the ChatCompletion model.
def bot_v2():
prompt = f'You are an assistant with the following background knowledge::\n{documentation}'
rsp = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=0.7,
max_tokens=1024,
messages=[
{'role': 'system', 'content': prompt},
{'role': 'user', 'content': question}
]
)
answer = rsp['choices'][0]['message']['content']
return answer
if mode == 'v1':
return bot_v1()
elif mode == 'v2':
return bot_v2()
else:
return bot_v2()
$$ language plpython3u;