|
56 | 56 | */ |
57 | 57 | public final class VectorStoreChatMemoryAdvisor implements BaseChatMemoryAdvisor { |
58 | 58 |
|
| 59 | + public static final String SIMILARITY_THRESHOLD = "chat_memory_vector_store_similarity_threshold"; |
| 60 | + |
59 | 61 | public static final String TOP_K = "chat_memory_vector_store_top_k"; |
60 | 62 |
|
61 | 63 | private static final String DOCUMENT_METADATA_CONVERSATION_ID = "conversationId"; |
@@ -128,10 +130,12 @@ public ChatClientRequest before(ChatClientRequest request, AdvisorChain advisorC |
128 | 130 | String conversationId = getConversationId(request.context(), this.defaultConversationId); |
129 | 131 | String query = request.prompt().getUserMessage() != null ? request.prompt().getUserMessage().getText() : ""; |
130 | 132 | int topK = getChatMemoryTopK(request.context()); |
| 133 | + double similarityThreshold = getChatMemorySimilarityThreshold(request.context()); |
131 | 134 | String filter = DOCUMENT_METADATA_CONVERSATION_ID + "=='" + conversationId + "'"; |
132 | 135 | var searchRequest = org.springframework.ai.vectorstore.SearchRequest.builder() |
133 | 136 | .query(query) |
134 | 137 | .topK(topK) |
| 138 | + .similarityThreshold(similarityThreshold) |
135 | 139 | .filterExpression(filter) |
136 | 140 | .build(); |
137 | 141 | java.util.List<org.springframework.ai.document.Document> documents = this.vectorStore |
@@ -163,6 +167,11 @@ private int getChatMemoryTopK(Map<String, Object> context) { |
163 | 167 | return context.containsKey(TOP_K) ? Integer.parseInt(context.get(TOP_K).toString()) : this.defaultTopK; |
164 | 168 | } |
165 | 169 |
|
| 170 | + private double getChatMemorySimilarityThreshold(Map<String, Object> context) { |
| 171 | + return context.containsKey(SIMILARITY_THRESHOLD) |
| 172 | + ? Double.parseDouble(context.get(SIMILARITY_THRESHOLD).toString()) : this.defaultSimilarityThreshold; |
| 173 | + } |
| 174 | + |
166 | 175 | @Override |
167 | 176 | public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) { |
168 | 177 | List<Message> assistantMessages = new ArrayList<>(); |
|
0 commit comments