From 9a8fc5fb0e574b209eb5c7ae6718edd4fd7b6a0f Mon Sep 17 00:00:00 2001 From: showpune Date: Tue, 18 Jun 2024 00:36:15 +0800 Subject: [PATCH] Use QuestionAnswerAdvisor --- .../samples/petclinic/chat/Agent.java | 17 ++++++----------- .../samples/petclinic/chat/AgentConfig.java | 7 +++++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/chat/Agent.java b/src/main/java/org/springframework/samples/petclinic/chat/Agent.java index d24627c..c16c4de 100644 --- a/src/main/java/org/springframework/samples/petclinic/chat/Agent.java +++ b/src/main/java/org/springframework/samples/petclinic/chat/Agent.java @@ -2,6 +2,7 @@ import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.model.ChatModel; import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.chat.prompt.SystemPromptTemplate; import org.springframework.ai.document.Document; @@ -28,6 +29,9 @@ public class Agent { "Without enumerations, hyphens, or any additional formatting!"; @Autowired private ChatClient chatClient; + + @Autowired + private ChatModel chatModel; @Autowired private VectorStore vectorStore; @Value("classpath:/prompts/system-message.st") @@ -36,19 +40,11 @@ public class Agent { public String chat(String userMessage, String username) { try { - String processedDocument = chatClient - .prompt().system(TRANSLATE).user(userMessage).call().content(); - - List docs = vectorStore.similaritySearch(processedDocument); - StringBuilder sop = new StringBuilder(); - for (Document doc : docs) { - sop.append(doc.getContent()).append("\n"); - } + String processedMessage = chatModel.call(TRANSLATE+"\n"+userMessage); Consumer advisorSpecConsumer = advisorSpec -> { advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, username); }; - PromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource); Map systemParameters = new HashMap<>() {{ put("username", username); @@ -59,8 +55,7 @@ public String chat(String userMessage, String username) { //userName as memory key .advisors(advisorSpecConsumer) .system(systemPromptTemplate.render(systemParameters)) - .messages(new UserMessage(sop.toString())) - .user(userMessage) + .user(processedMessage) .functions("queryOwners", "addOwner", "updateOwner", "queryVets") .call() .content(); diff --git a/src/main/java/org/springframework/samples/petclinic/chat/AgentConfig.java b/src/main/java/org/springframework/samples/petclinic/chat/AgentConfig.java index a1be131..bb75f00 100644 --- a/src/main/java/org/springframework/samples/petclinic/chat/AgentConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/chat/AgentConfig.java @@ -4,12 +4,14 @@ import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClientCustomizer; import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor; +import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.memory.InMemoryChatMemory; import org.springframework.ai.document.Document; import org.springframework.ai.embedding.EmbeddingModel; import org.springframework.ai.reader.TextReader; import org.springframework.ai.transformer.splitter.TokenTextSplitter; +import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.SimpleVectorStore; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.context.annotation.Bean; @@ -29,9 +31,10 @@ public ChatClient chatClient(ChatClient.Builder chatClientBuilder) { } @Bean - public ChatClientCustomizer chatClientCustomizer() { + public ChatClientCustomizer chatClientCustomizer(VectorStore vectorStore) { ChatMemory chatMemory = new InMemoryChatMemory(); - return b -> b.defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory)); + return b -> b.defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory) + , new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults())); } @Bean