quick and dirty pass at a /gpt function
This commit is contained in:
parent
6c7f305ce6
commit
8aa9552b4e
31
app/cogs/gpt.py
Executable file
31
app/cogs/gpt.py
Executable file
@ -0,0 +1,31 @@
|
||||
from discord import option
|
||||
from discord.ext import commands
|
||||
|
||||
|
||||
class Gpt(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot: commands.Bot = bot
|
||||
|
||||
@commands.slash_command(
|
||||
guild_ids=None,
|
||||
name="gpt",
|
||||
description="Ask dalebot a question, he'll do his best to answer",
|
||||
)
|
||||
@option(
|
||||
name="prompt",
|
||||
description="What you want to ask dale-bot",
|
||||
required=True,
|
||||
)
|
||||
async def gpt(self, ctx: commands.Context, prompt):
|
||||
import gpt
|
||||
|
||||
# try:
|
||||
await ctx.defer()
|
||||
embed = gpt.answer_question(prompt)
|
||||
await ctx.send_followup(embed=embed)
|
||||
# except Exception as e:
|
||||
# await ctx.send(e)
|
||||
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(Gpt(bot))
|
31
app/gpt.py
Normal file
31
app/gpt.py
Normal file
@ -0,0 +1,31 @@
|
||||
import discord
|
||||
import json
|
||||
import requests
|
||||
|
||||
|
||||
def answer_question(prompt):
|
||||
bots_context = "You are located in zipcode 94549. You are a chatbot written in python and you are answering questions for me"
|
||||
|
||||
session = requests.Session()
|
||||
payload = {
|
||||
"model": "text-davinci-003",
|
||||
"messages": [
|
||||
{"role": "system", "content": bots_context},
|
||||
{"role": "user", "content": prompt},
|
||||
],
|
||||
}
|
||||
r = requests.post(
|
||||
"https://sharegpt.churchless.tech/share/v1/chat",
|
||||
headers={"Content-Type": "application/json"},
|
||||
data=json.dumps(payload),
|
||||
)
|
||||
answer = json.loads(r.text)["choices"][0]["message"]["content"]
|
||||
|
||||
embed = discord.Embed(description=answer, color=discord.Color.green(), type="rich")
|
||||
embed.set_author(name="You asked me: %s" % prompt)
|
||||
embed.set_thumbnail(
|
||||
url="https://cdn.discordapp.com/emojis/1087404359844892712.webp"
|
||||
)
|
||||
return embed
|
||||
# answer = answer.replace("```python", "")
|
||||
# answer = answer.replace("```", "")
|
Loading…
x
Reference in New Issue
Block a user