I'm getting 404 when trying to use chatGPT turbo... why?

I’m getting 404 when trying to use chatGPT turbo… why?

that’s a python script I’m trying to run:
import requests

api_key = “sk-proj-myapikeyhere”
headers = {
“Authorization”: f"Bearer {api_key}"
}

def generate_random_sentence():
prompt = “Generate a random sentence.”
data = {
“model”: “gpt-4-turbo”,
“prompt”: prompt,
“temperature”: 0.7,
“max_tokens”: 60
}
#removed url becaue I connat include links on the post
response = requests.post(“api_openai_com-v1-chat-completions”, headers=headers, json=data)

if response.status_code == 200:
    response_json = response.json()
    if 'choices' in response_json and response_json['choices']:
        return response_json['choices'][0].get('text', '').strip()
    else:
        return "Error: 'choices' key not found or empty in response."
else:
    return f"Error: API request failed with status code {response.status_code}."

print(generate_random_sentence())

Please use the client library. It handles all of the heavy lifting for you so these errors don’t pop up.

Even with using the client library you will run into an issue of this type of code working. You are using the structure for completions and not chat completions

2 Likes

could you give me an example of how to use it properly? I already changed the url for only completions :: yet as you wrote the code is not working beacause of the script I’m using