-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
59 lines (49 loc) · 1.88 KB
/
bot.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
import discord
import openai
import os
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
openai.api_key = os.environ['OPENAI_API_KEY']
model = "gpt-3.5-turbo"
text_limit = 2000
sessions = {}
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.channel.type == discord.ChannelType.text:
thread = await message.create_thread(name='New chat', auto_archive_duration=60)
messages = [
{"role": "user", "content": message.content}
]
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
answer = response['choices'][0]['message']['content']
sessions[thread.id] = messages
sessions[thread.id].append({"role": "assistant", "content": answer})
for i in range(0, len(answer), text_limit):
await thread.send(answer[i:i + text_limit])
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "user", "content": f"'{message.content}'를 최대 세 단어로 요약"}
]
)
answer = response['choices'][0]['message']['content']
await thread.edit(
name=answer
)
elif message.channel.type == discord.ChannelType.public_thread:
sessions[message.channel.id].append({"role": "user", "content": message.content})
response = openai.ChatCompletion.create(
model=model,
messages=sessions[message.channel.id]
)
answer = response['choices'][0]['message']['content']
sessions[message.channel.id].append({"role": "assistant", "content": answer})
for i in range(0, len(answer), text_limit):
await message.channel.send(answer[i:i + text_limit])
client.run(os.environ['DISCORD_TOKEN'])