-
Notifications
You must be signed in to change notification settings - Fork 74
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
b208f2a
commit f9588c8
Showing
5 changed files
with
22 additions
and
52 deletions.
There are no files selected for viewing
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
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
35 changes: 15 additions & 20 deletions
35
00-use-cases/chatbot/src/main/java/com/thomasvitale/ai/spring/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 |
---|---|---|
@@ -1,35 +1,30 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
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.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; | ||
import org.springframework.ai.chat.memory.ChatMemory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY; | ||
|
||
@Service | ||
class ChatbotService { | ||
|
||
private final ChatService chatService; | ||
private final ChatClient chatClient; | ||
|
||
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))) | ||
ChatbotService(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) { | ||
this.chatClient = chatClientBuilder | ||
.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory)) | ||
.build(); | ||
} | ||
|
||
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(); | ||
String chat(String chatId, String message) { | ||
return chatClient | ||
.prompt() | ||
.user(message) | ||
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)) | ||
.call() | ||
.content(); | ||
} | ||
|
||
} |
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
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