Status/eorrer code 400, Newbie could need a hand

Hi folks,

my goal is to code a programm with a GUI which has 2 APIs

  1. To chatgpt
  2. to a ticket system

Maingoal is: Bot should answer the tickets and i only want to check in the gui if the answer is acceptable

In the first step i tried to create a GUI with ask button and receive the anwer from the bot.

The problem is, what ever i typ in, i always get the Error Code 400.
I tested my apikey and it is fine.

I cant imagin what the problem is, but im a lvl1 noob in python.
Maybe so can help me out where the problem is.
here is my code

import tkinter as tk
import requests

class GUI:
    def __init__(self, master):
        self.master = master
        master.title("OpenAI Chatbot")

        self.question_label = tk.Label(master, text="Frage:")
        self.question_label.pack()

        self.question_entry = tk.Entry(master)
        self.question_entry.pack()

        self.ask_button = tk.Button(master, text="Ask", command=self.ask)
        self.ask_button.pack()

        self.close_button = tk.Button(master, text="Close", command=self.close)
        self.close_button.pack()

        self.answer_label = tk.Label(master, text="Antwort:")
        self.answer_label.pack()

        self.answer_text = tk.Text(master, height=10, width=30)
        self.answer_text.pack()

    def ask(self):
        question = self.question_entry.get()
        API_KEY = "sk-XXXAPIKEY"
        ORG_ID = "org-is this relevant?"

        response = requests.post("https://api.openai.com/v1/engines/davinci/jobs",
                                 headers={"Authorization": f"Bearer {API_KEY}"},
                                 json={
                                        "model": "text-davinci-003",
                                        "prompt": question,
                                        "temperature": 0,
                                        "max_tokens": 100,
                                        "top_p": 1,
                                        "frequency_penalty": 0,
                                        "presence_penalty": 0,
                                        "stop": ["\n"]
})

        if response.status_code == 200:
            response_json = response.json()
            if "choices" in response_json:
                answer = response_json["choices"][0]["text"]
                self.answer_text.delete(1.0, tk.END)
                self.answer_text.insert(tk.END, answer)
            else:
                self.answer_text.delete(1.0, tk.END)
                self.answer_text.insert(tk.END, "Keine gültige Antwort von der API erhalten")
        else:
            self.answer_text.delete(1.0, tk.END)
            self.answer_text.insert(tk.END, f"Fehler bei der API-Anfrage. Statuscode: {response.status_code}")

    def close(self):
        self.master.quit()

root = tk.Tk()
app = GUI(root)
root.mainloop()

Summary

This text will be hidden

Had you checked this API documentaion?

Maybe just change the following POST request to
https://api.openai.com/v1/completions

hey thx for your reply.
But it found my mistake.
I try to learn coding with chatgpt so i used it.
The problem is that chatgpt limited knowledgebase with the end 21. I needed to give him the new information/tutorials of the actual state of the art of the api, and it immediatly worked.
This is my code now

Sry for the german :slight_smile:

import tkinter as tk
import openai

# Hauptklasse für die GUI
class GUI:
    # Konstruktor
    def __init__(self, master):
        # Hauptfenster
        self.master = master
        master.title("OpenAI Chatbot")
        # Größe des Fensters festlegen
        master.geometry("600x400")


        # Frage-Label
        self.question_label = tk.Label(master, text="Frage:")
        self.question_label.pack()

        # Frage-Eingabefeld
        self.question_entry = tk.Entry(master, width=80)
        self.question_entry.pack()

        # Modell-Label
        self.model_label = tk.Label(master, text="Modell:")
        self.model_label.pack()

        # Modell-Auswahlfeld
        self.model_var = tk.StringVar(value="text-davinci-003")
        self.model_dropdown = tk.OptionMenu(master, self.model_var, *self.get_models())
        self.model_dropdown.pack()

        # Frage-Button
        self.ask_button = tk.Button(master, text="Frage stellen", command=self.ask)
        self.ask_button.pack()

        # Schließen-Button
        self.close_button = tk.Button(master, text="Schließen", command=self.close)
        self.close_button.pack()

        # Antwort-Label
        self.answer_label = tk.Label(master, text="Antwort:")
        self.answer_label.pack()

        # Antwort-Textfeld
        self.answer_text = tk.Text(master, height=10, wrap='word', width=50)
        self.answer_text.pack()

    # Frage-Methode
    def ask(self):
        # Frage aus dem Eingabefeld holen
        question = self.question_entry.get()
        # Modell aus der Auswahl holen
        model = self.model_var.get()

        openai.api_key = "sk-XXX"
        completions = openai.Completion.create(
            engine=model,
            prompt="beantworte folgende frage aus der sicht eines servicedeskmitarbeiters mit dem namen Sanny auf deutsch und sehr höflich, wichtig ist das der User sich respektiert fühlt und er am besten selber die lösung umsetzen kann. verwende vorzugsweise listen bzw anleitungen" + question,
            max_tokens=500,
            n=1,
            stop=None,
            temperature=0,
        )

        # API-Antwort verarbeiten
        if completions.choices[0].text:
            answer = completions.choices[0].text
            self.answer_text.delete(1.0, tk.END)
            self.answer_text.insert(tk.END, answer)
        else:
            self.answer_text.delete(1.0, tk.END)
            self.answer_text.insert(tk.END, "Keine gültige Antwort von der API erhalten")

    # Schließen-Methode
    def close(self):
        self.master.quit()

    # Modell-Liste abrufen-Methode
    def get_models(self):
        openai.api_key = "sk-XXX"
        models = openai.Model.list()
        model_ids = [model.id for model in models['data']]
        return model_ids


root = tk.Tk()
app = GUI(root)
root.mainloop()