Skip to content
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

feat(retrievers)!: Move all retriever config options to RetrieverOptions #248

Merged
merged 1 commit into from
Nov 25, 2023

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Nov 25, 2023

Continuing the efforts to add full LCEL support to all core components.

In this case, the Retriever class didn't allow to pass any options when invoking it. VectorStoreRetriever only supported config options in the constructor, which forced users to re-create the retriever every time they wanted to change the options.

A typical use case that was hard to implement was to do an initial search with a high scoreThreshold and then repeat the search with a lower scoreThreshold if the first search didn't return any results.

Now the Retriever classes support defining default options when instantiating them, but you can always override them when invoking the retriever.

const query = 'How much does my order cost?';
// Default options
final retriever = vectorStore.asRetriever(
  defaultOptions: const VectorStoreRetrieverOptions(
    searchType: VectorStoreSimilaritySearch(scoreThreshold: 0.8),
  ),
);

// We invoke the retriever with the default options
List<Document> res = await retriever.invoke(query);

// If empty, we override the default options with a lower score threshold
if(res.isEmpty) {
  res = await retriever.invoke(
    query,
    options: const VectorStoreRetrieverOptions(
      searchType: VectorStoreSimilaritySearch(scoreThreshold: 0.5),
    ),
  );
}

You can also change the options in a Runnable pipeline using the bind method.

final retriever = vectorStore.asRetriever();
final chain = Runnable.fromMap<String>({
  'context1': retriever.bind(retrieverOptions1),
  'context2': retriever.bind(retrieverOptions2),
  'question': Runnable.passthrough(),
}).pipe(promptTemplate).pipe(chatModel).pipe(outputParser);

Migration

Getting a retriever from a vector store

The asRetriever method now takes an optional defaultOptions parameter instead of a searchType.

Before:

final retriever = vectorStore.asRetriever(
  searchType: const VectorStoreSimilaritySearch(k: 10),
);

Now:

final retriever = vectorStore.asRetriever(
  defaultOptions: const VectorStoreRetrieverOptions(
    searchType: VectorStoreSimilaritySearch(k: 10),
  ),
);

Custom retrievers

If you have any custom retrievers, the base retriever class has been renamed from BaseRetriever to Retriever.
You can also define a custom retriever options class (that extends from RetrieverOptions) if you retriever supports additional configuration options.

Before:

class MyCustomRetriever extends BaseRetriever {
  // ...

  @override
  Future<List<Document>> getRelevantDocuments(final String query) {
    // ...
  }
}

Now:

class MyCustomRetriever Retriever<MyCustomRetrieverOptions> {
  // ...

  @override
  Future<List<Document>> getRelevantDocuments(
    final String query, {
    final MyCustomRetrieverOptions? options,
  }) {
    // ...
  }
}

@davidmigloz davidmigloz self-assigned this Nov 25, 2023
@davidmigloz davidmigloz added c:retrievers Retrievers. c:lcel LangChain Expression Language t:enhancement New feature or request labels Nov 25, 2023
@davidmigloz davidmigloz added this to the v0.2.0 milestone Nov 25, 2023
@davidmigloz davidmigloz merged commit f5785b7 into main Nov 25, 2023
1 check passed
@davidmigloz davidmigloz deleted the retriever-options branch November 25, 2023 11:27
KennethKnudsen97 pushed a commit to KennethKnudsen97/langchain_dart that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:lcel LangChain Expression Language c:retrievers Retrievers. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant