-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce82ad3
commit 007b47f
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
chatbot-ollama3-springai/src/main/java/com/example/chatbot/controller/ChatbotController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
chatbot-ollama3-springai/src/main/java/com/example/chatbot/service/ChatbotService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
chatbot-ollama3-springai/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |