spring-ai
spring-ai copied to clipboard
QuestionAnswerAdvisor only uses userText for similarity search and does not take parameters into account
Bug description
The QuestionAnswerAdvisor only uses the userText attribute of an AdvisedRequest for the similarity search.
Therefore, the similarity search for this snippet works as expected:
var advise = new PromptTemplate(preferOwnRecipePromptResource).getTemplate();
var advisorSearchRequest = SearchRequest.defaults().withTopK(2).withSimilarityThreshold(0.7);
chatClient.prompt()
.user("Provide me a recipe with the ingredient potato")
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults(), advise))
.call()
.entity(Recipe.class);
But for the following snippet, parameters are not taken into account. So the similarity search will be executed with the query "Provide me a recipe with the ingredient {ingredient}".
chatClient.prompt()
.user(u -> u.text("Provide me a recipe with the ingredient {ingredient}").param("ingredient", "potato"))
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults(), advise))
.call()
.entity(Recipe.class);
The workaround I use is to create a PromptTemplate with the template and the parameters and use .user(promptTemplate.render()).
In addition, a configured UserMessages will also not be used for similarity search.
chatClient.prompt()
.messages(promptTemplate.createMessage())
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults(), advise))
.call()
.entity(Recipe.class);
Environment Sprin AI version: M1, Java version 21, vector db: redis, AI model: ollama llama3
Steps to reproduce See sample code in description.
Expected behavior By using a Consumer<AdvisorSpec> to define the user query, the parameters should be taken into account for similarity search. Configured UserMessages should be also used for similarity search.
Hi, we had the same issue in our example in the following lines
Our current workaround is to do:
PromptTemplate promptTemplate = new PromptTemplate(userPrompt);
Prompt prompt = promptTemplate.create(Map.of("question", message));
but if we switch to:
.user(prompt -> prompt.text(this.userPrompt).param("question", message))
then it fails because of what is described in the issue.
Debugging the code, we can see that the DefaultChatClient is rendering the content correctly but no the QuestionAnswerAdvisor.
https://github.com/spring-projects/spring-ai/blob/a55316c44dee87c01c330b90403075996d9acc73/spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java#L452