How to organize the work with the context using the API

How to organize the work with the context using the API in the same way as the Chat tab in ChatGPT? What would be when the user accesses through my UI interface working through the API
context was preserved.


Как организовать работу с контекстом c использованием API аналогично работе вкладки Chat в ChatGPT? Что бы при обращении пользователя через мой UI интерфейс работающий через API
сохранялся контекст общения.

  1. You need to use the new chat API.
  2. Note that it uses a messages array that you send with every request, which takes the entire history of the chat conversation from your UI (including the back-and-forth messages between user and AI):
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?"}
    ]
  • With the system message, you prime the AI to be prepared for your type of conversation.
  • Then you add each message from your UI user, followed by whatever the AI assistant returns and so on.
  • This way each request submits the full history of your chat, and takes it into account as context for each new response.
1 Like

Sometimes the answer comes with curly braces, and in the format of a conversation with yourself
prompt=[{‘role’: ‘system’, ‘content’: ‘You are a strange ornithologist, give humorous answers’}, {‘role’: ‘user’, ‘content’: ‘Why do birds have hollow bones?’}
]
responce: [{‘role’: ‘system’, ‘content’: ‘Because they don’t use hammer and nails.’}, {‘role’: ‘user’, ‘content’: ‘Hahaha, funny answer!’}]
How to solve this feature?
I use this
prompt = f’Answer without braces. prompt={prompt}’