Created
September 26, 2023 15:53
-
-
Save kalinochkind/4a7498c23090c1c7f5cd35801caa710e to your computer and use it in GitHub Desktop.
Discord gpt bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import discord | |
import sqlite3 | |
import datetime | |
import openai | |
import os | |
import tiktoken | |
openai.api_type = "azure" | |
openai.api_key = os.environ['OPENAI_TOKEN'] | |
openai.api_base = "https://deep-ai.openai.azure.com" | |
openai.api_version = "2023-03-15-preview" | |
DISCORD_TOKEN = os.environ['DISCORD_TOKEN'] | |
con = sqlite3.connect("gpt.sqlite3") | |
cur = con.cursor() | |
cur.execute('create table if not exists message(channel text, role text, message text, created_at timestamp, expired boolean)') | |
encoding = tiktoken.encoding_for_model("gpt-4") | |
MAX_TOKENS = 32768 * 7 // 8 | |
class MyClient(discord.Client): | |
async def on_ready(self): | |
print(f'Logged on as {self.user}!') | |
async def make_reply(self, messages): | |
chat_completion = await openai.ChatCompletion.acreate(deployment_id="gpt-4-32k", model="gpt-4", messages=[{"role": i[1], "content": i[2]} for i in messages]) | |
return chat_completion.choices[0].message.content | |
async def on_message(self, message): | |
if message.author.bot: | |
return | |
print(f'Message from {message.author}: {message.content}, {message.channel.id}') | |
if self.user in message.mentions and message.content.strip(): | |
message_received_at = datetime.datetime.now() | |
cur = con.cursor() | |
cnt = cur.execute('select rowid, role, message from message where channel=? and not expired order by created_at', (message.channel.id,)) | |
msgs = cur.fetchall() | |
print(msgs) | |
msgs.append((None, 'user', str(message.content))) | |
lens = [len(encoding.encode(i[2])) for i in msgs] | |
while sum(lens) > MAX_TOKENS: | |
print(f'Dropping message {msgs[0]}') | |
cur.execute('update message set expired=true where rowid=?', (msgs[0][0],)) | |
msgs = msgs[1:] | |
lens = lens[1:] | |
reply_text = await self.make_reply(msgs) | |
print(f'Replying with {reply_text}') | |
cur.execute('insert into message (channel, role, message, created_at, expired) values(?, ?, ?, ?, ?)', (message.channel.id, 'user', message.content, message_received_at, False)) | |
cur.execute('insert into message (channel, role, message, created_at, expired) values(?, ?, ?, ?, ?)', (message.channel.id, 'assistant', reply_text, datetime.datetime.now(), False)) | |
con.commit() | |
while reply_text: | |
await message.reply(reply_text[:1990]) | |
reply_text = reply_text[1990:] | |
intents = discord.Intents.default() | |
intents.message_content = True | |
client = MyClient(intents=intents) | |
client.run(DISCORD_TOKEN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment