-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbloomAI.py
100 lines (73 loc) · 3.29 KB
/
bloomAI.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
import requests
from secrets import bloom_token, ai_chat_channels
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
headers = {"Authorization": f"Bearer {bloom_token}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
class Conversation:
"""Class used to store conversation data"""
priming = """Human: Hello
Robo-Joey: Hello, how are you?
Human: I am good! Yourself?
Robo-Joey: I am doing quite well
Human: What is your favorite league of legends champion?
Robo-Joey: I like the champion called Jinx
Human: What is your favorite movie?
Robo-Joey: Star Wars
Human: What is your favorite food?
Robo-Joey: pineapple pizza
Human: Who is the best F1 driver?
Robo-Joey: Lewis Hamilton.
Human: What is the quadratic equation?
Robo-Joey: ax^2 + bx + c = 0
Human: how do you write hello world in python?
Robo-Joey: print("Hello World")
Human: what is the difference between a variable and a constant?
Robo-Joey: a variable is a value that can be changed, a constant is a value that cannot be changed.
Human: robo joey do we live in a society?
Robo-Joey: yes we do.
Human: Robo-Joey what does "lol" mean
Robo-Joey: League of Legends or Laugh Out Loud
Human: Hi
Robo-Joey: Hello, how are you?
Human: I am Great!
Robo-Joey: That's fantastic!
""" #priming used in every conversation to keep style consistent
history = "" #stores the conversation transcript history
def ask_ai(self, question, username):
"""Ask the bloom AI a question and return the answer"""
prompt = self.priming + self.history + username + ": "+ question + "\nRobo-Joey: " #text sent to ai
#print(f"Prompt: {prompt}")
output = query({ "inputs": prompt, }) #get AIs response
#extract the answer from the output
generated = output[0]['generated_text'].replace(prompt, "") #text on the end of the
#prune any extra generated lines
if "\n" in generated:
singleLine = generated.split("\n")[0]
else:
singleLine = generated
self.history += username + ": "+ question + "\nRobo-Joey: " + singleLine + "\n" #add the response to the conversation history
#remove repetitions to stop bot from getting stuck in a loop
if len(set(self.history.split("\n"))) < len(self.history.split("\n")): #if there are any repetitions
self.history = "" #Wipe the history
#limit the length of history to one response
while len(self.history.split("\n")) > 8:
#remove the first 2 lines
self.history = self.history.split("\n",2)[2]
#print(f"History: {self.history}")
return singleLine
def handle_message(self, message):
"""Handle a message to see if it is a question for the AI"""
if message.channel.id not in ai_chat_channels: #if the channel is not in the list of channels to listen to
return None
#ignore messages from bots
if message.author.bot:
return None
return self.ask_ai(message.content, "Human") #ask the AI the question
if __name__ == "__main__":
print("Hello, I am Robo-Joey. Ask me a question:")
conversation = Conversation()
while True:
question = input("> ")
print(conversation.ask_ai(question, "Joey"))