Skip to content

Commit

Permalink
expose endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed May 26, 2024
1 parent ce82ad3 commit 007b47f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.application.name=chatbot-ollama

spring.ai.ollama.chat.options.model=llama3

spring.threads.virtual.enabled=true

0 comments on commit 007b47f

Please sign in to comment.