33
33
import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisor ;
34
34
import org .springframework .ai .chat .client .advisor .api .StreamAroundAdvisorChain ;
35
35
import org .springframework .ai .chat .model .ChatResponse ;
36
+ import org .springframework .ai .chat .prompt .PromptTemplate ;
36
37
import org .springframework .ai .document .Document ;
37
38
import org .springframework .ai .model .Content ;
38
39
import org .springframework .ai .vectorstore .SearchRequest ;
47
48
* user text.
48
49
*
49
50
* @author Christian Tzolov
51
+ * @author Timo Salm
50
52
* @since 1.0.0
51
53
*/
52
54
public class QuestionAnswerAdvisor implements CallAroundAdvisor , StreamAroundAdvisor {
@@ -106,7 +108,7 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
106
108
* @param vectorStore The vector store to use
107
109
* @param searchRequest The search request defined using the portable filter
108
110
* 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
110
112
* should contain a placeholder named "question_answer_context".
111
113
*/
112
114
public QuestionAnswerAdvisor (VectorStore vectorStore , SearchRequest searchRequest , String userTextAdvise ) {
@@ -119,9 +121,9 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
119
121
* @param vectorStore The vector store to use
120
122
* @param searchRequest The search request defined using the portable filter
121
123
* 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
123
125
* 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
125
127
* blocking threads. If false the advisor will not protect the execution from blocking
126
128
* threads. This is useful when the advisor is used in a non-blocking environment. It
127
129
* is true by default.
@@ -137,13 +139,13 @@ public QuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchReques
137
139
* @param vectorStore The vector store to use
138
140
* @param searchRequest The search request defined using the portable filter
139
141
* 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
141
143
* 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
143
145
* blocking threads. If false the advisor will not protect the execution from blocking
144
146
* threads. This is useful when the advisor is used in a non-blocking environment. It
145
147
* is true by default.
146
- * @param order the order of the advisor.
148
+ * @param order The order of the advisor.
147
149
*/
148
150
public QuestionAnswerAdvisor (VectorStore vectorStore , SearchRequest searchRequest , String userTextAdvise ,
149
151
boolean protectFromBlocking , int order ) {
@@ -213,16 +215,17 @@ private AdvisedRequest before(AdvisedRequest request) {
213
215
// 1. Advise the system text.
214
216
String advisedUserText = request .userText () + System .lineSeparator () + this .userTextAdvise ;
215
217
218
+ // 2. Search for similar documents in the vector store.
219
+ String query = new PromptTemplate (request .userText (), request .userParams ()).render ();
216
220
var searchRequestToUse = SearchRequest .from (this .searchRequest )
217
- .withQuery (request . userText () )
221
+ .withQuery (query )
218
222
.withFilterExpression (doGetFilterExpression (context ));
219
223
220
- // 2. Search for similar documents in the vector store.
221
224
List <Document > documents = this .vectorStore .similaritySearch (searchRequestToUse );
222
225
226
+ // 3. Create the context from the documents.
223
227
context .put (RETRIEVED_DOCUMENTS , documents );
224
228
225
- // 3. Create the context from the documents.
226
229
String documentContext = documents .stream ()
227
230
.map (Content ::getContent )
228
231
.collect (Collectors .joining (System .lineSeparator ()));
0 commit comments