|
|
|
|
@@ -1,10 +1,11 @@
|
|
|
|
|
import config
|
|
|
|
|
import openai
|
|
|
|
|
from openai import AsyncOpenAI
|
|
|
|
|
import discord
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
|
from openai import AsyncOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("garflog")
|
|
|
|
|
@@ -32,59 +33,67 @@ if not logger.hasHandlers():
|
|
|
|
|
|
|
|
|
|
openai.api_key = config.OPENAI_TOKEN
|
|
|
|
|
jonkey = config.JONBOT_TOKEN
|
|
|
|
|
model = config.TXT_MODEL
|
|
|
|
|
txtmodel = config.TXT_MODEL
|
|
|
|
|
sysprompt = config.SYSTEM_PROMPT
|
|
|
|
|
|
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
|
intents.messages = True
|
|
|
|
|
intents.message_content = True
|
|
|
|
|
client = discord.Client(intents=intents)
|
|
|
|
|
|
|
|
|
|
client = commands.Bot(
|
|
|
|
|
command_prefix=["Jonbot ", "jonbot ", "Jon", "jon"],
|
|
|
|
|
case_insensitive=True,
|
|
|
|
|
intents=intents,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@client.event
|
|
|
|
|
async def on_ready():
|
|
|
|
|
print(f"Logged in as {client.user.name} running {model}.", flush=True)
|
|
|
|
|
print(f"Logged in as {client.user.name} running {txtmodel}.", flush=True)
|
|
|
|
|
|
|
|
|
|
@client.command(name="chat")
|
|
|
|
|
async def jonchat(ctx, *, prompt):
|
|
|
|
|
if "is this true" in prompt.lower():
|
|
|
|
|
messages = [msg async for msg in ctx.channel.history(limit=2)]
|
|
|
|
|
prompt = messages[1].content
|
|
|
|
|
prompt = f"Is this true: {prompt}"
|
|
|
|
|
answer = await generate_chat(prompt)
|
|
|
|
|
logger.info(
|
|
|
|
|
f"Chat Request - User: {ctx.author.name}, Server: {ctx.guild.name}, Prompt: {prompt}"
|
|
|
|
|
)
|
|
|
|
|
await ctx.reply(answer)
|
|
|
|
|
|
|
|
|
|
@client.event
|
|
|
|
|
async def on_message(message):
|
|
|
|
|
if message.author == client.user:
|
|
|
|
|
return
|
|
|
|
|
if message.content.lower().startswith("hey jon") or isinstance(message.channel, discord.DMChannel):
|
|
|
|
|
question = message.content[7:] if message.content.lower().startswith("hey jon") else message.content
|
|
|
|
|
try:
|
|
|
|
|
response = openai.ChatCompletion.create(
|
|
|
|
|
model=model,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": "Pretend you are friendly Jon Arbuckle."},
|
|
|
|
|
{"role": "user", "content": f"{question}"}
|
|
|
|
|
],
|
|
|
|
|
max_tokens=400
|
|
|
|
|
)
|
|
|
|
|
answer = response['choices'][0]['message']['content']
|
|
|
|
|
answer = answer.replace("AI language model", "American citizen")
|
|
|
|
|
answer = answer.replace("language model AI", "citizen of the United States")
|
|
|
|
|
await message.channel.send(answer)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
e = str(e)
|
|
|
|
|
await message.channel.send(f"`JonBot Error: {e}`")
|
|
|
|
|
|
|
|
|
|
content = message.content.strip()
|
|
|
|
|
lower = content.lower()
|
|
|
|
|
if lower.startswith("hey jon") or isinstance(
|
|
|
|
|
message.channel, discord.DMChannel
|
|
|
|
|
):
|
|
|
|
|
ctx = await client.get_context(message)
|
|
|
|
|
await jonchat(ctx, prompt=content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oai = AsyncOpenAI(
|
|
|
|
|
api_key=config.OPENAI_TOKEN,
|
|
|
|
|
base_url=config.BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def generate_chat(self, question: str) -> str:
|
|
|
|
|
async def generate_chat(question: str) -> str:
|
|
|
|
|
try:
|
|
|
|
|
response = await oai.chat.completions.create(
|
|
|
|
|
model=self.txtmodel,
|
|
|
|
|
model=txtmodel,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": self.sysprompt},
|
|
|
|
|
{"role": "system", "content": sysprompt},
|
|
|
|
|
{"role": "user", "content": question},
|
|
|
|
|
],
|
|
|
|
|
max_tokens=400,
|
|
|
|
|
temperature=1.2,
|
|
|
|
|
)
|
|
|
|
|
answer = response.choices[0].message.content
|
|
|
|
|
return answer.replace("an AI language model", "a cartoon animal")
|
|
|
|
|
return answer
|
|
|
|
|
except openai.BadRequestError as e:
|
|
|
|
|
logger.error(e)
|
|
|
|
|
return f"`JonBot Error: {e}`"
|
|
|
|
|
|