When building AI-powered chatbots or assistants using LangChain4j, developers often wonder why they need to provide multiple messages instead of just the latest user query. This post explains the rationale, the stateless nature of LLMs, and how LangChain4j handles multi-turn conversations.
LLMs are powerful, but they're forgetful.
When you use LangChain4j to build a chatbot, it's important to understand that Language Models are stateless. That means they don’t remember what was said earlier unless you explicitly provide that context.
Consider this simple conversation:
User: Hello, my name is Krishna AI: Hi Krishna, how can I help you? User: What is my name? AI: Krishna
To get this working correctly, you must pass all previous messages when invoking the chat() method in LangChain4j. Here's what that looks like:
// 1st user message UserMessage firstUserMessage = UserMessage.from("Hello, my name is Krishna"); AiMessage firstAiMessage = chatModel.chat(firstUserMessage).aiMessage(); System.out.println("AI: " + firstAiMessage.text()); // 2nd user message UserMessage secondUserMessage = UserMessage.from("What is my name?"); AiMessage secondAiMessage = chatModel.chat(firstUserMessage, firstAiMessage, secondUserMessage).aiMessage(); System.out.println("AI: " + secondAiMessage.text());
As you can see, the second call includes the entire chat history so far.
Manually tracking this can get messy fast. That's why LangChain4j introduces ChatMemory, a helpful abstraction to automatically manage conversation state, but more on that I will explain in the later posts. If you’re building multi-turn bots or assistants, understanding this pattern is very important.
Find the below working application.
ChatConversation.java
package com.sample.app.chatmodels; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.data.message.UserMessage; import dev.langchain4j.model.chat.ChatModel; import dev.langchain4j.model.ollama.OllamaChatModel; public class ChatConversation { public static void main(String[] args) { // Create the Ollama language model ChatModel chatModel = OllamaChatModel.builder().baseUrl("http://localhost:11434") // Make sure Ollama is running .modelName("llama3.2").build(); // 1st user message UserMessage firstUserMessage = UserMessage.from("Hello, my name is Krishna"); AiMessage firstAiMessage = chatModel.chat(firstUserMessage).aiMessage(); System.out.println("AI: " + firstAiMessage.text()); // 2nd user message UserMessage secondUserMessage = UserMessage.from("What is my name?"); AiMessage secondAiMessage = chatModel.chat(firstUserMessage, firstAiMessage, secondUserMessage).aiMessage(); System.out.println("AI: " + secondAiMessage.text()); } }
Output
AI: Namaste Krishna! It's lovely to meet you. Is there something I can help you with or would you like to chat for a bit? AI: I remember! Your name is Krishna. You mentioned it earlier.
Previous Next Home
No comments:
Post a Comment