π§ AI with Java & Spring Boot β Part 3: Building a Memory-Aware Chatbot with LangChain4j
Hey devs! π
Welcome back to the third part of our AI-with-Java journey. So far, we've:
Today, weβll go even further β and introduce LangChain4j, the Java adaptation of LangChain, to create a chatbot with memory and context.
Imagine asking your bot:
"Whoβs the CEO of Tesla?"
"What year was he born?"
And the bot remembers who βheβ refers to. Thatβs what weβre building today. π§
π§° Tools Weβll Use
- LangChain4j β Java library for building LLM apps
- Spring Boot
- OpenAI API
- Session Memory
π§ Step-by-Step: AI Chatbot with Memory (LangChain4j + Spring Boot)
1. Add Dependencies (Maven)
Add this to your pom.xml
:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>0.25.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-openai</artifactId>
<version>0.25.0</version>
</dependency>
2. Configure OpenAI
In application.yml
:
openai:
api-key: YOUR_OPENAI_API_KEY
model: gpt-3.5-turbo
3. Create the ChatMemoryService
@Component
@Scope("session") // memory per user session
public class MemoryChatService {
private final ChatLanguageModel model;
private final ChatMemory memory;
public MemoryChatService(@Value("${openai.api-key}") String apiKey) {
this.model = OpenAiChatModel.builder()
.apiKey(apiKey)
.modelName("gpt-3.5-turbo")
.temperature(0.7)
.build();
this.memory = MessageWindowChatMemory.withMaxMessages(10);
}
public String chat(String userInput) {
ChatLanguageModelChain chain = new ChatLanguageModelChain(model, memory);
return chain.execute(userInput);
}
}
4. Expose It via REST Controller
@RestController
@RequestMapping("/api/langchain")
@SessionAttributes("memoryChatService")
public class LangChainController {
private final MemoryChatService memoryChatService;
public LangChainController(MemoryChatService memoryChatService) {
this.memoryChatService = memoryChatService;
}
@PostMapping("/chat")
public ResponseEntity<String> chat(@RequestBody Map<String, String> request) {
String prompt = request.get("prompt");
String response = memoryChatService.chat(prompt);
return ResponseEntity.ok(response);
}
}
5. Try It Out π
Make a POST request:
curl -X POST http://localhost:8080/api/langchain/chat \
-H "Content-Type: application/json" \
-d '{"prompt": "Who is the CEO of Google?"}'
Then ask:
curl -X POST http://localhost:8080/api/langchain/chat \
-H "Content-Type: application/json" \
-d '{"prompt": "Where was he born?"}'
Itβll remember!
π Wrapping Up
Now you have an intelligent chatbot that remembers your conversation! π
You can also:
- Store memory to DB or Redis
- Customize personas
- Add tools like calculators or file readers (LangChain4j supports them too)
π Coming in Part 4...
- File-based Q&A
- Upload PDFs and ask questions
- Embedding + vector search (e.g., using ChromaDB or Pinecone with Java!)
If you're enjoying the series, make sure to:
β€οΈ React
π¨οΈ Comment
π Follow for Part 4!
Stay sharp!
β RF π¨βπ»
Top comments (0)