Yeah, I have given up on that. Here’s what i have so far: ```import openai
import os
Set up the OpenAI API key
openai.api_key = os.getenv('OPENAI_API_KEY')
Initialize the conversation history
conversation_history = ""
Define a function to generate a response using the GPT-3 API
def generate_response(prompt, conversation_history):
# Combine the conversation history and the new prompt
full_prompt = conversation_history + "\n" + prompt
parameters = {
"model": "text-davinci-002",
"prompt": full_prompt,
"temperature": 0.5,
"max_tokens": 60,
"n": 1,
"stop": "\n"
}
response = openai.Completion.create(**parameters)
response_text = response.choices[0].text.strip()
# Update the conversation history
new_conversation_history = conversation_history + "\n" + prompt + "\n" + response_text
# Limit the conversation history length using a rolling window
max_history_lines = 20
lines = new_conversation_history.split("\n")
if len(lines) > max_history_lines:
lines = lines[-max_history_lines:]
new_conversation_history = "\n".join(lines)
return response_text, new_conversation_history
Loop indefinitely
while True:
try:
# Prompt the user for input
user_input = input("> ")
# Exit the chatbot if the user types 'quit'
if user_input.lower() == 'quit':
print("Goodbye!")
break
# Generate a response using the previous conversation history
response, conversation_history = generate_response(user_input, conversation_history)
# Display the response to the user
print(response)
except openai.OpenAIError as e:
# Handle any OpenAI API errors that occur during execution
print(f"OpenAI API error: {e}")
except Exception as e:
# Handle any other unexpected errors that occur during execution
print(f"Error: {e}")