o
    Zh_A                     @  s  d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZ ddlmZ e
rbddl m!Z!m"Z" e#Z$e%e Z&ee$e&f Z'eee&f Z(G dd deddZ)G dd dee$e&f eZ*dS )a  **Retriever** class returns Documents given a text **query**.

It is more general than a vector store. A retriever does not need to be able to
store documents, only to return (or retrieve) it. Vector stores can be used as
the backbone of a retriever, but there are other types of retrievers as well.

**Class hierarchy:**

.. code-block::

    BaseRetriever --> <name>Retriever  # Examples: ArxivRetriever, MergerRetriever

**Main helpers:**

.. code-block::

    RetrieverInput, RetrieverOutput, RetrieverLike, RetrieverOutputLike,
    Document, Serializable, Callbacks,
    CallbackManagerForRetrieverRun, AsyncCallbackManagerForRetrieverRun
    )annotationsN)ABCabstractmethod)	signature)TYPE_CHECKINGAnyOptional)
ConfigDict)Self	TypedDictoverride)
deprecated)	Callbacks)Document)RunnableRunnableConfigRunnableSerializableensure_config)run_in_executor)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRunc                   @  s8   e Zd ZU dZded< 	 ded< 	 ded< 	 ded< dS )	LangSmithRetrieverParamsz!LangSmith parameters for tracing.strls_retriever_nameOptional[str]Zls_vector_store_providerZls_embedding_providerZls_embedding_modelN)__name__
__module____qualname____doc____annotations__ r    r    P/var/www/html/lang_env/lib/python3.10/site-packages/langchain_core/retrievers.pyr   7   s   
 r   F)totalc                      s   e Zd ZU dZeddZdZded< dZded< dZ	d	ed
< 	 dZ
ded< 	 ed6 fddZd7ddZe	d8d9ddZe	d8d9ddZed:d#d$Zd;d&d'Zed(d)d*d+ddddd,d<d1d2Zed(d3d*d+ddddd,d<d4d5Z  ZS )=BaseRetrievera	  Abstract base class for a Document retrieval system.

    A retrieval system is defined as something that can take string queries and return
    the most 'relevant' Documents from some source.

    Usage:

    A retriever follows the standard Runnable interface, and should be used
    via the standard Runnable methods of `invoke`, `ainvoke`, `batch`, `abatch`.

    Implementation:

    When implementing a custom retriever, the class should implement
    the `_get_relevant_documents` method to define the logic for retrieving documents.

    Optionally, an async native implementations can be provided by overriding the
    `_aget_relevant_documents` method.

    Example: A retriever that returns the first 5 documents from a list of documents

        .. code-block:: python

            from langchain_core.documents import Document
            from langchain_core.retrievers import BaseRetriever

            class SimpleRetriever(BaseRetriever):
                docs: list[Document]
                k: int = 5

                def _get_relevant_documents(self, query: str) -> list[Document]:
                    """Return the first k documents from the list of documents"""
                    return self.docs[:self.k]

                async def _aget_relevant_documents(self, query: str) -> list[Document]:
                    """(Optional) async native implementation."""
                    return self.docs[:self.k]

    Example: A simple retriever based on a scikit-learn vectorizer

        .. code-block:: python

            from sklearn.metrics.pairwise import cosine_similarity

            class TFIDFRetriever(BaseRetriever, BaseModel):
                vectorizer: Any
                docs: list[Document]
                tfidf_array: Any
                k: int = 4

                class Config:
                    arbitrary_types_allowed = True

                def _get_relevant_documents(self, query: str) -> list[Document]:
                    # Ip -- (n_docs,x), Op -- (n_docs,n_Feats)
                    query_vec = self.vectorizer.transform([query])
                    # Op -- (n_docs,1) -- Cosine Sim with each doc
                    results = cosine_similarity(self.tfidf_array, query_vec).reshape((-1,))
                    return [self.docs[i] for i in results.argsort()[-self.k :][::-1]]
    T)Zarbitrary_types_allowedFbool_new_arg_supported_expects_other_argsNOptional[list[str]]tagsOptional[dict[str, Any]]metadatakwargsr   returnNonec                   s   t  jdi | | jtjkr!tjdtdd | j}tj| _|| _t| dr>| j	tj	kr>tjdtdd | j	}tj	| _	|| _
t| jj}|dd u| _| js]| j
tj
kr]ddd}|| _
tt| h d dk| _d S )NzgRetrievers must implement abstract `_get_relevant_documents` method instead of `get_relevant_documents`   )
stacklevelaget_relevant_documentsziRetrievers must implement abstract `_aget_relevant_documents` method instead of `aget_relevant_documents`run_managerselfr
   queryr   r,   list[Document]c                   s   t d | j|I d H S N)r   _get_relevant_documents)r2   r3   r    r    r!   _aget_relevant_documents   s   zABaseRetriever.__init_subclass__.<locals>._aget_relevant_documents>   r2   r1   r3   r   r    )r2   r
   r3   r   r,   r4   )super__init_subclass__get_relevant_documentsr#   warningswarnDeprecationWarningr6   hasattrr0   r7   r   
parametersgetr%   lensetkeysr&   )clsr+   ZswapZaswapr?   r7   	__class__r    r!   r9      s@   
zBaseRetriever.__init_subclass___kwargsr   c                 K  sH   |   }|dr|dd }n|dr|dd }| }t|dS )z Get standard params for tracing.Z	Retriever	   Ni)r   )get_name
startswithendswithlowerr   )r2   rG   Zdefault_retriever_namer    r    r!   _get_ls_params   s   


zBaseRetriever._get_ls_paramsinputr   configOptional[RunnableConfig]r4   c              
   K  s  ddl m} t|}i |dpi | jdi |}|j|dd|dd|d| j|| jd	}|jd||d
p?| 	 |
ddd}z!| jrM|ni }| jr^| j|fd|i|}	n	| j|fi |}	W n tyz }
 z||
  d}
~
ww ||	 |	S )a  Invoke the retriever to get relevant documents.

        Main entry point for synchronous retriever invocations.

        Args:
            input: The query string.
            config: Configuration for the retriever. Defaults to None.
            kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.

        Examples:

        .. code-block:: python

            retriever.invoke("query")
        r   )CallbackManagerr*   	callbacksNverboseFr(   rS   Zinheritable_tagsZ
local_tagsinheritable_metadataZlocal_metadatarun_namerun_idnamerW   r1   r    ) langchain_core.callbacks.managerrQ   r   r@   rM   	configurer(   r*   on_retriever_startrI   popr&   r%   r6   	Exceptionon_retriever_erroron_retriever_end)r2   rN   rO   r+   rQ   rU   callback_managerr1   rG   resulter    r    r!   invoke   sV   
	

zBaseRetriever.invokec              
     s$  ddl m} t|}i |dpi | jdi |}|j|dd|dd|d| j|| jd	}|jd||d
p@| 	 |
dddI dH }z'| jrQ|ni }| jre| j|fd|i|I dH }	n| j|fi |I dH }	W n ty }
 z	||
I dH   d}
~
ww ||	I dH  |	S )a  Asynchronously invoke the retriever to get relevant documents.

        Main entry point for asynchronous retriever invocations.

        Args:
            input: The query string.
            config: Configuration for the retriever. Defaults to None.
            kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.

        Examples:

        .. code-block:: python

            await retriever.ainvoke("query")
        r   )AsyncCallbackManagerr*   rR   NrS   Fr(   rT   rV   rW   rX   r1   r    )rZ   re   r   r@   rM   r[   r(   r*   r\   rI   r]   r&   r%   r7   r^   r_   r`   )r2   rN   rO   r+   re   rU   ra   r1   rG   rb   rc   r    r    r!   ainvoke  sX   
	

zBaseRetriever.ainvoker3   r1   r   c                C  s   dS )zGet documents relevant to a query.

        Args:
            query: String to find relevant documents for.
            run_manager: The callback handler to use.

        Returns:
            List of relevant documents.
        Nr    r2   r3   r1   r    r    r!   r6   Q  s    z%BaseRetriever._get_relevant_documentsr   c                  s   t d| j|| dI dH S )zAsynchronously get documents relevant to a query.

        Args:
            query: String to find relevant documents for
            run_manager: The callback handler to use
        Returns:
            List of relevant documents
        N)r1   )r   r6   Zget_syncrg   r    r    r!   r7   _  s   z&BaseRetriever._aget_relevant_documentsz0.1.46rd   z1.0)ZsincealternativeZremoval)rR   r(   r*   rV   rR   r   rV   r   c                K  sH   i }|r||d< |r||d< |r||d< |r||d< | j ||fi |S )a  Retrieve documents relevant to a query.

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

        Args:
            query: string to find relevant documents for.
            callbacks: Callback manager or list of callbacks. Defaults to None.
            tags: Optional list of tags associated with the retriever.
                These tags will be associated with each call to this retriever,
                and passed as arguments to the handlers defined in `callbacks`.
                Defaults to None.
            metadata: Optional metadata associated with the retriever.
                This metadata will be associated with each call to this retriever,
                and passed as arguments to the handlers defined in `callbacks`.
                Defaults to None.
            run_name: Optional name for the run. Defaults to None.
            kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.
        rR   r(   r*   rV   )rd   r2   r3   rR   r(   r*   rV   r+   rO   r    r    r!   r:   q  s   !z$BaseRetriever.get_relevant_documentsrf   c                  sP   i }|r	||d< |r||d< |r||d< |r||d< | j ||fi |I dH S )a  Asynchronously get documents relevant to a query.

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

        Args:
            query: string to find relevant documents for.
            callbacks: Callback manager or list of callbacks.
            tags: Optional list of tags associated with the retriever.
                These tags will be associated with each call to this retriever,
                and passed as arguments to the handlers defined in `callbacks`.
                Defaults to None.
            metadata: Optional metadata associated with the retriever.
                This metadata will be associated with each call to this retriever,
                and passed as arguments to the handlers defined in `callbacks`.
                Defaults to None.
            run_name: Optional name for the run. Defaults to None.
            kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.
        rR   r(   r*   rV   N)rf   ri   r    r    r!   r0     s   !z%BaseRetriever.aget_relevant_documents)r+   r   r,   r-   )rG   r   r,   r   r5   )rN   r   rO   rP   r+   r   r,   r4   )r3   r   r1   r   r,   r4   )r3   r   r1   r   r,   r4   )r3   r   rR   r   r(   r'   r*   r)   rV   r   r+   r   r,   r4   )r   r   r   r   r	   Zmodel_configr%   r   r&   r(   r*   r   r9   rM   rd   rf   r   r6   r7   r   r:   r0   __classcell__r    r    rE   r!   r#   D   sF   
 <
2<?
+r#   )+r   
__future__r   r;   abcr   r   inspectr   typingr   r   r   Zpydanticr	   Ztyping_extensionsr
   r   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.runnablesr   r   r   r   Zlangchain_core.runnables.configr   rZ   r   r   r   ZRetrieverInputlistZRetrieverOutputZRetrieverLikeZRetrieverOutputLiker   r#   r    r    r    r!   <module>   s*    