diff --git a/chatbot-ollama3-springai/src/main/java/com/example/chatbot/controller/ChatbotController.java b/chatbot-ollama3-springai/src/main/java/com/example/chatbot/controller/ChatbotController.java new file mode 100644 index 0000000..e5292dd --- /dev/null +++ b/chatbot-ollama3-springai/src/main/java/com/example/chatbot/controller/ChatbotController.java @@ -0,0 +1,21 @@ +package com.example.chatbot.controller; + +import com.example.chatbot.service.ChatbotService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +class ChatbotController { + + private final ChatbotService chatbotService; + + ChatbotController(ChatbotService chatbotService) { + this.chatbotService = chatbotService; + } + + @PostMapping("/chat") + String chat(@RequestBody String input) { + return chatbotService.chat(input); + } +} diff --git a/chatbot-ollama3-springai/src/main/java/com/example/chatbot/service/ChatbotService.java b/chatbot-ollama3-springai/src/main/java/com/example/chatbot/service/ChatbotService.java new file mode 100644 index 0000000..86586e6 --- /dev/null +++ b/chatbot-ollama3-springai/src/main/java/com/example/chatbot/service/ChatbotService.java @@ -0,0 +1,33 @@ +package com.example.chatbot.service; + +import java.util.List; +import org.springframework.ai.chat.memory.*; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.chat.prompt.transformer.ChatServiceContext; +import org.springframework.ai.chat.service.ChatService; +import org.springframework.ai.chat.service.PromptTransformingChatService; +import org.springframework.ai.tokenizer.TokenCountEstimator; +import org.springframework.stereotype.Service; + +@Service +public class ChatbotService { + + private final ChatService chatService; + + ChatbotService(ChatModel chatModel, ChatMemory chatMemory, TokenCountEstimator tokenCountEstimator) { + this.chatService = PromptTransformingChatService.builder(chatModel) + .withRetrievers(List.of(new ChatMemoryRetriever(chatMemory))) + .withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000))) + .withAugmentors(List.of(new SystemPromptChatMemoryAugmentor())) + .withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatMemory))) + .build(); + } + + public String chat(String message) { + var prompt = new Prompt(new UserMessage(message)); + var chatServiceResponse = this.chatService.call(new ChatServiceContext(prompt)); + return chatServiceResponse.getChatResponse().getResult().getOutput().getContent(); + } +} diff --git a/chatbot-ollama3-springai/src/main/resources/application.properties b/chatbot-ollama3-springai/src/main/resources/application.properties index 689616e..edce8bf 100644 --- a/chatbot-ollama3-springai/src/main/resources/application.properties +++ b/chatbot-ollama3-springai/src/main/resources/application.properties @@ -1,3 +1,5 @@ spring.application.name=chatbot-ollama spring.ai.ollama.chat.options.model=llama3 + +spring.threads.virtual.enabled=true