I'm trying to create a Tkinter chat assistant that uses the Claude 3 API from Anthropic to generate responses. However, I'm encountering errors when running the code. I've tried to fix the issues based on suggestions, but I'm still facing problems. Can someone help me identify and resolve the errors?
import tkinter as tk
from tkinter import scrolledtext
import requests
import anthropic
API_KEY = "Not sharing my API KEY"
API_URL = "https://api.anthropic.com/v1/complete"
def get_response():
user_input = user_input_box.get()
chat_history.configure(state='normal')
chat_history.insert(tk.END, "You: " + user_input + "\n")
chat_history.configure(state='enabled')
user_input_box.delete(0, tk.END)
headers = {
"Content-Type": "application/json",
"X-API-Key": API_KEY
}
data = {
"prompt": f"{user_input}\n\nAssistant:",
"max_tokens_to_sample": 100,
"model": "claude-v1"
}
try:
response = requests.post(API_URL, headers=headers, json=data)
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
result = response.json()
claude_response = result["completion"].strip()
chat_history.configure(state='normal')
chat_history.insert(tk.END, "Claude 3: " + claude_response + "\n\n")
chat_history.configure(state='enbaled')
except requests.exceptions.RequestException as e:
chat_history.configure(state='normal')
chat_history.insert(tk.END, "Error: Unable to get response from Claude 3. Please check your API key and network connection.\n\n")
chat_history.configure(state='enabled')
print("Error:", e)
root = tk.Tk()
root.title("Claude 3 Chat Assistant")
chat_history = scrolledtext.ScrolledText(root, state='normal', width=70, height=20)
chat_history.grid(row=0, column=0, columnspan=2)
user_input_box = tk.Entry(root, width=50)
user_input_box.grid(row=1, column=0)
send_button = tk.Button(root, text="Send", command=get_response)
send_button.grid(row=1, column=1)
root.mainloop()`