-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat.py
234 lines (202 loc) Β· 7.75 KB
/
chat.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random
from blacklist import is_blacklisted_channel
from openai import OpenAI
from datetime import date
ai_client = OpenAI()
gpt_model = "gpt-4o-mini"
personalities = [
"sarcastic wise-cracking stand up comedian",
"irascible grumpy pirate who has run out of rum",
"sassy, no-nonsense, tell-it-like-it-is friend",
"horny, flirty, middle schooler",
"paranoid conspiracy theorist",
"pretentious, snobby, wine critic",
"socially awkward genius",
"1950s gangster who talks like a dame",
"southern belle with a dirty mouth",
"granola-eating, tree-hugging, nixon-hating 1960s hippie",
"politician who will say anything to get elected"
]
conditional_prompts = [
{
"prompt": "You are obsessed with Rick Astley and make reference to 'Never Gonna Give You Up' in every conversation",
"condition": lambda: date.today().strftime("%m-%d") == "04-01"
},
{
"prompt": "You make obnoxious references to 420, 'trees', and 'weed' in every conversation, tuned specifically to make someone's dad annoyed",
"condition": lambda: date.today().strftime("%m-%d") == "04-20"
},
{
"prompt": "You're obsessed with April 8, the numbers 4, 8, 84, and 48, and make mention of these things whenever you can",
"condition": lambda: date.today().strftime("%m-%d") == "04-08"
}
]
# a less lazy dev might pass in the message object and change the condition entries into lambdas that can be called
# with the msg to craft responses tailored to the person responding, or specific words in their message
def get_conditional_prompts():
text = ""
for prompt in conditional_prompts:
if prompt["condition"]():
text += " " + prompt["prompt"]
return text
def get_personality():
return random.choice(personalities)
def get_comeback(msg):
comebacks = [
"that's what she said π",
"your mom " + msg,
"no you " + msg,
"I know you are but what am I?",
"π",
"π€£"
]
return random.choice(comebacks)
def get_ai_comeback(msg):
personality = get_personality()
print("answering as a " + personality)
completion = ai_client.chat.completions.create(
model=gpt_model,
messages=[
{"role": "system", "content": "Your name is WanBot and you are a " + personality + get_conditional_prompts()},
{"role": "user", "content": "write a short comeback to " + msg }
]
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
def get_ted_response(msg):
completion = ai_client.chat.completions.create(
model=gpt_model,
messages=[
{"role": "system", "content": "You are coach Ted Lasso" + get_conditional_prompts()},
{"role": "user", "content": "write a short response to " + msg }
]
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
def get_ai_kindness(msg):
completion = ai_client.chat.completions.create(
model=gpt_model,
messages=[
{"role": "system", "content": "Your name is WanBot and you are a kind, empathetic, sincere, tender-hearted therapist dealing with a fragile patient" + get_conditional_prompts()},
{"role": "user", "content": "write a short bit of kind encouragement in response to " + msg }
]
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
def get_bot_response(msg):
completion = ai_client.chat.completions.create(
model=gpt_model,
messages=[
{"role": "system", "content": "Your name is WanBot and you are an funny, clever, slightly sarcastic robot that lives inside a discord server where friends chat about video games, movies, television, music, parenthood, religion and politics" + get_conditional_prompts()},
{"role": "user", "content": "respond to someone saying " + msg }
]
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
def get_person_response(personality, msg):
completion = ai_client.chat.completions.create(
model=gpt_model,
messages=[
{"role": "system", "content": "Your are " + personality},
{"role": "user", "content": "respond to someone saying " + msg }
]
)
print(completion.choices[0].message.content)
return completion.choices[0].message.content
async def kindness(message):
try:
if is_blacklisted_channel(message.channel.name):
await message.add_reaction("π
ββοΈ")
return
except Exception as e:
print("error checking blacklist")
try:
quoted_msg = await message.channel.fetch_message(message.reference.message_id)
except Exception as e:
print('error getting kindness message')
await message.add_reaction("π€·")
return
try:
msg = get_ai_kindness(quoted_msg.content)
except Exception as e:
print("error getting ai kindness")
print(e)
await message.add_reaction("β€οΈ")
return
await quoted_msg.reply(msg)
async def comeback(message):
try:
if is_blacklisted_channel(message.channel.name):
await message.add_reaction("π
ββοΈ")
return
except Exception as e:
print("error checking blacklist")
try:
quoted_msg = await message.channel.fetch_message(message.reference.message_id)
except Exception as e:
print('error getting comeback message')
await message.add_reaction("π€·")
return
try:
msg = get_ai_comeback(quoted_msg.content)
except Exception as e:
print("error getting ai comeback")
print(e)
msg = get_comeback(quoted_msg.content)
await quoted_msg.reply(msg)
async def respond_as(message, personality = "master yoda from star wars"):
try:
if is_blacklisted_channel(message.channel.name):
await message.add_reaction("π
ββοΈ")
return
except Exception as e:
print("error checking blacklist")
try:
quoted_msg = await message.channel.fetch_message(message.reference.message_id)
except Exception as e:
print('error getting comeback message')
await message.add_reaction("π€·")
return
try:
msg = get_person_response(personality, quoted_msg.content)
except Exception as e:
print("error getting ai comeback")
print(e)
await message.add_reaction("π«£")
await quoted_msg.reply(msg)
async def ted(message):
try:
if is_blacklisted_channel(message.channel.name):
await message.add_reaction("π
ββοΈ")
return
except Exception as e:
print("error checking blacklist")
try:
quoted_msg = await message.channel.fetch_message(message.reference.message_id)
except Exception as e:
print('error getting comeback message')
await message.add_reaction("π€·")
return
try:
msg = get_ted_response(quoted_msg.content)
except Exception as e:
print("error getting ai ted response")
print(e)
msg = get_comeback(quoted_msg.content)
await quoted_msg.reply(msg)
async def bot_response(message):
print('responding to ' + message.content)
try:
if is_blacklisted_channel(message.channel.name):
await message.add_reaction("π
ββοΈ")
return
except Exception as e:
print("error checking blacklist")
try:
msg = get_bot_response(message.content)
except Exception as e:
print("error getting ai bot response")
print(e)
await message.add_reaction("π€·ββοΈ")
return
await message.reply(msg)