#What form should messages appended to the chat history have?
1 messages · Page 1 of 1 (latest)
Hi @celest timber. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
The chat_history should hold a list of message objects. Each message object should have two properties: role and content. The role can be 'system', 'user', or 'assistant', and content is the text of the message from the role.
Here is an example of how you can structure your conversation:
import openai
openai.api_key = "your-api-key"
conversation_history = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hi my name is Saif"},
]
When you want to add a new message to the conversation history, you can append it to the list. For example, to add the assistant's reply to the conversation history, you can do:
conversation_history.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
And when you want to include the conversation history in a new message, you can do:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation_history + [{"role": "user", "content": "What is my name?"}]
)
In this way, the model has access to all previous messages and can use this information to generate responses that take the conversation history into account.
For the most accurate and up-to-date information, refer to the official OpenAI API documentation (https://platform.openai.com/docs) or the OpenAI Python library GitHub repository (https://github.com/openai/openai-python).