GPT-3.5-turbo how to remember previous messages like Chat-GPT website

go to this link, OpenAI API
look a the example

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

what you need to do in your app is add to the messages array in each interaction, for example:
when a user Joins the chat and says hello

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
    ]
)

When OpenAI replies, you add that to your messages array

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello, how can I help you?"},
    ]
)

when the user enters more text you send the whole array back to the server

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello, how can I help you?"},
        {"role": "user", "content": "who is more stylish Pikachu or Neo"},
    ]
)

then you keep adding each interaction to the messages array as you send it back to the server.

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello, how can I help you"},
        {"role": "user", "content": "who is more stylish Pikachu or Neo"},
        {"role": "assistant", "content": "Well Neo of course"},
    ]
)

and continue

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello, how can I help you"},
        {"role": "user", "content": "What is more stylish Pikachu or Neo"},
        {"role": "assistant", "content": "Well Neo of course"},
        {"role": "user", "content": "Why?"},
    ]
)

when the user says “Why?” the only way for ChatGPT to know what you are talking about is by sending the entire conversation, this is how it gets context.

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello, how can I help you"},
        {"role": "user", "content": "What is more stylish Pikachu or Neo"},
        {"role": "assistant", "content": "Well Neo of course"},
        {"role": "user", "content": "Why?"},
        {"role": "assistant", "content": "Well Pikachu is naked! there is no style in that"},
    ]
)

In your application, you need to have a data structure to represent conversations and that data structure needs to be able to accept an array of messages, and be able to be updated with new messages coming from either the user or the server, then whenever the user updates the data structure you send it to the server whenever you get a reply from the server you update the data structure and present it in the UI.

7 Likes