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.
More Related questions...