retriever#

The base class for all retrievers who in particular retrieve documents from a given database.

Classes

Retriever(*args, **kwargs)

The base class for all retrievers.

class Retriever(*args, **kwargs)[source]#

Bases: GradComponent, Generic[RetrieverDocumentType, RetrieverQueryType]

The base class for all retrievers.

Retriever will manage its own index and retrieve in format of RetrieverOutput

Parameters:
  • indexed (bool, optional) – whether the retriever has an index. Defaults to False.

  • index_keys (List[str], optional) – attributes that define the index that can be used to restore the retriever. Defaults to [].

The key method build_index_from_documents() is the method to build the index from the documents. documents is a sequence of any type of document. With document_map_func, you can map the document of Any type to the specific type RetrieverDocumentType that the retriever expects.

note: To get the state of the retriever, leverage the :methd: “from_dict” and “to_dict” methods of the base class Component.

indexed: bool = False#
index_keys: List[str] = []#
name: str = 'Retriever'#
top_k: int#
reset_index()[source]#

Initialize/reset any attributes/states for the index.

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

Built index from the [document_map_func(doc) for doc in documents].

save_to_file(path: str)[source]#

Save the state, including the index to a file.

Optional for subclass to implement a default persistence method. Subclass can leverge component’s to_dict method to get the states and choose to save them in any file format.

classmethod load_from_file(path: str)[source]#

Load the state, including index from a file to restore the retriever.

Subclass can leverge component’s from_dict method to restore the states from the file.

call(input: RetrieverQueryType | Sequence[RetrieverQueryType], top_k: int | None = None, **kwargs) List[RetrieverOutput][source]#
async acall(input: RetrieverQueryType | Sequence[RetrieverQueryType], top_k: int | None = None, **kwargs) List[RetrieverOutput][source]#

Implement this for your async call.

forward(input: RetrieverQueryType | Sequence[RetrieverQueryType] | Parameter, top_k: int | None = None, **kwargs) Parameter[source]#

Customized forward on top of the GradComponent forward method.

To track the input as Parameters and set the parameter type as RETRIEVER_OUTPUT in the response.

backward(response: Parameter, id: str | None = None, backward_engine: Generator | None = None)[source]#

Backward the response to pass the score to predecessors