-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathmain.py
312 lines (278 loc) · 10.6 KB
/
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from collections import defaultdict
from typing import Literal, Optional, Union
import discord
from discord import Message as DiscordMessage, app_commands
import logging
from src.base import Message, Conversation, ThreadConfig
from src.constants import (
BOT_INVITE_URL,
DISCORD_BOT_TOKEN,
EXAMPLE_CONVOS,
ACTIVATE_THREAD_PREFX,
MAX_THREAD_MESSAGES,
SECONDS_DELAY_RECEIVING_MSG,
AVAILABLE_MODELS,
DEFAULT_MODEL,
)
import asyncio
from src.utils import (
logger,
should_block,
close_thread,
is_last_message_stale,
discord_message_to_message,
)
from src import completion
from src.completion import generate_completion_response, process_response
from src.moderation import (
moderate_message,
send_moderation_blocked_message,
send_moderation_flagged_message,
)
logging.basicConfig(
format="[%(asctime)s] [%(filename)s:%(lineno)d] %(message)s", level=logging.INFO
)
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = discord.app_commands.CommandTree(client)
thread_data = defaultdict()
@client.event
async def on_ready():
logger.info(f"We have logged in as {client.user}. Invite URL: {BOT_INVITE_URL}")
completion.MY_BOT_NAME = client.user.name
completion.MY_BOT_EXAMPLE_CONVOS = []
for c in EXAMPLE_CONVOS:
messages = []
for m in c.messages:
if m.user == "Lenard":
messages.append(Message(user=client.user.name, text=m.text))
else:
messages.append(m)
completion.MY_BOT_EXAMPLE_CONVOS.append(Conversation(messages=messages))
await tree.sync()
# /chat message:
@tree.command(name="chat", description="Create a new thread for conversation")
@discord.app_commands.checks.has_permissions(send_messages=True)
@discord.app_commands.checks.has_permissions(view_channel=True)
@discord.app_commands.checks.bot_has_permissions(send_messages=True)
@discord.app_commands.checks.bot_has_permissions(view_channel=True)
@discord.app_commands.checks.bot_has_permissions(manage_threads=True)
@app_commands.describe(message="The first prompt to start the chat with")
@app_commands.describe(model="The model to use for the chat")
@app_commands.describe(
temperature="Controls randomness. Higher values mean more randomness. Between 0 and 1"
)
@app_commands.describe(
max_tokens="How many tokens the model should output at max for each message."
)
async def chat_command(
int: discord.Interaction,
message: str,
model: AVAILABLE_MODELS = DEFAULT_MODEL,
temperature: Optional[float] = 1.0,
max_tokens: Optional[int] = 512,
):
try:
# only support creating thread in text channel
if not isinstance(int.channel, discord.TextChannel):
return
# block servers not in allow list
if should_block(guild=int.guild):
return
user = int.user
logger.info(f"Chat command by {user} {message[:20]}")
# Check for valid temperature
if temperature is not None and (temperature < 0 or temperature > 1):
await int.response.send_message(
f"You supplied an invalid temperature: {temperature}. Temperature must be between 0 and 1.",
ephemeral=True,
)
return
# Check for valid max_tokens
if max_tokens is not None and (max_tokens < 1 or max_tokens > 4096):
await int.response.send_message(
f"You supplied an invalid max_tokens: {max_tokens}. Max tokens must be between 1 and 4096.",
ephemeral=True,
)
return
try:
# moderate the message
flagged_str, blocked_str = moderate_message(message=message, user=user)
await send_moderation_blocked_message(
guild=int.guild,
user=user,
blocked_str=blocked_str,
message=message,
)
if len(blocked_str) > 0:
# message was blocked
await int.response.send_message(
f"Your prompt has been blocked by moderation.\n{message}",
ephemeral=True,
)
return
embed = discord.Embed(
description=f"<@{user.id}> wants to chat! 🤖💬",
color=discord.Color.green(),
)
embed.add_field(name="model", value=model)
embed.add_field(name="temperature", value=temperature, inline=True)
embed.add_field(name="max_tokens", value=max_tokens, inline=True)
embed.add_field(name=user.name, value=message)
if len(flagged_str) > 0:
# message was flagged
embed.color = discord.Color.yellow()
embed.title = "⚠️ This prompt was flagged by moderation."
await int.response.send_message(embed=embed)
response = await int.original_response()
await send_moderation_flagged_message(
guild=int.guild,
user=user,
flagged_str=flagged_str,
message=message,
url=response.jump_url,
)
except Exception as e:
logger.exception(e)
await int.response.send_message(
f"Failed to start chat {str(e)}", ephemeral=True
)
return
# create the thread
thread = await response.create_thread(
name=f"{ACTIVATE_THREAD_PREFX} {user.name[:20]} - {message[:30]}",
slowmode_delay=1,
reason="gpt-bot",
auto_archive_duration=60,
)
thread_data[thread.id] = ThreadConfig(
model=model, max_tokens=max_tokens, temperature=temperature
)
async with thread.typing():
# fetch completion
messages = [Message(user=user.name, text=message)]
response_data = await generate_completion_response(
messages=messages, user=user, thread_config=thread_data[thread.id]
)
# send the result
await process_response(
user=user, thread=thread, response_data=response_data
)
except Exception as e:
logger.exception(e)
await int.response.send_message(
f"Failed to start chat {str(e)}", ephemeral=True
)
# calls for each message
@client.event
async def on_message(message: DiscordMessage):
try:
# block servers not in allow list
if should_block(guild=message.guild):
return
# ignore messages from the bot
if message.author == client.user:
return
# ignore messages not in a thread
channel = message.channel
if not isinstance(channel, discord.Thread):
return
# ignore threads not created by the bot
thread = channel
if thread.owner_id != client.user.id:
return
# ignore threads that are archived locked or title is not what we want
if (
thread.archived
or thread.locked
or not thread.name.startswith(ACTIVATE_THREAD_PREFX)
):
# ignore this thread
return
if thread.message_count > MAX_THREAD_MESSAGES:
# too many messages, no longer going to reply
await close_thread(thread=thread)
return
# moderate the message
flagged_str, blocked_str = moderate_message(
message=message.content, user=message.author
)
await send_moderation_blocked_message(
guild=message.guild,
user=message.author,
blocked_str=blocked_str,
message=message.content,
)
if len(blocked_str) > 0:
try:
await message.delete()
await thread.send(
embed=discord.Embed(
description=f"❌ **{message.author}'s message has been deleted by moderation.**",
color=discord.Color.red(),
)
)
return
except Exception as e:
await thread.send(
embed=discord.Embed(
description=f"❌ **{message.author}'s message has been blocked by moderation but could not be deleted. Missing Manage Messages permission in this Channel.**",
color=discord.Color.red(),
)
)
return
await send_moderation_flagged_message(
guild=message.guild,
user=message.author,
flagged_str=flagged_str,
message=message.content,
url=message.jump_url,
)
if len(flagged_str) > 0:
await thread.send(
embed=discord.Embed(
description=f"⚠️ **{message.author}'s message has been flagged by moderation.**",
color=discord.Color.yellow(),
)
)
# wait a bit in case user has more messages
if SECONDS_DELAY_RECEIVING_MSG > 0:
await asyncio.sleep(SECONDS_DELAY_RECEIVING_MSG)
if is_last_message_stale(
interaction_message=message,
last_message=thread.last_message,
bot_id=client.user.id,
):
# there is another message, so ignore this one
return
logger.info(
f"Thread message to process - {message.author}: {message.content[:50]} - {thread.name} {thread.jump_url}"
)
channel_messages = [
discord_message_to_message(message)
async for message in thread.history(limit=MAX_THREAD_MESSAGES)
]
channel_messages = [x for x in channel_messages if x is not None]
channel_messages.reverse()
# generate the response
async with thread.typing():
response_data = await generate_completion_response(
messages=channel_messages,
user=message.author,
thread_config=thread_data[thread.id],
)
if is_last_message_stale(
interaction_message=message,
last_message=thread.last_message,
bot_id=client.user.id,
):
# there is another message and its not from us, so ignore this response
return
# send response
await process_response(
user=message.author, thread=thread, response_data=response_data
)
except Exception as e:
logger.exception(e)
client.run(DISCORD_BOT_TOKEN)