-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
98 lines (74 loc) · 3.51 KB
/
ai.py
File metadata and controls
98 lines (74 loc) · 3.51 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# NAME: ai.py
# PURPOSE: the brain of the trivia bot!
# AUTHORS: Reid Baris and Emma Bethel
from openai import OpenAI
import requests
from bs4 import BeautifulSoup
import os
class BotsonBrain:
def respond(self, question):
raise NotImplementedError("Trivia bots need a response function!")
# created by Reid Baris, w/ minor refactoring from Emma Bethel
class WebScraperBotson(BotsonBrain):
def respond(self, question):
words = question.split(' ')
question_reformatted = ""
for word in words:
question_reformatted += word + "+"
question_reformatted = question[:-1]
# Get all of the HTML code from the website
URL = "https://www.google.com/search?q=" + question_reformatted + "&rlz=1C1CHBF_enUS892US892&oq=what+day+is+it&aqs=chrome.0" \
".0i512l4j0i131i433j0i512l5.7446j0j1&sourceid=chrome&ie=UTF-8"
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html5lib')
answer_keys_1 = ["<div class=\"BNeawe iBp4i AP7Wnd\"><div><div class=\"BNeawe iBp4i AP7Wnd\">",
"<div class=\"BNeawe s3v9rd AP7Wnd\"><span class=\"atOwb UMOHqf\">",
]
answer_keys_2 = ["</div>", "</span", "</div>"]
wiki_key_1 = "<h3 class=\"zBAuLc l97dzf\"><div class=\"BNeawe vvjwJb AP7Wnd\">"
wiki_key_2 = "</div>"
found = False
if question.lower() != 'q' and question.lower() != "quit":
# Try a range of techniques
for i in range(0, len(answer_keys_1)):
data = str(soup)
key_1 = answer_keys_1[i]
key_2 = answer_keys_2[i]
data = data[data.find(key_1) + len(key_1):]
answer = data[:data.find(key_2)].replace("&", "&")
# If the answer seems reasonable
if len(answer) < 1000:
found = True
#label2 = Label(root, text=answer).pack()
#canvas1.create_window(200, 180, window=label2)
return answer
if not found:
# Make a second guess in case no results were found
data = str(soup)
shortest_answer = ""
while data.find(wiki_key_1) != -1:
data = data[data.find(wiki_key_1) + len(wiki_key_1):]
answer = data[:data.find(wiki_key_2)].replace("&", "&").replace(" - Wikipedia", "")
if shortest_answer == "" or len(answer) < len(shortest_answer):
shortest_answer = answer
found = True
if found:
#print(shortest_answer)
#label2 = Label(root, text=shortest_answer).pack()
#canvas1.create_window(200, 180, window=label2)
return answer
else:
return "I couldn't find an answer :("
# created by Emma Bethel
class GptBotson(BotsonBrain):
def __init__(self):
api_key = os.environ.get('OPENAI_API_KEY')
if api_key is None:
raise Exception("No OpenAI API key set!")
self.client = OpenAI(api_key=api_key)
def respond(self, question):
response = self.client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": f"In 3 words or less, {question}"}]
)
return response.choices[0].message.content