This second exercise will focus on the conversational memory. We will improve our LLM client to make it able to store conversation history and pass it as context in LLM query.
Modify the LLMService class.
Add List<Message> attribute called memory and instantiate it in the constructor
In the getResponse method, add question wrapped in UserMessage object to memory before the return statement.
Remove question object from prompt() builder method and call messages() builder method to give the memory object.
import org.springframework.ai.chat.messages.UserMessage;
private Stream<String> getResponse(final String question) {
memory.add(new UserMessage(question));
return chatClient.prompt()
.messages(memory)
...Create a new private method that will append the response (passed as argument), wrapped in AssistantMessage to memory and return the response object.
import org.springframework.ai.chat.messages.AssistantMessage;
private String appendToHistory(String response) {
memory.add(new AssistantMessage(response));
return response;
}In the getResponse method, modify the return statement to append the response content to the conversation memory by using appendToHistory method on the initially returned stream.
private Stream<String> getResponse(final String question) {
...
.stream()
.content()
.toStream()
.map(this::appendToHistory);
}If needed, the solution can be checked in the solution/exercise-2 folder.
- Make sure that ollama container is running
- Run the application
- In the application prompt, type
llm Give me 8 famous dishes from Japan - Check the response
- Try a new query
llm Classify them into categories with meat and without meat - Is the response better now ? (We hope so! ;)
In this exercise, we added conversation history feature to our LLM client making it LLM able to follow a conversation. Here are some key points:
- LLM handle different types of messages called "roles" (System, User, Assistant)
- Conversation history can be provided as context in query
- Token volume in limited for LLM input and there is a subject about how to compress history
- Conversation history is the first step to use LLM with step-by-step reflexion approach as few shots prompting or CoT, ToT prompting that consists in splitting reflexion and chaining multiple queries
- Spring AI provides some advisors classes to manage conversation history automatically (see bonus 1)
- Model Context Protocol (MCP) can be used to optimize conversation history management and solve token volume problem
Let's add some document content as context in the next exercise!