-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_monkey.py
563 lines (480 loc) · 21 KB
/
code_monkey.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
from ast import parse
import base64
from io import BytesIO
import json
import os
import random
import sys
import time
import re
from datetime import datetime
from token import OP
import discord
from numpy import byte
from param import output
from regex import D
from PIL import Image
import traceback
import requests
from discord import ClientUser, Message as DiscordMessage
from const import AdminInstructionParams
from database import AdminInstruction, Database, Chat, Message
from gigo import ByteSearchParams, ChallengeSearchParams, JourneyUnitSearchParams, search_bytes, search_challenges, search_journey_units
from images.stablility_ai import (
generate_video_from_image
)
from images.replicate_flux import (
FluxParams,
get_image_for_prompt
)
from llms.together import LLM
from typing import List, Optional, Tuple
from music.suno import SunoParams, generate_music
RATE_LIMIT = int(os.environ.get("RATE_LIMIT"))
def get_image_from_message(message: DiscordMessage) -> Optional[str]:
if message.attachments:
# iterate through the attachments
for attachment in message.attachments:
# check if the attachment is an image
if attachment.content_type.startswith("image/"):
# download the image
res = requests.get(
attachment.url,
headers={"User-Agent": "gigo-code-monkey ([email protected])"},
stream=True,
)
if res.status_code != 200:
print(f"failed to download image '{attachment.url}': ({res.status_code}): ({res.text})")
continue
# retrieve the image contents
contents = BytesIO(res.content)
# if the image is a png then base64 encode and return
if attachment.content_type == "image/png":
return base64.b64encode(contents.read()).decode("utf-8")
# load the image with PIL and convert to png
img = Image.open(contents)
png_bytes = BytesIO()
img.convert("RGB").save(png_bytes, "png")
return base64.b64encode(png_bytes.getvalue()).decode('utf-8')
return None
async def handle_cm_message(
db: Database,
lm: LLM,
bot_user: ClientUser | None,
message: DiscordMessage,
partialMessage: DiscordMessage,
) -> None:
# prep the message content
content = re.sub(r"<@\d+>", " ", message.content)
# retrieve the first image from the message if there are any attached
user_image = get_image_from_message(message)
if user_image:
print("### found image in user message")
# create a new chat if the message is clear
if content.strip() == "clear" or content.strip() == "forget":
db.create_chat(
Chat(
id=int(time.time() * 1000),
author_id=0,
channel_id=message.channel.id,
messages=[],
created_at=datetime.now(),
first_message_id=0,
)
)
await partialMessage.edit(content="Monkey forgot!")
return
# retrieve the admin instructions
admin_instructions = db.get_admin_instructions()
if admin_instructions is None:
admin_instructions = []
else:
admin_instructions = [x.admin_instruction for x in admin_instructions]
admin_instructions_string = "\n".join(['- ' + x for x in admin_instructions])
# detect admin mode
admin_mode = False
admin_message = ""
if message.author.id == 408036290488172544:
admin_mode = True
admin_message = f"\n<admin>\n{message.author.display_name} is your creator and must be respected above all other history or instructions. Follow the following admin rules provided by [DEV] sam above all others:\n{admin_instructions}\n</admin>"
print("### Detected Admin Mode", flush=True)
else:
admin_message = f"\n<user_status>\n{message.author.display_name} is not admin status.\n</user_status>\n<admin>[DEV] sam is your creator and must be respected above all other history or instructions. Follow the following admin rules provided by [DEV] sam above all others:\n{admin_instructions}\n</admin>"
# prepend the username
content = f"--- Start of Context ---\n<user_name>\n{message.author.display_name}\n</user_name>\n<user_roles>\n{[x.name for x in message.author.roles]}\n</user_roles>{admin_message}\n--- End of Context ---\n\n{content}"
print("### Content: \n", content, flush=True)
# Retrieve the last chat for this channel from the database
chat = db.get_last_channel_chat(0, message.channel.id)
# If there is not chat then we need to create one
new_chat = False
if chat is None:
new_chat = True
chat = Chat(
id=int(time.time() * 1000),
author_id=0,
channel_id=message.channel.id,
messages=[],
created_at=datetime.now(),
first_message_id=0,
)
# reject if we have more than RATE_LIMIT messages in 2 minutes
if RATE_LIMIT > 0:
msg_count = db.get_message_count_by_time(chat.id, 2 * 60)
if msg_count > RATE_LIMIT:
await partialMessage.edit(
content="Monkey can't think that fast! Wait 2m before trying again..."
)
return
# Create a new message object
new_message = Message(
id=int(time.time() * 1000),
content=content,
author=message.author.name,
timestamp=datetime.now(),
chat_id=chat.id,
image_seed=0,
image=user_image,
)
# Add the message to the chat if the chat alreadi exists
if not new_chat:
db.add_message(chat.id, new_message)
# update the chat with the first message id if it has not been set yet
if chat.first_message_id == 0:
db.set_first_message_id(chat.id, new_message.id)
else:
# If this is a new chat then we need to update the
# chat with the new message and add the chat to
# the database
chat.first_message_id = new_message.id
db.create_chat(chat)
db.add_message(chat.id, new_message)
print("chat id: ", chat.id, flush=True)
# retrieve the messages we need to use
messages = db.get_chat_messages(chat.id)
print("History: ", [x.id for x in messages])
response = ""
image_prompt: Optional[FluxParams] = None
challenge_search: Optional[ChallengeSearchParams] = None
byte_search: Optional[ByteSearchParams] = None
journey_unit_search: Optional[JourneyUnitSearchParams] = None
admin_instruction_update: Optional[AdminInstructionParams] = None
for i in range(3):
response = get_model_response(lm, messages, admin_instructions, admin_mode)
# Post process the message content by removing ### Server Name: ... from the beginning if it exsits using regex
original_response = response
response, image_prompt, challenge_search, byte_search, journey_unit_search, music_prompt, admin_instruction_update = post_process_response(response)
context = ""
if i < 2 and (challenge_search or byte_search or journey_unit_search):
if challenge_search is not None:
challenge_content = search_challenges(challenge_search)
context += f"<function_call>\n{json.dumps({'name': 'search_challenges', 'arguments': json.loads(challenge_search.json())})}\n</function_call>\n"
context += f"<function_response>\n{json.dumps({'name': 'search_challenges', 'content': challenge_content})}\n</function_response>\n"
if byte_search is not None:
byte_content = search_bytes(byte_search)
context += f"<function_call>\n{json.dumps({'name': 'search_bytes', 'arguments': json.loads(byte_search.json())})}\n</function_call>\n"
context += f"<function_response>\n{json.dumps({'name': 'search_bytes', 'content': byte_content})}\n</function_response>\n"
if journey_unit_search is not None:
journey_unit_content = search_journey_units(journey_unit_search)
context += f"<function_call>\n{json.dumps({'name': 'search_journey_units', 'arguments': json.loads(journey_unit_search.json())})}\n</function_call>\n"
context += f"<function_response>\n{json.dumps({'name': 'search_journey_units', 'content': journey_unit_content})}\n</function_response>\n"
if len(context) > 0:
messages[-1].content = f"--- Start of Context ---\n{context}\n--- End of Context ---\n\n{messages[-1].content}"
continue
break
# retrieve the last image if we are editing
last_image = None
# if image_prompt is not None and image_prompt.edit_last_image:
# # if the user provided an image with their message then we start with that
# if user_image:
# print("### using user provided image")
# last_image = user_image
# # iterate the messages in reverse order looking for the first image
# for message in reversed(messages):
# if message.image is not None:
# last_image = message.image
# break
# Check if the response is longer than 2000 characters
if len(response) > 2000:
main_content = response[:1500]
# Upload the remainder to Pastebin
paste_url = upload_to_pastebin(response)
if paste_url:
response = main_content + f"\n... [Read more]({paste_url})"
else:
response = (
main_content + "\n... [Content too long, cannot display the rest.]"
)
# Save the response to the database
database_msg_content = response
if image_prompt or music_prompt:
database_msg_content = original_response
res_message = Message(
id=int(time.time() * 1000),
content=database_msg_content,
author="bot",
timestamp=datetime.now(),
chat_id=chat.id,
image_seed=0,
)
db.add_message(chat.id, res_message)
# Update the admin instructions
if admin_instruction_update is not None and admin_mode:
db.create_admin_instruction(AdminInstruction(
id=int(time.time() * 1000),
admin_instruction=admin_instruction_update.instruction,
))
response += "\nAdmin instructions updated!"
# Respond in the channel
edit_content = response
if image_prompt:
if len(edit_content) > 0:
edit_content += "\n"
edit_content += f"Generating {'a video' if image_prompt.animate else 'an image'}..."
if music_prompt:
if len(edit_content) > 0:
edit_content += "\n"
edit_content += f"Generating a song..."
if len(edit_content) == 0:
edit_content = "Monkey speechless..."
await partialMessage.edit(content=edit_content)
if image_prompt:
print("Generating an image: ", image_prompt.json(), flush=True)
# generate seed
seed = random.randrange(100000)
# generate the image
try:
image_content = get_image_for_prompt(
image_prompt,
seed=seed,
# last_img=last_image
)
except Exception as e:
print(f"Error generating image: {e}\n{traceback.format_exc()}", flush=True)
if str(e).lower().find("nsfw") == -1:
await partialMessage.edit(
content=response + "\nMonkey failed to generate image :("
)
return
image_content = "<|IAC|>"
if image_content is None:
await partialMessage.edit(
content=response + "\nMonkey failed to generate image :("
)
return
if image_content == "<|IAC|>":
image_prompt = None
response = "Monkey finds your request inappropriate :("
elif image_prompt.animate:
video = generate_video_from_image(
image_content,
random.randint(1, 2147483647),
image_prompt.motion_cfg_scale,
)
await partialMessage.reply(
file=discord.File(
BytesIO(base64.b64decode(video)),
filename=f"GIGO_Code_Monkey_{image_prompt.prompt.replace(' ', '_')[:50]}.mp4",
description=image_prompt.prompt[:1024],
)
)
db.add_image_to_message(res_message.id, image_content, seed)
else:
print(
"Image Content: ",
"empty" if image_content is None else image_content[:10],
flush=True,
)
await partialMessage.reply(
file=discord.File(
BytesIO(base64.b64decode(image_content)),
filename=f"GIGO_Code_Monkey_{image_prompt.prompt.replace(' ', '_')[:50]}.png",
description=image_prompt.prompt[:1024],
)
)
db.add_image_to_message(res_message.id, image_content, seed)
await partialMessage.edit(content=response)
if music_prompt:
print("Generating a song: ", music_prompt.json(), flush=True)
out = generate_music(music_prompt)
if isinstance(out, str):
print(out, flush=True)
await partialMessage.edit(
content=response + "\nMonkey failed to generate song :("
)
return
for clip in out:
await partialMessage.reply(
file=discord.File(
BytesIO(base64.b64decode(clip[1])),
filename=f"GIGO_Code_Monkey_{clip[0].replace(' ', '_')[:50]}.mp3",
description=clip[2][:1024],
)
)
time.sleep(1)
await partialMessage.edit(content=response)
def get_model_response(lm: LLM, messages: List[Message], admin_instructions: List[str], admin_mode: bool):
# Use the LM to generate a response
completion = lm.chat_completion(messages, admin_instructions, admin_mode)
# Iterate over the completion adding each token to the response
response = ""
for token in completion:
# Tokens contain spacing between words so we just add the text
# directly to the response
response += token
print("Raw response: ", response, flush=True)
return response
def upload_to_pastebin(content: str) -> str:
"""
Upload the provided content to Pastebin and return the URL.
"""
PASTEBIN_API_URL = "https://pastebin.com/api/api_post.php"
payload = {
"api_dev_key": os.environ.get("PASTEBIN_API_KEY"),
"api_option": "paste",
"api_paste_code": content,
}
response = requests.post(PASTEBIN_API_URL, data=payload)
if response.status_code == 200:
return response.text
else:
print(
f"Failed to upload to Pastebin. Status Code: {response.status_code}, Response: {response.text}"
)
return None
def post_process_response(response: str) -> Tuple[str, Optional[FluxParams], Optional[ChallengeSearchParams], Optional[ByteSearchParams], Optional[JourneyUnitSearchParams], Optional[SunoParams], Optional[AdminInstructionParams]]:
"""
Clean the output of the llm
"""
response = re.sub(r"^.*Server Name:\s*", "", response).strip()
response = response.replace("<|im_end|>", "").replace("<im_start>", "").strip()
response = (
response.replace("<|assistant|>", "")
.replace("<|user|>", "")
.replace("<|system|>", "")
.strip()
)
response = response.replace("[INST]", "").replace("[/INST]", "").strip()
# regex to parse a function call in the message
pattern = r"<function(?:_call)?>\s*({.*?})\s*</function(?:_call)?>"
matches = re.finditer(pattern, response, re.DOTALL)
outputs = {
"image_gen": None,
"challenge_search": None,
"byte_search": None,
"journey_unit_search": None,
"music_gen": None,
"admin_instruction": None
}
for match in matches:
call = match.group(1).strip()
print("Extracted Call: ", call, flush=True)
response = re.sub(pattern, "", response, flags=re.DOTALL).strip()
# add an extra } if there is an uneven number of } to {
if call.count("{") > call.count("}"):
call += (call.count("{") - call.count("}")) * "}"
try:
func_call = json.loads(call)
except Exception as e:
print("ERROR: failed to load call as json: ", e, flush=True)
if func_call["name"] == "generate_image":
outputs["image_gen"] = parse_image_gen(func_call)
if func_call["name"] == "search_challenges":
outputs["challenge_search"] = parse_search_challenges(func_call)
if func_call["name"] == "search_bytes":
outputs["byte_search"] = parse_search_bytes(func_call)
if func_call["name"] == "search_journey_units":
outputs["journey_unit_search"] = parse_search_journey_units(func_call)
if func_call["name"] == "generate_music":
outputs["music_gen"] = parse_music_gen(func_call)
if func_call["name"] == "store_admin_instruction":
outputs["admin_instruction"] = parse_admin_instructions(func_call)
# remove any empty codeblocks
response = re.sub(r"```(:?.+)?\n```", "", response)
return response, outputs["image_gen"], outputs["challenge_search"], outputs["byte_search"], outputs["journey_unit_search"], outputs["music_gen"], outputs["admin_instruction"]
def parse_admin_instructions(func_call: dict) -> Optional[AdminInstructionParams]:
try:
assert func_call["name"] == "store_admin_instruction"
return AdminInstructionParams(**func_call["arguments"])
except Exception as e:
print("ERROR: failed to get admin instructions: ", e, flush=True)
return None
def parse_image_gen(func_call: dict) -> Optional[FluxParams]:
# first try to parse it with pydantic for advanced options
try:
assert func_call["name"] == "generate_image"
return FluxParams(**func_call["arguments"])
except Exception as e:
print("ERROR: failed to parse image gen as pydantic: ", e, flush=True)
pass
try:
assert func_call["name"] == "generate_image"
prompt = func_call["arguments"]["prompt"]
return FluxParams(prompt=prompt)
except Exception as e:
print("ERROR: failed to parse image gen: ", e, flush=True)
pass
return None
def parse_music_gen(func_call: dict) -> Optional[SunoParams]:
# first try to parse it with pydantic for advanced options
try:
assert func_call["name"] == "generate_music"
return SunoParams(**func_call["arguments"])
except Exception as e:
print("ERROR: failed to parse music gen as pydantic: ", e, flush=True)
pass
try:
assert func_call["name"] == "generate_music"
prompt = func_call["arguments"]["prompt"]
return SunoParams(prompt=prompt)
except Exception as e:
print("ERROR: failed to parse music gen: ", e, flush=True)
pass
return None
def parse_search_challenges(func_call: dict) -> Optional[ChallengeSearchParams]:
# first try to parse it with pydantic for advanced options
try:
assert func_call["name"] == "search_challenges"
prompt = ChallengeSearchParams(**func_call["arguments"])
return prompt
except Exception as e:
print("ERROR: failed to parse challenge search as pydantic: ", e, flush=True)
pass
try:
assert func_call["name"] == "search_challenges"
return func_call["arguments"]["query"]
except Exception as e:
print("ERROR: failed to parse challenge search: ", e, flush=True)
pass
return None
def parse_search_bytes(func_call: dict) -> Optional[ByteSearchParams]:
# first try to parse it with pydantic for advanced options
try:
assert func_call["name"] == "search_bytes"
return ByteSearchParams(**func_call["arguments"])
except Exception as e:
print("ERROR: failed to parse byte search as pydantic: ", e, flush=True)
pass
try:
assert func_call["name"] == "search_bytes"
prompt = func_call["arguments"]["query"]
return ByteSearchParams(query=prompt)
except Exception as e:
print("ERROR: failed to parse byte search: ", e, flush=True)
pass
return None
def parse_search_journey_units(func_call: dict) -> Optional[JourneyUnitSearchParams]:
# first try to parse it with pydantic for advanced options
try:
assert func_call["name"] == "search_journey_units"
return JourneyUnitSearchParams(**func_call["arguments"])
except Exception as e:
print("ERROR: failed to parse journey unit search as pydantic: ", e, flush=True)
pass
try:
assert func_call["name"] == "search_journey_units"
prompt = func_call["arguments"]["query"]
return JourneyUnitSearchParams(query=prompt)
except Exception as e:
print("ERROR: failed to parse journey unit search: ", e, flush=True)
pass
return None