forked from davidmigloz/langchain_dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(retrievers)!: Move all retriever config options to RetrieverOpti…
…ons (davidmigloz#248)
- Loading branch information
1 parent
f5184fc
commit 208f476
Showing
9 changed files
with
76 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import '../../core/core.dart'; | ||
import '../models/models.dart'; | ||
import 'models/models.dart'; | ||
|
||
/// {@template base_retriever} | ||
/// Base Index class. All indexes should extend this class. | ||
/// {@endtemplate} | ||
abstract class BaseRetriever | ||
extends Runnable<String, BaseLangChainOptions, List<Document>> { | ||
abstract class Retriever<Options extends RetrieverOptions> | ||
extends Runnable<String, Options, List<Document>> { | ||
/// {@macro base_retriever} | ||
const BaseRetriever(); | ||
const Retriever(); | ||
|
||
/// Get the most relevant documents for a given query. | ||
/// | ||
/// - [input] - The query to search for. | ||
/// - [options] - Retrieval options. | ||
@override | ||
Future<List<Document>> invoke( | ||
final String input, { | ||
final BaseLangChainOptions? options, | ||
final Options? options, | ||
}) { | ||
return getRelevantDocuments(input); | ||
return getRelevantDocuments(input, options: options); | ||
} | ||
|
||
/// Get the most relevant documents for a given query. | ||
/// | ||
/// - [query] - The query to search for. | ||
Future<List<Document>> getRelevantDocuments(final String query); | ||
/// - [options] - Retrieval options. | ||
Future<List<Document>> getRelevantDocuments( | ||
final String query, { | ||
final Options? options, | ||
}); | ||
} |
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,19 +1,23 @@ | ||
import '../models/models.dart'; | ||
import 'base.dart'; | ||
import 'models/models.dart'; | ||
|
||
/// {@template fake_retriever} | ||
/// A retriever that returns a fixed list of documents. | ||
/// This class is meant for testing purposes only. | ||
/// {@endtemplate} | ||
class FakeRetriever extends BaseRetriever { | ||
class FakeRetriever extends Retriever<RetrieverOptions> { | ||
/// {@macro fake_retriever} | ||
const FakeRetriever(this.docs); | ||
|
||
/// The documents to return. | ||
final List<Document> docs; | ||
|
||
@override | ||
Future<List<Document>> getRelevantDocuments(final String query) { | ||
Future<List<Document>> getRelevantDocuments( | ||
final String query, { | ||
final RetrieverOptions? options, | ||
}) { | ||
return Future.value(docs); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/langchain/lib/src/documents/retrievers/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,29 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../../../core/base.dart'; | ||
import '../../vector_stores/models/models.dart'; | ||
import '../retrievers.dart'; | ||
|
||
/// {@template retriever_options} | ||
/// Base class for [Retriever] options. | ||
/// {@endtemplate} | ||
@immutable | ||
class RetrieverOptions extends BaseLangChainOptions { | ||
/// {@macro retriever_options} | ||
const RetrieverOptions(); | ||
} | ||
|
||
/// {@template vector_store_retriever_options} | ||
/// Options for [VectorStoreRetriever]. | ||
/// {@endtemplate} | ||
class VectorStoreRetrieverOptions extends RetrieverOptions { | ||
/// {@macro vector_store_retriever_options} | ||
const VectorStoreRetrieverOptions({ | ||
this.searchType = const VectorStoreSimilaritySearch(), | ||
}); | ||
|
||
/// The type of search to perform, either: | ||
/// - [VectorStoreSearchType.similarity] (default) | ||
/// - [VectorStoreSearchType.mmr] | ||
final VectorStoreSearchType searchType; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/langchain/lib/src/documents/retrievers/retrievers.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,3 +1,4 @@ | ||
export 'base.dart'; | ||
export 'fake.dart'; | ||
export 'models/models.dart'; | ||
export 'vector_store.dart'; |
20 changes: 14 additions & 6 deletions
20
packages/langchain/lib/src/documents/retrievers/vector_store.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,25 +1,33 @@ | ||
import '../models/models.dart'; | ||
import '../vector_stores/vector_stores.dart'; | ||
import 'base.dart'; | ||
import 'models/models.dart'; | ||
|
||
/// {@template vector_store_retriever} | ||
/// A retriever that uses a vector store to retrieve documents. | ||
/// {@endtemplate} | ||
class VectorStoreRetriever<V extends VectorStore> extends BaseRetriever { | ||
class VectorStoreRetriever<V extends VectorStore> | ||
extends Retriever<VectorStoreRetrieverOptions> { | ||
/// {@macro vector_store_retriever} | ||
const VectorStoreRetriever({ | ||
required this.vectorStore, | ||
this.searchType = const VectorStoreSimilaritySearch(), | ||
this.defaultOptions = const VectorStoreRetrieverOptions(), | ||
}); | ||
|
||
/// The vector store to retrieve documents from. | ||
final V vectorStore; | ||
|
||
/// The type of search to perform. | ||
final VectorStoreSearchType searchType; | ||
/// Default options for this retriever. | ||
final VectorStoreRetrieverOptions defaultOptions; | ||
|
||
@override | ||
Future<List<Document>> getRelevantDocuments(final String query) { | ||
return vectorStore.search(query: query, searchType: searchType); | ||
Future<List<Document>> getRelevantDocuments( | ||
final String query, { | ||
final VectorStoreRetrieverOptions? options, | ||
}) { | ||
return vectorStore.search( | ||
query: query, | ||
searchType: options?.searchType ?? defaultOptions.searchType, | ||
); | ||
} | ||
} |
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