-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vector-stores): Allow to pass vector search config (#135)
- Made VectorStoreSearchType a sealed class (similarity and mrr) - Each can have specific configuration (e.g. scoreThreshold) - Implemented scoreThreshold in MemoryVectorStore and VertexAIMatchingEngine
- Loading branch information
1 parent
b211ab4
commit 5b8fa5a
Showing
9 changed files
with
209 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
84 changes: 74 additions & 10 deletions
84
packages/langchain/lib/src/documents/vector_stores/models/models.dart
This file contains 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 |
---|---|---|
@@ -1,11 +1,75 @@ | ||
enum VectorStoreSearchType { | ||
/// Search for documents that are similar to the query. | ||
/// Eg. using Cosine similarity. | ||
similarity, | ||
|
||
/// Maximal marginal relevance. | ||
/// | ||
/// Maximal marginal relevance optimizes for similarity to query | ||
/// AND diversity among selected documents. | ||
mmr, | ||
/// {@template vector_store_search_type} | ||
/// Vector store search type. | ||
/// {@endtemplate} | ||
sealed class VectorStoreSearchType { | ||
/// {@macro vector_store_search_type} | ||
const VectorStoreSearchType({ | ||
required this.k, | ||
}); | ||
|
||
/// The number of documents to return. | ||
final int k; | ||
|
||
/// Similarity search. | ||
factory VectorStoreSearchType.similarity({ | ||
final int k = 4, | ||
final double? scoreThreshold, | ||
}) { | ||
return VectorStoreSimilaritySearch( | ||
k: k, | ||
scoreThreshold: scoreThreshold, | ||
); | ||
} | ||
|
||
/// Maximal Marginal Relevance (MMR) search. | ||
factory VectorStoreSearchType.mmr({ | ||
final int k = 4, | ||
final int fetchK = 20, | ||
final double lambdaMult = 0.5, | ||
}) { | ||
return VectorStoreMMRSearch( | ||
k: k, | ||
fetchK: fetchK, | ||
lambdaMult: lambdaMult, | ||
); | ||
} | ||
} | ||
|
||
/// {@template vector_store_similarity_search} | ||
/// Similarity search. | ||
/// Eg. using Cosine similarity. | ||
/// {@endtemplate} | ||
class VectorStoreSimilaritySearch extends VectorStoreSearchType { | ||
/// {@macro vector_store_similarity_search} | ||
const VectorStoreSimilaritySearch({ | ||
super.k = 4, | ||
this.scoreThreshold, | ||
}); | ||
|
||
/// The minimum relevance score a document must have to be returned. | ||
/// Range: [0, 1]. | ||
final double? scoreThreshold; | ||
} | ||
|
||
/// {@template vector_store_mmr_search} | ||
/// Maximal Marginal Relevance (MMR) search . | ||
/// | ||
/// Maximal marginal relevance optimizes for similarity to query | ||
/// AND diversity among selected documents. | ||
/// {@endtemplate} | ||
class VectorStoreMMRSearch extends VectorStoreSearchType { | ||
/// {@macro vector_store_mmr_search} | ||
const VectorStoreMMRSearch({ | ||
super.k = 4, | ||
this.fetchK = 20, | ||
this.lambdaMult = 0.5, | ||
}); | ||
|
||
/// The number of documents to pass to MMR algorithm. | ||
final int fetchK; | ||
|
||
/// Number between 0 and 1 that determines the degree of diversity among the | ||
/// results with 0 corresponding to maximum diversity and 1 to minimum | ||
/// diversity. | ||
final double lambdaMult; | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.