-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
27 lines (22 loc) · 842 Bytes
/
chat.py
File metadata and controls
27 lines (22 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from openai import OpenAI
from tkinter import messagebox
import os
import tkinter as tk
# OpenAI API Client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def ask_chat_gpt(question, response_box):
"""Send the question to ChatGPT and display the response in the sidebar."""
try:
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": question}
]
)
response = completion.choices[0].message.content
# Return response for storage
return response
except Exception as e:
messagebox.showerror("Error", f"Failed to get response from ChatGPT: {str(e)}")
return "Error retrieving response."