langchain.retrievers.bm25.BM25Retriever

class langchain.retrievers.bm25.BM25Retriever(*, tags: ~typing.Optional[~typing.List[str]] = None, metadata: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, vectorizer: ~typing.Any = None, docs: ~typing.List[~langchain.schema.document.Document], k: int = 4, preprocess_func: ~typing.Callable[[str], ~typing.List[str]] = <function default_preprocessing_func>)[source]

Bases: BaseRetriever

BM25 Retriever without elastic search.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param docs: List[langchain.schema.document.Document] [Required]

List of documents.

param k: int = 4

Number of documents to return.

param metadata: Optional[Dict[str, Any]] = None

Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

param preprocess_func: Callable[[str], List[str]] = <function default_preprocessing_func>

Preprocessing function to use on the text before BM25 vectorization.

param tags: Optional[List[str]] = None

Optional list of tags associated with the retriever. Defaults to None These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

param vectorizer: Any = None

BM25 vectorizer.

async aget_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]

Asynchronously get documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

async ainvoke(input: str, config: Optional[RunnableConfig] = None) List[Document]
classmethod from_documents(documents: ~typing.Iterable[~langchain.schema.document.Document], *, bm25_params: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, preprocess_func: ~typing.Callable[[str], ~typing.List[str]] = <function default_preprocessing_func>, **kwargs: ~typing.Any) BM25Retriever[source]

Create a BM25Retriever from a list of Documents. :param documents: A list of Documents to vectorize. :param bm25_params: Parameters to pass to the BM25 vectorizer. :param preprocess_func: A function to preprocess each text before vectorization. :param **kwargs: Any other arguments to pass to the retriever.

Returns

A BM25Retriever instance.

classmethod from_texts(texts: ~typing.Iterable[str], metadatas: ~typing.Optional[~typing.Iterable[dict]] = None, bm25_params: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, preprocess_func: ~typing.Callable[[str], ~typing.List[str]] = <function default_preprocessing_func>, **kwargs: ~typing.Any) BM25Retriever[source]

Create a BM25Retriever from a list of texts. :param texts: A list of texts to vectorize. :param metadatas: A list of metadata dicts to associate with each text. :param bm25_params: Parameters to pass to the BM25 vectorizer. :param preprocess_func: A function to preprocess each text before vectorization. :param **kwargs: Any other arguments to pass to the retriever.

Returns

A BM25Retriever instance.

get_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]

Retrieve documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

invoke(input: str, config: Optional[RunnableConfig] = None) List[Document]
to_json() Union[SerializedConstructor, SerializedNotImplemented]
to_json_not_implemented() SerializedNotImplemented
property lc_attributes: Dict

Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.

property lc_namespace: List[str]

Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]

property lc_secrets: Dict[str, str]

Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}

property lc_serializable: bool

Return whether or not the class is serializable.

model Config[source]

Bases: object

Configuration for this pydantic object.

arbitrary_types_allowed = True

Examples using BM25Retriever