teasdasdas
unknown
python
3 years ago
2.5 kB
9
Indexable
def handle_input(event): # Get the user's input from the text entry box user_input_str = user_input.get() # Check if the api_key is empty if api_key == "": # Display an error message conversation_history_text.insert(END, "Error: No API key is set\n") settings_button_click() return # Set up the proxy proxy_url = "zproxy.lum-superproxy.io:22225" proxy_user = "brd-customer-hl_26032ed2-zone-residential:ekf7ia0gs20p" proxies = { "http": f"http://{proxy_user}@{proxy_url}", "https": f"https://{proxy_user}@{proxy_url}" } # Set up the request to the OpenAI API url = "https://api.openai.com/v1/completions" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "engine": engine, "prompt": user_input_str, "temperature": float(temp), "max_tokens": int(tokens), "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0 } # Send the request and get the response response = requests.get(url, headers=headers, json=data) # Check if the request was successful if response.status_code != 200: # Handle the error conversation_history_text.insert(END, "Error: request to OpenAI API failed\n") return # Get the response data response_data = response.json() # Check if the response data contains a 'choices' key if "choices" not in response_data: # Handle the error conversation_history_text.insert(END, "Error: Could not get response from OpenAI API") print(response_data) return # Get the response text from the 'choices' key in the response data response_text = response_data["choices"][0]["text"] # Add the user input and response to the conversation history conversation_history.append((user_input_str, response_text)) # Clear the text area conversation_history_text.delete("1.0", END) # Print the conversation history in the text area for conversation in conversation_history: conversation_history_text.insert(END, conversation[0] + "\n", "user") conversation_history_text.insert(END, conversation[1] + "\n", "assistant") # Clear the text entry box user_input.delete(0, END) Edit the code so it can work with the latest openai API docs
Editor is loading...