-
Notifications
You must be signed in to change notification settings - Fork 1.6k
add similarity threshold capability to VectorStoreChatMemoryAdvisor #3454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
scionaltera
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
scionaltera:add-vector-store-similarity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ | |
*/ | ||
public final class VectorStoreChatMemoryAdvisor implements BaseChatMemoryAdvisor { | ||
|
||
public static final String SIMILARITY_THRESHOLD = "chat_memory_vector_store_similarity_threshold"; | ||
|
||
public static final String TOP_K = "chat_memory_vector_store_top_k"; | ||
|
||
private static final String DOCUMENT_METADATA_CONVERSATION_ID = "conversationId"; | ||
|
@@ -64,6 +66,8 @@ public final class VectorStoreChatMemoryAdvisor implements BaseChatMemoryAdvisor | |
|
||
private static final int DEFAULT_TOP_K = 20; | ||
|
||
private static final double DEFAULT_SIMILARITY_THRESHOLD = 0; | ||
|
||
private static final PromptTemplate DEFAULT_SYSTEM_PROMPT_TEMPLATE = new PromptTemplate(""" | ||
{instructions} | ||
|
||
|
@@ -79,6 +83,8 @@ public final class VectorStoreChatMemoryAdvisor implements BaseChatMemoryAdvisor | |
|
||
private final int defaultTopK; | ||
|
||
private final double defaultSimilarityThreshold; | ||
|
||
private final String defaultConversationId; | ||
|
||
private final int order; | ||
|
@@ -88,14 +94,17 @@ public final class VectorStoreChatMemoryAdvisor implements BaseChatMemoryAdvisor | |
private final VectorStore vectorStore; | ||
|
||
private VectorStoreChatMemoryAdvisor(PromptTemplate systemPromptTemplate, int defaultTopK, | ||
String defaultConversationId, int order, Scheduler scheduler, VectorStore vectorStore) { | ||
double defaultSimilarityThreshold, String defaultConversationId, int order, Scheduler scheduler, | ||
VectorStore vectorStore) { | ||
Assert.notNull(systemPromptTemplate, "systemPromptTemplate cannot be null"); | ||
Assert.isTrue(defaultTopK > 0, "topK must be greater than 0"); | ||
Assert.isTrue(defaultSimilarityThreshold >= 0, "similarityThreshold must be equal to or greater than 0"); | ||
Assert.hasText(defaultConversationId, "defaultConversationId cannot be null or empty"); | ||
Assert.notNull(scheduler, "scheduler cannot be null"); | ||
Assert.notNull(vectorStore, "vectorStore cannot be null"); | ||
this.systemPromptTemplate = systemPromptTemplate; | ||
this.defaultTopK = defaultTopK; | ||
this.defaultSimilarityThreshold = defaultSimilarityThreshold; | ||
this.defaultConversationId = defaultConversationId; | ||
this.order = order; | ||
this.scheduler = scheduler; | ||
|
@@ -121,10 +130,12 @@ public ChatClientRequest before(ChatClientRequest request, AdvisorChain advisorC | |
String conversationId = getConversationId(request.context(), this.defaultConversationId); | ||
String query = request.prompt().getUserMessage() != null ? request.prompt().getUserMessage().getText() : ""; | ||
int topK = getChatMemoryTopK(request.context()); | ||
double similarityThreshold = getChatMemorySimilarityThreshold(request.context()); | ||
String filter = DOCUMENT_METADATA_CONVERSATION_ID + "=='" + conversationId + "'"; | ||
var searchRequest = org.springframework.ai.vectorstore.SearchRequest.builder() | ||
.query(query) | ||
.topK(topK) | ||
.similarityThreshold(similarityThreshold) | ||
.filterExpression(filter) | ||
.build(); | ||
java.util.List<org.springframework.ai.document.Document> documents = this.vectorStore | ||
|
@@ -156,6 +167,11 @@ private int getChatMemoryTopK(Map<String, Object> context) { | |
return context.containsKey(TOP_K) ? Integer.parseInt(context.get(TOP_K).toString()) : this.defaultTopK; | ||
} | ||
|
||
private double getChatMemorySimilarityThreshold(Map<String, Object> context) { | ||
return context.containsKey(SIMILARITY_THRESHOLD) | ||
? Double.parseDouble(context.get(SIMILARITY_THRESHOLD).toString()) : this.defaultSimilarityThreshold; | ||
} | ||
|
||
@Override | ||
public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) { | ||
List<Message> assistantMessages = new ArrayList<>(); | ||
|
@@ -221,6 +237,8 @@ public static class Builder { | |
|
||
private Integer defaultTopK = DEFAULT_TOP_K; | ||
|
||
private Double defaultSimilarityThreshold = DEFAULT_SIMILARITY_THRESHOLD; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
|
||
private String conversationId = ChatMemory.DEFAULT_CONVERSATION_ID; | ||
|
||
private Scheduler scheduler = BaseAdvisor.DEFAULT_SCHEDULER; | ||
|
@@ -257,6 +275,17 @@ public Builder defaultTopK(int defaultTopK) { | |
return this; | ||
} | ||
|
||
/** | ||
* Set the similarity threshold for retrieving relevant documents. | ||
* @param defaultSimilarityThreshold the required similarity for documents to | ||
* retrieve | ||
* @return this builder | ||
*/ | ||
public Builder defaultSimilarityThreshold(Double defaultSimilarityThreshold) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we don't need to use the Double wrapper class here? |
||
this.defaultSimilarityThreshold = defaultSimilarityThreshold; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the conversation id. | ||
* @param conversationId the conversation id | ||
|
@@ -287,8 +316,8 @@ public Builder order(int order) { | |
* @return the advisor | ||
*/ | ||
public VectorStoreChatMemoryAdvisor build() { | ||
return new VectorStoreChatMemoryAdvisor(this.systemPromptTemplate, this.defaultTopK, this.conversationId, | ||
this.order, this.scheduler, this.vectorStore); | ||
return new VectorStoreChatMemoryAdvisor(this.systemPromptTemplate, this.defaultTopK, | ||
this.defaultSimilarityThreshold, this.conversationId, this.order, this.scheduler, this.vectorStore); | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the assertion of method
org.springframework.ai.vectorstore.SearchRequest.Builder#similarityThreshold
.The
defaultSimilarityThreshold
should bedefaultSimilarityThreshold >= 0 && defaultSimilarityThreshold <= 1