Skip to content

Commit

Permalink
Use QuestionAnswerAdvisor
Browse files Browse the repository at this point in the history
  • Loading branch information
showpune committed Jun 17, 2024
1 parent ec04b77 commit 9a8fc5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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<Document> 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<ChatClient.AdvisorSpec> advisorSpecConsumer = advisorSpec -> {
advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, username);
};

PromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Map<String, Object> systemParameters = new HashMap<>() {{
put("username", username);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 9a8fc5f

Please sign in to comment.