-
Notifications
You must be signed in to change notification settings - Fork 0
/
asssistants.py
91 lines (81 loc) · 3.01 KB
/
asssistants.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
import os
import asyncio
import discord
import json
from discord import Client, Intents
from openai import OpenAI
openai = OpenAI(api_key='youropenaikey')
discord_token = "yourdiscordtoken"
assistant_id = "yourassistantid"
intents = Intents.all()
client = Client(intents=intents)
def get_thread(discord_thread):
try:
threads = open("threads.json", "r")
except IOError:
newfile = open("threads.json", "w")
newfile.write("{}")
newfile.close()
print("initial threads.json written")
threads = open("threads.json", "r")
data = json.load(threads)
openai_thread = data[discord_thread] if discord_thread in data else create_openai_thread(discord_thread)
threads.close()
return openai_thread
def create_openai_thread(discord_thread):
thread_id = openai.beta.threads.create().id
try:
threads = open("threads.json", "r+")
except IOError:
newfile = open("threads.json", "w")
newfile.write("{}")
newfile.close()
threads = open("threads.json", "r+")
data = json.load(threads)
new_thread = "{ \"" + str(discord_thread) + "\" : \"" + str(thread_id) + "\" }"
new_thread = json.loads(new_thread)
print("thread to add : " + str(new_thread))
data.update(new_thread)
#overwrite threads
threads.seek(0)
json.dump(data, threads)
threads.close()
return thread_id
def send_openai_message(discord_thread, user, content):
thread_id = get_thread(discord_thread)
message = openai.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=user + ": " + content + " {Important! limit reply to 2000 characters. Less is better.}",
)
run = openai.beta.threads.runs.create(
thread_id=thread_id,
assistant_id=assistant_id
)
while run.status != "completed":
keep_retrieving_run = openai.beta.threads.runs.retrieve(
thread_id=thread_id,
run_id=run.id
)
print(f"Run status: {keep_retrieving_run.status}")
if keep_retrieving_run.status == "completed":
print("\n")
break
reply = openai.beta.threads.messages.list(
thread_id=thread_id
)
return str(reply.data[0].content[0].text.value)
@client.event
async def on_message(message):
if message.author.bot or not message.content:
return
print(message.channel.type)
if "thread" in str(message.channel.type):
async with message.channel.typing():
status = str("gpt-assistant is Typing...")
await message.reply(send_openai_message(str(message.channel.id), str(message.author.display_name), str(message.content)))
else:
thread = await message.create_thread(name=str(message.content)[:20], auto_archive_duration=60)
async with client.get_channel(int(thread.id)).typing():
await client.get_channel(int(thread.id)).send(send_openai_message(str(client.get_channel(int(thread.id)).id), str(message.author), str(message.content)))
client.run(discord_token)