ChatGPT source code in Python [Update 2024]
![ChatGPT source code in Python [Update 2024]](/_next/image?url=https%3A%2F%2Fpub-6260987aa5e84a04b470576604a2f319.r2.dev%2Fblog-images%2F1791291504211-Chat-Gtp-source-code-in-python-1.jpg&w=1920&q=75)
Apply the API Key
openai.api_key = "ENTER_YOUR_API_KEY"
message = completions.choices[0].text
return message</pre></div>
Apply the API Key
openai.api_key = "YOUR_API_KEY"
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=("Arial", 14)) input_field.pack(pady=10)
Create the submit button
submit_button = tk.Button(root, text="Submit", font=("Arial", 14), command=display_response) submit_button.pack(pady=10)
Create the output field
output_field = tk.Text(root, font=("Arial", 14), state='disabled') output_field.pack(pady=10)
Start the GUI event loop
root.mainloop()
Need help with your project?
Talk to the Papaya Coders team for a free consultation.