AI / LangChain4j interview questions
How do you implement multi-turn conversation with memory per user in a Spring REST API using LangChain4j?
Implementing per-user conversational memory in a Spring REST API requires three things: an AI Services interface with a memory-id parameter, a ChatMemoryProvider that returns isolated memory per ID, and a backing store to persist conversations across requests (or restarts).
// 1. AI Services interface with per-user memory
interface ChatAssistant {
String chat(@MemoryId String userId, @UserMessage String message);
}
// 2. In-memory store for development (switch to Redis/DB for production)
Map<String, ChatMemory> memoryMap = new ConcurrentHashMap<>();
ChatMemoryProvider memoryProvider = memoryId ->
memoryMap.computeIfAbsent(memoryId.toString(), id ->
MessageWindowChatMemory.withMaxMessages(20));
// 3. Build the AI Service with the provider
ChatAssistant assistant = AiServices.builder(ChatAssistant.class)
.chatLanguageModel(model)
.chatMemoryProvider(memoryProvider)
.build();
// 4. Spring REST controller
@RestController
@RequestMapping("/api/chat")
class ChatController {
private final ChatAssistant assistant;
ChatController(ChatAssistant assistant) {
this.assistant = assistant;
}
@PostMapping("/{userId}")
String chat(@PathVariable String userId, @RequestBody String message) {
return assistant.chat(userId, message); // each user gets isolated memory
}
}The @MemoryId annotation tells LangChain4j which parameter is the memory key. The ChatMemoryProvider lambda receives this key and returns the appropriate memory store for that user. For production, replace the ConcurrentHashMap with a Redis-backed or JDBC-backed memory store so conversations survive application restarts and work across multiple pods.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
