Building conversations
1. Building conversations
So far, we've explored how to generate single responses. We'll now look at how to create a model that remembers previous interactions and maintains context.2. Maintaining context
Imagine an AI assistant developed for a travel booking company that helps customers plan vacations. A user could start by asking about popular destinations in France3. Maintaining context
, then, based on the AI-generated response, follow up with questions4. Maintaining context
about more destinations, flights, hotels, and local attractions. Instead of treating each query as a separate request, the model should5. Maintaining context
remember what was previously discussed and use6. Maintaining context
that information in its responses. This means we need a structured way to track conversation history and maintain a consistent dialog, which can be done with Llama by implementing a conversation class.7. Conversation class
Through this class we'll organize the code so that it can store a history of prior messages, and new messages will be added to the list as the conversation with Llama continues. The class starts with a method called 'init'. This is the constructor method that runs every time we create a new instance of the class. It allows us to initialize attributes: in this case, we store an instance of the Llama model and setting up a system prompt. This defines how the assistant should behave throughout the conversation. The system message is stored at the start of the history list.8. Conversation class
Next, we define create_completion(), which: Adds the user input to the conversation history; calls create_chat_completion() to generate a response; and finally, extracts the assistant's reply and adds it to the conversation history. With this setup, every new input builds on previous exchanges, and this helps to ensure a more continuous conversation rather than a series of separate responses.9. Running a multi-turn conversation
Let's have a look at how to interact with our conversation class. First, we load our model and initialize the class with a system prompt, such as asking to act as a virtual travel assistant helping with planning trips. We then pass a prompt to the create_completion method, asking for some destinations in France for a short weekend break. Llama returns some options, and we follow up with another question about destinations in Spain.10. Running a multi-turn conversation
Because conversation history is tracked, Llama understands the reference to the weekend11. Running a multi-turn conversation
break and provides a relevant response. Multi-turn conversations make interactions feel more natural;12. Let's practice!
let's see some in action.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.