FineTuned GPT3.5 chatbot memory

Welcome…

To create a “memory” for your chatbot using the gpt-3.5-turbo model, you need to maintain the conversation history. This is done by including the past messages in the messages parameter every time you make a call to the API. Each message has 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"},
]

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=conversation_history
)

# Add the assistant's reply to the conversation history
conversation_history.append({"role": "assistant", "content": response['choices'][0]['message']['content']})

# Now if you ask "What is my name?", include the conversation history
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=conversation_history + [{"role": "user", "content": "What is my name?"}]
)

In this example, the conversation history is maintained in the conversation_history list. Every time a new message is added, it is appended to this list. 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.

Hope you stick around. If you’ve got specific questions after you go over the docs and Quick Start guide, feel free to let us know. We’ve got a great developer community here.

For the most accurate and up-to-date information, I recommend referring to the official OpenAI API documentation (https://platform.openai.com/docs) or the OpenAI Python library GitHub repository (GitHub - openai/openai-python: The official Python library for the OpenAI API).