forked from daveshap/MultiDocumentAnswering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer_questions.py
93 lines (79 loc) · 3.12 KB
/
answer_questions.py
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
import openai
import json
import numpy as np
import textwrap
import re
from time import time,sleep
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return infile.read()
openai.api_key = open_file('openaiapikey.txt')
def gpt3_embedding(content, engine='text-similarity-ada-001'):
content = content.encode(encoding='ASCII',errors='ignore').decode()
response = openai.Embedding.create(input=content,engine=engine)
vector = response['data'][0]['embedding'] # this is a normal list
return vector
def similarity(v1, v2): # return dot product of two vectors
return np.dot(v1, v2)
def search_index(text, data, count=20):
vector = gpt3_embedding(text)
scores = list()
for i in data:
score = similarity(vector, i['vector'])
#print(score)
scores.append({'content': i['content'], 'score': score})
ordered = sorted(scores, key=lambda d: d['score'], reverse=True)
return ordered[0:count]
def gpt3_completion(prompt, engine='text-davinci-002', temp=0.6, top_p=1.0, tokens=2000, freq_pen=0.25, pres_pen=0.0, stop=['<<END>>']):
max_retry = 5
retry = 0
prompt = prompt.encode(encoding='ASCII',errors='ignore').decode()
while True:
try:
response = openai.Completion.create(
engine=engine,
prompt=prompt,
temperature=temp,
max_tokens=tokens,
top_p=top_p,
frequency_penalty=freq_pen,
presence_penalty=pres_pen,
stop=stop)
text = response['choices'][0]['text'].strip()
text = re.sub('\s+', ' ', text)
filename = '%s_gpt3.txt' % time()
with open('gpt3_logs/%s' % filename, 'w') as outfile:
outfile.write('PROMPT:\n\n' + prompt + '\n\n==========\n\nRESPONSE:\n\n' + text)
return text
except Exception as oops:
retry += 1
if retry >= max_retry:
return "GPT3 error: %s" % oops
print('Error communicating with OpenAI:', oops)
sleep(1)
if __name__ == '__main__':
with open('index.json', 'r') as infile:
data = json.load(infile)
#print(data)
while True:
query = input("Enter your question here: ")
#print(query)
results = search_index(query, data)
#print(results)
#exit(0)
answers = list()
# answer the same question for all returned chunks
for result in results:
prompt = open_file('prompt_answer.txt').replace('<<PASSAGE>>', result['content']).replace('<<QUERY>>', query)
answer = gpt3_completion(prompt)
print('\n\n', answer)
answers.append(answer)
# summarize the answers together
all_answers = '\n\n'.join(answers)
chunks = textwrap.wrap(all_answers, 10000)
final = list()
for chunk in chunks:
prompt = open_file('prompt_summary.txt').replace('<<SUMMARY>>', chunk)
summary = gpt3_completion(prompt)
final.append(summary)
print('\n\n=========\n\n', '\n\n'.join(final))