3333import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisor ;
3434import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisorChain ;
3535import org .springframework .ai .chat .model .ChatResponse ;
36+ import org .springframework .ai .chat .prompt .PromptTemplate ;
3637import org .springframework .ai .document .Document ;
3738import org .springframework .ai .model .Content ;
3839import org .springframework .ai .vectorstore .SearchRequest ;
4748 * user text.
4849 *
4950 * @author Christian Tzolov
51+ * @author Timo Salm
5052 * @since 1.0.0
5153 */
5254public class QuestionAnswerAdvisor implements CallAroundAdvisor , StreamAroundAdvisor {
@@ -106,7 +108,7 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
106108 * @param vectorStore The vector store to use
107109 * @param searchRequest The search request defined using the portable filter
108110 * expression syntax
109- * @param userTextAdvise the user text to append to the existing user prompt. The text
111+ * @param userTextAdvise The user text to append to the existing user prompt. The text
110112 * should contain a placeholder named "question_answer_context".
111113 */
112114 public QuestionAnswerAdvisor (VectorStore vectorStore , SearchRequest searchRequest , String userTextAdvise ) {
@@ -119,9 +121,9 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
119121 * @param vectorStore The vector store to use
120122 * @param searchRequest The search request defined using the portable filter
121123 * expression syntax
122- * @param userTextAdvise the user text to append to the existing user prompt. The text
124+ * @param userTextAdvise The user text to append to the existing user prompt. The text
123125 * should contain a placeholder named "question_answer_context".
124- * @param protectFromBlocking if true the advisor will protect the execution from
126+ * @param protectFromBlocking If true the advisor will protect the execution from
125127 * blocking threads. If false the advisor will not protect the execution from blocking
126128 * threads. This is useful when the advisor is used in a non-blocking environment. It
127129 * is true by default.
@@ -137,13 +139,13 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
137139 * @param vectorStore The vector store to use
138140 * @param searchRequest The search request defined using the portable filter
139141 * expression syntax
140- * @param userTextAdvise the user text to append to the existing user prompt. The text
142+ * @param userTextAdvise The user text to append to the existing user prompt. The text
141143 * should contain a placeholder named "question_answer_context".
142- * @param protectFromBlocking if true the advisor will protect the execution from
144+ * @param protectFromBlocking If true the advisor will protect the execution from
143145 * blocking threads. If false the advisor will not protect the execution from blocking
144146 * threads. This is useful when the advisor is used in a non-blocking environment. It
145147 * is true by default.
146- * @param order the order of the advisor.
148+ * @param order The order of the advisor.
147149 */
148150 public QuestionAnswerAdvisor (VectorStore vectorStore , SearchRequest searchRequest , String userTextAdvise ,
149151 boolean protectFromBlocking , int order ) {
@@ -213,16 +215,17 @@ private AdvisedRequest before(AdvisedRequest request) {
213215 // 1. Advise the system text.
214216 String advisedUserText = request .userText () + System .lineSeparator () + this .userTextAdvise ;
215217
218+ // 2. Search for similar documents in the vector store.
219+ String query = new PromptTemplate (request .userText (), request .userParams ()).render ();
216220 var searchRequestToUse = SearchRequest .from (this .searchRequest )
217- .withQuery (request . userText () )
221+ .withQuery (query )
218222 .withFilterExpression (doGetFilterExpression (context ));
219223
220- // 2. Search for similar documents in the vector store.
221224 List <Document > documents = this .vectorStore .similaritySearch (searchRequestToUse );
222225
226+ // 3. Create the context from the documents.
223227 context .put (RETRIEVED_DOCUMENTS , documents );
224228
225- // 3. Create the context from the documents.
226229 String documentContext = documents .stream ()
227230 .map (Content ::getContent )
228231 .collect (Collectors .joining (System .lineSeparator ()));
0 commit comments