langchain.retrievers.self_query.base.SelfQueryRetriever

Note

SelfQueryRetriever implements the standard Runnable Interface. 🏃

class langchain.retrievers.self_query.base.SelfQueryRetriever[source]

Bases: BaseRetriever

Retriever that uses a vector store and an LLM to generate the vector store queries.

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 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 query_constructor: Runnable[dict, StructuredQuery] [Required] (alias 'llm_chain')

The query constructor chain for generating the vector store queries.

llm_chain is legacy name kept for backwards compatibility.

param search_kwargs: dict [Optional]

Keyword arguments to pass in to the vector store search.

param search_type: str = 'similarity'

The search type to perform on the vector store.

param structured_query_translator: Visitor [Required]

Translator for turning internal query language into vectorstore search params.

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 use_original_query: bool = False

Use original query instead of the revised new query from LLM

param vectorstore: VectorStore [Required]

The underlying vector store from which documents will be retrieved.

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

[Deprecated] Asynchronously get documents relevant to a query.

Users should favor using .ainvoke or .abatch rather than aget_relevant_documents directly.

Parameters
  • query (str) – string to find relevant documents for

  • callbacks (Callbacks) – Callback manager or list of callbacks

  • tags (Optional[List[str]]) – 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.

  • metadata (Optional[Dict[str, Any]]) – 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.

  • run_name (Optional[str]) – Optional name for the run.

  • kwargs (Any) –

Returns

List of relevant documents

Return type

List[Document]

Notes

Deprecated since version langchain-core==0.1.46: Use ainvoke instead.

classmethod from_llm(llm: BaseLanguageModel, vectorstore: VectorStore, document_contents: str, metadata_field_info: Sequence[Union[AttributeInfo, dict]], structured_query_translator: Optional[Visitor] = None, chain_kwargs: Optional[Dict] = None, enable_limit: bool = False, use_original_query: bool = False, **kwargs: Any) SelfQueryRetriever[source]
Parameters
  • llm (BaseLanguageModel) –

  • vectorstore (VectorStore) –

  • document_contents (str) –

  • metadata_field_info (Sequence[Union[AttributeInfo, dict]]) –

  • structured_query_translator (Optional[Visitor]) –

  • chain_kwargs (Optional[Dict]) –

  • enable_limit (bool) –

  • use_original_query (bool) –

  • kwargs (Any) –

Return type

SelfQueryRetriever

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

[Deprecated] Retrieve documents relevant to a query.

Users should favor using .invoke or .batch rather than get_relevant_documents directly.

Parameters
  • query (str) – string to find relevant documents for

  • callbacks (Callbacks) – Callback manager or list of callbacks

  • tags (Optional[List[str]]) – 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.

  • metadata (Optional[Dict[str, Any]]) – 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.

  • run_name (Optional[str]) – Optional name for the run.

  • kwargs (Any) –

Returns

List of relevant documents

Return type

List[Document]

Notes

Deprecated since version langchain-core==0.1.46: Use invoke instead.

property llm_chain: Runnable

llm_chain is legacy name kept for backwards compatibility.

Examples using SelfQueryRetriever