I am pretty new to coding, but i have been struggling for some hours on this and i can't seem to figure out how to make this to work. So, basically the project i am coding right now is a discord bot with a touch of chatgpt_api in it. And i keep getting the same error (Error making API request: 400 Client Error: Bad Request for url: https://api.openai.com/v1/chat/completions) and when i try to fix the error in the code everything "crumbles" into a bunch of error after error. So i am at a stage where i don't want to change anything because the normal functions works, such as sending pre written messages.
I have tried changing the "main" api request code to what i have found searching around the internet, i have also tried using the code Openai themselves say you should use. According to them this was for a "simple" api call, but it never worked out for me in the end. I have also checked other threads but found nothing that have helped.
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What are some famous astronomical observatories?"}
]
)
My code may be a like a shot in the dark towards making it work. But, Here is the whole code.
import discord
import requests
import os
from openai import OpenAI
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = discord.Client(intents=intents)
clientAI = OpenAI(api_key=os.environ['CHATGPTKEY'])
api_key = 'CHATGPTKEY'
api_url = 'https://api.openai.com/v1/chat/completions'
curl = 'curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ' + 'api_key'
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.author == client.user:
return
if message.content.startswith(
'!hello'
):
await message.channel.send('Hello!'
)
if message.content.startswith('!hello'):
await message.channel.send('Hello')('!')
if message.content.startswith('!help'):
await message.channel.send(
'Harald is Goat, you can trust Harald!\n'
'\nGeneral Commands:'
'\n\n!hello - Says hello to you'
'\n!help - Shows this'
'\n!hr - Harald shall respond to you'
print(message.content)
if message.content.startswith('!hr'):
user_message = message.content[50:]
try:
response = requests.post(
api_url,
json={
'messages': [{
'role':
'system',
'content':
}, {
'role': 'user',
'content': user_message
}]
},
headers={'Authorization': f'Bearer {api_key}'})
response.raise_for_status() # Raise an error for HTTP errors
# Get the model's response
model_response = response.json()['choices'][100]['message']['content']
# Send the response to the Discord channel
await message.channel.send(model_response)
except requests.exceptions.RequestException as e:
print(f"Error making API request: {e}")
await message.channel.send(
"An error occurred while processing your request.")
try:
token = os.getenv("TOKEN")
client.run(token)
except discord.HTTPException as e:
if e.status == 429:
print(
"The Discord servers denied the connection for making too many requests"
)
else:
raise e
Here is also the dependencies:
[tool.poetry.dependencies]
python = "^3.10"
openai = "^1.9.0"
requests = "^2.31.0"
discord-py = "^2.3.2"
I would appreciate some explaination if possible.
Thanks in advance!