I’ve been experimenting with the OpenAI API and I’m trying to figure out how to keep a conversation going. Here’s what I’ve done so far:
import openai
openai.api_key = 'my_secret_key'
initial_prompt = 'Create a limerick'
response = openai.Completion.create(
engine='text-davinci-002',
prompt=initial_prompt,
max_tokens=60
)
print(response.choices[0].text)
This works fine for a single request. But how can I ask for another limerick without losing context? In the OpenAI playground and ChatGPT, the AI remembers previous exchanges. Is there a way to do this in my Python script?
I noticed the response includes an ‘id’. Can I use this somehow to keep the conversation flowing? Any tips on maintaining context when making multiple API calls would be really helpful. Thanks!
To maintain conversation context with the OpenAI API, you’ll need to use the ChatCompletion endpoint instead of Completion. This allows for stateful interactions. Here’s how you can modify your code:
import openai
openai.api_key = 'your_api_key'
messages = [
{'role': 'system', 'content': 'You are a limerick generator.'},
{'role': 'user', 'content': 'Create a limerick'}
]
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages
)
print(response['choices'][0]['message']['content'])
# To continue the conversation, append new messages and make another API call
messages.append({'role': 'user', 'content': 'Another limerick, please'})
# Make another API call with the updated messages list
This approach allows you to maintain context across multiple interactions. Each new exchange is simply appended to the messages list before making the next API call.
I’ve been working with the OpenAI API for a while now, and I can share some insights on maintaining conversation context. The key is to use the ChatCompletion API instead of the older Completion API. Here’s what I’ve found works well:
Start by creating a list of messages, each as a dictionary with ‘role’ and ‘content’ keys. Include a system message to set the initial context or behavior. Then, add user and assistant messages for each exchange. When you want to continue the conversation, just append new messages to the list and send the entire history with each API call.
This approach has worked great for me in maintaining context across multiple interactions. It’s more similar to how ChatGPT works, allowing for more natural, context-aware conversations. One tip: Be mindful of token limits, and if your conversation gets too long, you might need to truncate older messages to stay within the model’s context window.
hey isaac, ive been messin with this too. u can use the ‘messages’ parameter in ChatCompletion.create() to keep context. just add each exchange as a dict with ‘role’ and ‘content’. somthin like:
messages=[
{‘role’: ‘system’, ‘content’: ‘You write limericks.’},
{‘role’: ‘user’, ‘content’: ‘Create a limerick’},
{‘role’: ‘assistant’, ‘content’: ‘first limerick here’},
{‘role’: ‘user’, ‘content’: ‘Another one’}
]
hope that helps!