This is a Python script that creates a basic GUI interface for an OpenAI GPT-3 chatbot. It uses the tkinter library for creating the GUI and the OpenAI API for generating responses.
The code starts by importing the necessary libraries - tkinter and openai. Then it sets the OpenAI API key by assigning a string value to the openai.api_key variable.
The generate_response() function is defined, which takes a prompt as input and generates a response using OpenAI's GPT-3 language model. The function creates a completion request using the openai.Completion.create() method, specifying the engine to use, the prompt, the maximum number of tokens to generate, the number of completions to return, and the temperature. The function returns the generated message as a string.
The display_response() function is defined next, which is called when the Submit button is clicked in the GUI. The function retrieves the input text from the input field, generates a response using the generate_response() function, and displays the response in the output field.
The main GUI window is created using the tk.Tk() method and titled "OpenAI Chatbot". It has a size of 600x700 pixels. An input field, submit button, and output field are created using the tk.Entry(), tk.Button(), and tk.Text() methods, respectively. The input field is for the user to enter their message, the submit button is for submitting the message and generating a response, and the output field is for displaying the response. The output field is initially disabled so that the user cannot edit its contents.
Finally, the GUI event loop is started using the root.mainloop() method, which waits for user input and responds accordingly until the window is closed
Here is the code:
import tkinter as tk
import openai
# Apply the API Key
openai.api_key = "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"
# Generate a response using OpenAI GPT-3
def generate_response(prompt):
completions = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
message = completions.choices[0].text
return message
# GUI interface
def display_response():
input_text = input_field.get()
response = generate_response(input_text)
output_field.config(state='normal')
output_field.delete(1.0, tk.END)
output_field.insert(tk.END, response)
output_field.config(state='disabled')
# Create the main window
root = tk.Tk()
root.title("OpenAI Chatbot")
root.geometry("600x700")
# Create the input field
input_field = tk.Entry(root, font=("Times New Roman", 14))
input_field.pack(pady=10)
# Create the submit button
submit_button = tk.Button(root, text="Submit", font=("Times New Roman", 14), command=display_response)
submit_button.pack(pady=10)
# Create the output field
output_field = tk.Text(root, font=("Times New Roman", 14), state='disabled')
output_field.pack(pady=10)
# Start the GUI event loop
root.mainloop()
0 Comments