Memory for your AI
LLMs are stateless. They don't remember previous messages unless you send the whole history back with every new request.
How to Manage Context:
- Maintain an array of messages:
[{ role: 'user', content: 'Hi' }, { role: 'assistant', content: 'Hello!' }]. - When the user sends a new message, push it to the array.
- Send the entire array to the API.
We will explore how to manage this state efficiently in React using useState or in Vanilla JS with global variables.