Skip to content

Instantly share code, notes, and snippets.

@ThomasVitale
Created May 2, 2025 21:13
Show Gist options
  • Save ThomasVitale/04bf6320f4cf273e396e5108121b4a34 to your computer and use it in GitHub Desktop.
Save ThomasVitale/04bf6320f4cf273e396e5108121b4a34 to your computer and use it in GitHub Desktop.
JDBC Memory with Spring Security Conversation ID
@Configuration
public class AiConfig {
@Bean
ChatMemory jdbcChatMemory(JdbcChatMemoryRepository chatMemoryRepository) {
return MessageWindowChatMemory.builder()
.chatMemoryRepository(chatMemoryRepository)
.build();
}
}
@RestController
class MemoryControllerAdvisorJdbc {
private final ChatClient chatClient;
MemoryControllerAdvisorVectorStore(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
this.chatClient = chatClientBuilder.clone()
.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))
.build();
}
@PostMapping("/memory/jdbc/{conversationId}")
String chat(@RequestBody String question) {
String conversationId = SecurityContextHolder.getContext().getAuthentication().getName();
return chatClient.prompt()
.user(question)
.advisors(a -> a.param(
AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,
))
.call()
.content();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment