Help with Discord Bot GPT 3.5-TURBO Python

I must said I’m pretty noob when it comes to coding but enjoy it a lot and I’m learning through videos and tons of internet websites. All that said, I would really appreciate your help guys.

I have a working simple Discord bot, currently using OpenAI Model Engine text-davinci-003. I’ve been trying to make the necessary changes so that it can start running gpt-3.5-turbo. Ive tried to change the API Call and format to https://api.openai.com/v1/chat/completions

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

But without luck.

I would really appreciate you help.

This is my code so far:

from discord.ext import commands
from dotenv import load_dotenv
import discord
import os
import openai
import textwrap
import asyncio


load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

openai.api_key = ""
model_engine = "text-davinci-003"


class Bot(Client):[spoiler]
[details="Summary"]
This text will be hidden
[/details]
[/spoiler]

  def __init__(self, *, intents: Intents):
    super().__init__(intents=intents)
    self.tree = app_commands.CommandTree(self)
    self.conversations = {}

  async def setup_hook(self) -> None:
    await self.tree.sync(guild=None)


bot = Bot(intents=Intents.default())


@bot.event
async def on_ready():
  print("Online")
  await bot.change_presence(activity=discord.Game("WithChatGPT"))


@bot.tree.command(description="Generates text using OpenAI GPT-3 API")
async def chatgpt(interaction: Interaction, prompt: str):
    await interaction.response.defer(ephemeral = True) # defer the interaction
    
    user_id = interaction.user.id
    if user_id not in bot.conversations:
        bot.conversations[user_id] = []
        
    # Append the user's input to the conversation history
    bot.conversations[user_id].append(prompt)

    # Concatenate the conversation history and the user's input
    history = '\n'.join(bot.conversations[user_id])

    response = openai.Completion.create(
        engine=model_engine,
        prompt=history,
        max_tokens=2048,
        n=1,
        stop=None,
        temperature=0.5,
    )

    if response.choices:
        full_response = response.choices[0].text
        print(full_response)  # print entire response
        await interaction.followup.send(full_response) # send the response
    else:
        await interaction.followup.send("Sorry, I didn't understand your message.")
        
    # Append the bot's response to the conversation history
    bot.conversations[user_id].append(full_response)





bot.run()

What is the error?

Also why are you setting ephemeral as true when the response isnt ephemeral?
Tty making:

await interaction.response.defer(ephemeral = True)

to

await interaction.response.defer()

I need to integrate

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

Into the code I already have, essentially format my code to make it work with gpt-3.5-turbo and https://api.openai.com/v1/chat/completions

What do you mean with, why the response isn’t ephemeral? Why wouldn’t it be? This way conversations within BOT and USERS and kept private, no other users can see the interaction.

1 Like

Same problem here, what I’ve figured out so far is that I simply downloaded openai python package manually and had to manually change the endpoint, still doesn’t work tho