llm_retriever#

LLM as retriever module.

Classes

LLMRetriever(*[, top_k, model_kwargs, ...])

Use LLM to access the query and the documents to retrieve the top k relevant indices of the documents.

class LLMRetriever(*, top_k: int | None = 1, model_client: ModelClient, model_kwargs: Dict[str, Any] = {}, documents: Sequence[RetrieverDocumentType] | None = None, document_map_func: Callable[[Any], str] | None = None)[source]#

Bases: Retriever[str, str]

Use LLM to access the query and the documents to retrieve the top k relevant indices of the documents.

Users can follow this example and to customize the prompt or additionally ask it to output score along with the indices.

Parameters:
  • top_k (Optional[int], optional) – top k documents to fetch. Defaults to 1.

  • model_client (ModelClient) – the model client to use.

  • model_kwargs (Dict[str, Any], optional) – the model kwargs. Defaults to {}.

Note

There is chance some queries might fail, which will lead to empty response None for that query in the List of RetrieverOutput. Users should handle this case.

reset_index()[source]#

Initialize/reset any attributes/states for the index.

build_index_from_documents(documents: Sequence[RetrieverDocumentType], document_map_func: Callable[[Any], str] | None = None)[source]#

prepare the user query input for the retriever

call(input: str | Sequence[str], top_k: int | None = None, model_kwargs: Dict[str, Any] = {}) List[RetrieverOutput][source]#

Retrieve the k relevant documents.

Parameters:
  • query_or_queries (RetrieverStrQueriesType) – a string or a list of strings.

  • top_k (Optional[int], optional) – top k documents to fetch. Defaults to None.

  • model_kwargs (Dict[str, Any], optional) – the model kwargs. You can switch to another model provided by the same model client without reinitializing the retriever. Defaults to {}.

Returns:

the developers should be aware that the returned LLMRetrieverOutputType is actually a list of GeneratorOutput(GeneratorOutput), post processing is required depends on how you instruct the model to output in the prompt and what output_processors you set up. E.g. If the prompt is to output a list of indices and the output_processors is ListParser(), then it return: GeneratorOutput(data=[indices], error=None, raw_response=’[indices]’)

Return type:

RetrieverOutputType