-
-
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): Support filtering in VertexAI Matching Engine (#136
- Loading branch information
1 parent
5b8fa5a
commit 768c698
Showing
6 changed files
with
156 additions
and
2 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
15 changes: 15 additions & 0 deletions
15
packages/langchain_google/lib/src/vector_stores/models/mappers.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:vertex_ai/vertex_ai.dart'; | ||
|
||
import 'models.dart'; | ||
|
||
abstract class VertexAIMatchingEngineFilterMapper { | ||
static VertexAIIndexDatapointRestriction toDto( | ||
final VertexAIMatchingEngineFilter filter, | ||
) { | ||
return VertexAIIndexDatapointRestriction( | ||
namespace: filter.namespace, | ||
allowList: filter.allowList, | ||
denyList: filter.denyList, | ||
); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/langchain_google/lib/src/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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'package:langchain/langchain.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'mappers.dart'; | ||
|
||
/// {@template vector_store_similarity_search} | ||
/// Vertex AI Matching Engine similarity search config. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// VertexAIMatchingEngineSimilaritySearch( | ||
/// k: 5, | ||
/// filters: [ | ||
/// const VertexAIMatchingEngineFilter( | ||
/// namespace: 'class', | ||
/// allowList: ['pet'], | ||
/// ), | ||
/// const VertexAIMatchingEngineFilter( | ||
/// namespace: 'category', | ||
/// denyList: ['canine'], | ||
/// ), | ||
/// ] | ||
/// ), | ||
/// ``` | ||
/// {@endtemplate} | ||
class VertexAIMatchingEngineSimilaritySearch | ||
extends VectorStoreSimilaritySearch { | ||
VertexAIMatchingEngineSimilaritySearch({ | ||
super.k = 4, | ||
final List<VertexAIMatchingEngineFilter>? filters, | ||
super.scoreThreshold, | ||
}) : super( | ||
filter: filters != null | ||
? { | ||
filterKey: filters | ||
.map( | ||
VertexAIMatchingEngineFilterMapper.toDto, | ||
) | ||
.toList(growable: false), | ||
} | ||
: null, | ||
); | ||
|
||
static const filterKey = 'restricts'; | ||
} | ||
|
||
/// {@template vertex_ai_matching_engine_filter} | ||
/// Filter for the Vertex AI Matching Engine. | ||
/// See: https://cloud.google.com/vertex-ai/docs/matching-engine/filtering | ||
/// {@endtemplate} | ||
@immutable | ||
class VertexAIMatchingEngineFilter { | ||
/// {@macro vertex_ai_matching_engine_filter} | ||
const VertexAIMatchingEngineFilter({ | ||
required this.namespace, | ||
this.allowList = const [], | ||
this.denyList = const [], | ||
}); | ||
|
||
/// The namespace of this restriction. | ||
/// | ||
/// eg: color. | ||
final String namespace; | ||
|
||
/// The attributes to allow in this namespace. | ||
/// | ||
/// eg: 'red' | ||
final List<String> allowList; | ||
|
||
/// The attributes to deny in this namespace. | ||
/// | ||
/// eg: 'blue' | ||
final List<String> denyList; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/langchain_google/lib/src/vector_stores/vector_stores.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 +1,2 @@ | ||
export 'matching_engine.dart'; | ||
export '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