Hi folks,
my goal is to code a programm with a GUI which has 2 APIs
- To chatgpt
- 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