Added GPT-3.5: Commands /ask for questions (without context atm) and /image for image generation

This commit is contained in:
Marco Loewe 2023-11-15 12:05:36 +01:00
parent ac880a863c
commit 8b8ebc35aa
4 changed files with 61 additions and 3 deletions

7
.gitignore vendored
View File

@ -1 +1,6 @@
config.local.json
config.local.json
__pycache__/
.vscode/
test.py

21
bot.py
View File

@ -5,6 +5,8 @@ import json
import discord
from discord import app_commands
from chatgpt import ChatGPT
with open('config.json', 'r') as f:
data = json.load(f)
@ -30,7 +32,6 @@ async def on_ready():
await client.get_channel(1173561093579362305).send('Update done')
write_json('update', 'none')
@tree.command(name = 'cool', description = 'Check how cool you are!', guild=discord.Object(id='745287426402156555'))
async def cool(ctx: discord.Interaction):
cool_factor = round(random.uniform(0, 100), 2)
@ -43,6 +44,24 @@ async def update(ctx: discord.Interaction):
write_json('update', 'pending')
subprocess.run('./update.sh')
@tree.command(name = 'ask', description = 'Query anything, powered by GPT-3.5', guild=discord.Object(id='745287426402156555'))
async def ask(ctx: discord.Interaction, query: str):
await ctx.response.defer(thinking=True)
gpt = ChatGPT(data['openAI'])
response = gpt.completion(query)
await ctx.followup.send(response)
@tree.command(name = 'image', description = 'Generate an image from a text prompt, powered by DALL-E-3', guild=discord.Object(id='745287426402156555'))
async def image(ctx: discord.Interaction, prompt: str):
await ctx.response.defer(thinking=True)
gpt = ChatGPT(data['openAI'])
response = gpt.image(prompt)
await ctx.followup.send(response)
@update.error
async def update_error(ctx: discord.Interaction, error):
if isinstance(error, discord.app_commands.MissingRole):

33
chatgpt.py Normal file
View File

@ -0,0 +1,33 @@
from openai import OpenAI
class ChatGPT:
def __init__(self, api_key):
self.api_key = api_key
self.client = OpenAI(
api_key=api_key
)
self.role = {'role': 'system', 'content': 'You are a helpful personal assistent called Steve.'}
def completion(self, message: str) -> str:
messages = [
self.role,
{'role': 'user', 'content': message}
]
response = self.client.chat.completions.create(
model='gpt-3.5-turbo',
messages=messages
)
return response.choices[0].message.content
def image(self, prompt: str) -> str:
response = self.client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1
)
return response.data[0].url

View File

@ -1,3 +1,4 @@
{
"token": "Your token here"
"token": "Your token here",
"openAI": "OpenAI API Key"
}