-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAGI.py
173 lines (135 loc) · 5.06 KB
/
AGI.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# coding: utf-8
#
import openai
import urllib.request
import requests
import urllib.parse
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
openai.api_key = config['DEFAULT']['open_ai']
prompt = ['''Question: Who lived longer, Muhammad Ali or Alan Turing?
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
Question: When was the founder of craigslist born?
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
Question: Who was the maternal grandfather of George Washington?
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
Question: Are both the directors of Jaws and Casino Royale from the same country?
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No
Question: ''',
'''
Are follow up questions needed here:''', ]
def promptf(question, prompt, intermediate="\nIntermediate answer:", followup="Follow up:", finalans='\nSo the final answer is:'):
cur_prompt = prompt[0] + question + prompt[1]
ret_text = call_gpt(cur_prompt, intermediate)
while followup in get_last_line(ret_text):
cur_prompt += ret_text
question = extract_question(ret_text)
external_answer = get_answer(question)
if external_answer is not None:
cur_prompt += intermediate + ' ' + external_answer + '.'
ret_text = call_gpt(cur_prompt, intermediate)
else:
cur_prompt += intermediate
gpt_answer = call_gpt(cur_prompt, ['\n'+followup, finalans])
cur_prompt += gpt_answer
if finalans not in ret_text:
cur_prompt += finalans
ret_text = call_gpt(cur_prompt, '\n')
return cur_prompt + ret_text
def google(question):
params = {
"api_key": config['DEFAULT']['google_search'],
"q": question,
"google_domain": "https://www.googleapis.com/customsearch/v1",
"cx": config['DEFAULT']['google_cx']
}
data = requests.get(params["google_domain"]+"?key="+params["api_key"] +
"&cx="+params["cx"]+"&q="+urllib.parse.quote_plus(params["q"]))
res = ''
if ("items" in data.json()):
for value in data.json()["items"]:
if value["snippet"]:
res += value["snippet"]
toret = res
return toret
def get_answer(question):
return google(question)
def call_gpt(cur_prompt, stop):
ans = openai.Completion.create(
model="text-davinci-003",
max_tokens=512,
stop=stop,
prompt=cur_prompt,
temperature=1)
returned = ans['choices'][0]['text']
return returned
def extract_answer(generated):
if '\n' not in generated:
last_line = generated
else:
last_line = generated.split('\n')[-1]
if ':' not in last_line:
after_colon = last_line
else:
after_colon = generated.split(':')[-1]
if ' ' == after_colon[0]:
after_colon = after_colon[1:]
if '.' == after_colon[-1]:
after_colon = after_colon[:-1]
return after_colon
def extract_question(generated):
if '\n' not in generated:
last_line = generated
else:
last_line = generated.split('\n')[-1]
if 'Follow up:' not in last_line:
print('we probably should never get here...' + generated)
if ':' not in last_line:
after_colon = last_line
else:
after_colon = generated.split(':')[-1]
if ' ' == after_colon[0]:
after_colon = after_colon[1:]
if '?' != after_colon[-1]:
print('we probably should never get here...' + generated)
return after_colon
def get_last_line(generated):
if '\n' not in generated:
last_line = generated
else:
last_line = generated.split('\n')[-1]
return last_line
def gbg(input):
return "\33[42m" + input + "\x1b[0m"
def pbg(input):
return "\33[35m" + input + "\x1b[0m"
def main(question):
ret = promptf(question, prompt)
clean_ans = extract_answer(ret)
return clean_ans