-
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
b386585
commit 013d514
Showing
4 changed files
with
50 additions
and
37 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
35 changes: 13 additions & 22 deletions
35
chatbot/chatbot-openai-springai/src/main/java/com/example/chatbot/config/ChatConfig.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,40 +1,31 @@ | ||
package com.example.chatbot.config; | ||
|
||
import java.util.List; | ||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; | ||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor; | ||
import org.springframework.ai.chat.client.advisor.VectorStoreChatMemoryAdvisor; | ||
import org.springframework.ai.chat.memory.ChatMemory; | ||
import org.springframework.ai.chat.memory.ChatMemoryChatServiceListener; | ||
import org.springframework.ai.chat.memory.ChatMemoryRetriever; | ||
import org.springframework.ai.chat.memory.InMemoryChatMemory; | ||
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer; | ||
import org.springframework.ai.chat.memory.SystemPromptChatMemoryAugmentor; | ||
import org.springframework.ai.chat.model.ChatModel; | ||
import org.springframework.ai.chat.service.ChatService; | ||
import org.springframework.ai.chat.service.PromptTransformingChatService; | ||
import org.springframework.ai.tokenizer.JTokkitTokenCountEstimator; | ||
import org.springframework.ai.tokenizer.TokenCountEstimator; | ||
import org.springframework.ai.vectorstore.SearchRequest; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
public class ChatConfig { | ||
|
||
@Bean | ||
ChatMemory chatHistory() { | ||
ChatMemory chatMemory() { | ||
return new InMemoryChatMemory(); | ||
} | ||
|
||
@Bean | ||
TokenCountEstimator tokenCountEstimator() { | ||
return new JTokkitTokenCountEstimator(); | ||
} | ||
|
||
@Bean | ||
ChatService chatService(ChatModel chatModel, ChatMemory chatMemory, TokenCountEstimator tokenCountEstimator) { | ||
return 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))) | ||
ChatClient chatClient(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory, VectorStore vectorStore) { | ||
return chatClientBuilder | ||
.defaultAdvisors( | ||
new MessageChatMemoryAdvisor(chatMemory), | ||
new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()), // RAG | ||
new VectorStoreChatMemoryAdvisor(vectorStore)) | ||
.build(); | ||
} | ||
} |
33 changes: 18 additions & 15 deletions
33
...bot/chatbot-openai-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 |
---|---|---|
@@ -1,34 +1,37 @@ | ||
package com.example.chatbot.service; | ||
|
||
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY; | ||
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY; | ||
|
||
import com.example.chatbot.model.request.AIChatRequest; | ||
import com.example.chatbot.model.response.AIChatResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
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.ChatServiceResponse; | ||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.model.ChatResponse; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ChatbotService { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ChatbotService.class); | ||
|
||
private final ChatService chatService; | ||
private final ChatClient chatClient; | ||
|
||
public ChatbotService(ChatService chatService) { | ||
this.chatService = chatService; | ||
public ChatbotService(ChatClient chatClient) { | ||
this.chatClient = chatClient; | ||
} | ||
|
||
public AIChatResponse chat(AIChatRequest request) { | ||
Prompt prompt = new Prompt(new UserMessage(request.query())); | ||
String conversationId = request.conversationId() == null ? "default" : request.conversationId(); | ||
ChatServiceResponse chatServiceResponse = this.chatService.call(new ChatServiceContext(prompt, conversationId)); | ||
LOGGER.info("Response :{}", chatServiceResponse.getChatResponse().getResult()); | ||
return new AIChatResponse( | ||
chatServiceResponse.getChatResponse().getResult().getOutput().getContent(), | ||
chatServiceResponse.getPromptContext().getConversationId()); | ||
|
||
ChatResponse chatResponse = this.chatClient | ||
.prompt() | ||
.user(request.query()) | ||
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, request.conversationId()) | ||
.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)) | ||
.call() | ||
.chatResponse(); | ||
LOGGER.info("Response :{}", chatResponse.getResult()); | ||
return new AIChatResponse(chatResponse.getResult().getOutput().getContent(), request.conversationId()); | ||
} | ||
} |
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