Commit e461af67 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

prosektikos maxMessages

parent 5c382cc7
Loading
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
package org.etsi.osl.mcp.backend;

import java.util.Map;

import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.SessionScope;

@RestController
@SessionScope
public class ChatController {
    private static final Logger log = LoggerFactory.getLogger(ChatController.class);
    private final ChatClient chatClient;
    private final String systemPrompt;
    private final String conversationId;
    
    @Value("${spring.ai.chat.maxMessages}")
    private final int maxMessages= 100;
    
    ChatMemoryRepository chatMemoryRepository;

    ChatMemory chatMemory = MessageWindowChatMemory.builder()
      .chatMemoryRepository(chatMemoryRepository)
      .maxMessages( maxMessages )
      .build();
    
    public ChatController(ChatClient.Builder chatClientBuilder,
                           ToolCallbackProvider tools,
                           @Value("${spring.ai.chat.system-prompt}") String systemPrompt) {
                           @Value("${spring.ai.chat.system-prompt}") String systemPrompt,
                           ChatMemory chatMemory) {
        // Validate that system prompt is not empty
        if (systemPrompt == null || systemPrompt.trim().isEmpty()) {
            throw new IllegalArgumentException("System prompt cannot be null or empty. Please configure SPRING_AI_CHAT_SYSTEM_PROMPT with a valid value.");
@@ -34,12 +50,14 @@ public class ChatController {
                .defaultSystem(systemPrompt)
                .build();
        log.info("ChatController initialized with system prompt: {}", systemPrompt);
        this.conversationId = UUID.randomUUID().toString();
    }

    @PostMapping("/ask")
    public Answer ask(@RequestBody Question question) {
        var response = chatClient.prompt()
                .user(question.question())
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
                .call()
                .content();
        log.debug("Response from AI: {}", response);
+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ spring:
          temperature: ${SPRING_AI_OLLAMA_CHAT_TEMPERATURE:0.7}
    chat:
      system-prompt: ${SPRING_AI_CHAT_SYSTEM_PROMPT:You are an OpenSlice AI assistant.}
      maxMessages: ${SPRING_AI_CHAT_MAX_MESSAGES:100}
    mcp:
      client:
        type: ${SPRING_AI_MCP_CLIENT_TYPE:SYNC}  # or ASYNC