
    Ig>                       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 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
r
ddlmZmZmZ e Z!e"e   Z#ee!e#f   Z$eee#f   Z% G d ded      Z& G d dee!e#f   e      Z'y)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)	TypedDict)
deprecated)Document)RunnableRunnableConfigRunnableSerializableensure_config)run_in_executor)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRun	Callbacksc                  @    e Zd ZU dZded<   	 ded<   	 ded<   	 ded<   y)	LangSmithRetrieverParamsz!LangSmith parameters for tracing.strls_retriever_nameOptional[str]ls_vector_store_providerls_embedding_providerls_embedding_modelN)__name__
__module____qualname____doc____annotations__     V/var/www/html/answerous/venv/lib/python3.12/site-packages/langchain_core/retrievers.pyr   r   7   s)    +++ ((%%r$   r   F)totalc                  v    e Zd ZU dZ ed      ZdZded<   dZded<   dZ	d	ed
<   	 dZ
ded<   	 d fdZddZ	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 dd       Z	 	 	 	 	 	 d dZ eddd      ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d!d       Z eddd      ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d!d       Z x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 import Document, BaseRetriever
            from typing import List

            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)arbitrary_types_allowedFbool_new_arg_supported_expects_other_argsNOptional[list[str]]tagsOptional[dict[str, Any]]metadatac                   t        |   d	i | | j                  t        j                  k7  rDt	        j
                  dt        d       | j                  }t        j                  | _        || _        t        | d      ra| j                  t        j                  k7  rDt	        j
                  dt        d       | j                  }t        j                  | _	        || _
        t        | j                        j                  }|j                  d      d u| _        t        t!        |j#                               h dz
        dkD  | _        y )
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>   selfqueryr5   r   r#   )super__init_subclass__get_relevant_documentsr(   warningswarnDeprecationWarning_get_relevant_documentshasattrr4   _aget_relevant_documentsr   
parametersgetr+   lensetkeysr,   )clskwargsswapaswaprA   	__class__s        r%   r9   zBaseRetriever.__init_subclass__   s   !+F+ %%)M)MMMM7"	 --D44 & +/C'C23++}/T/TTMM8"	 //E55 ' ,1C(s::;FF
!+!>d!J JOO%&)IIJQN 	r$   c                    | j                         }|j                  d      r|dd }n|j                  d      r|dd }|j                         }t	        |      }|S )z Get standard params for tracing.	Retriever	   Ni)r   )get_name
startswithendswithlowerr   )r6   rG   default_retriever_name	ls_paramss       r%   _get_ls_paramszBaseRetriever._get_ls_params   sf     "&!,,[9%;AB%?"#,,[9%;CR%@"!7!=!=!?,?UV	r$   c           	        ddl m} t        |      }i |j                  d      xs i  | j                  di |}|j                  |j                  d      d|j                  dd      |j                  d      | j                  || j                  	      }|j                  d||j                  d
      xs | j                         |j                  dd            }	 | j                  r|ni }| j                  r | j                  |fd|i|}	n | j                  |fi |}	|j                  |	       |	S # t        $ r}
|j!                  |
       |
d}
~
ww xY w)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   )CallbackManagerr0   	callbacksNverboseFr.   rX   inheritable_tags
local_tagsinheritable_metadatalocal_metadatarun_namerun_idnamer_   r5   r#   ) langchain_core.callbacks.managerrV   r   rB   rT   	configurer.   r0   on_retriever_startrN   popr,   r+   r>   on_retriever_end	Exceptionon_retriever_error)r6   inputconfigrG   rV   r\   callback_managerr5   _kwargsresultes              r%   invokezBaseRetriever.invoke   sv   * 	Ev& 
zz*%+ 
!d!!+F+ 
 +44JJ{#JJy%0#ZZ/yy!5== 5 
 '99J':4==?::h-	 : 
	 $ 8 8fbG&&555'26= 655eGwG
 (( M  	**1-G	s   AD4 4	E=EEc           	       K   ddl m} t        |      }i |j                  d      xs i  | j                  di |}|j                  |j                  d      d|j                  dd      |j                  d      | j                  || j                  	      }|j                  d||j                  d
      xs | j                         |j                  dd             d{   }	 | j                  r|ni }| j                  r | j                  |fd|i| d{   }	n | j                  |fi | d{   }	|j                  |	       d{    |	S 7 u7 =7 #7 # t        $ r!}
|j!                  |
       d{  7   |
d}
~
ww xY ww)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   )AsyncCallbackManagerr0   rW   NrX   Fr.   rY   r^   r_   r`   r5   r#   )rb   rq   r   rB   rT   rc   r.   r0   rd   rN   re   r,   r+   r@   rf   rg   rh   )r6   ri   rj   rG   rq   r\   rk   r5   rl   rm   rn   s              r%   ainvokezBaseRetriever.ainvoke  s    0 	Jv& 
zz*%+ 
!d!!+F+ 
 099JJ{#JJy%0#ZZ/yy!5== : 
 -??J':4==?::h-	 @ 
 
	 $ 8 8fbG&&<t<< '2 6=    =t<<UNgNN
 ..   M+
 O
	  	00333G	sx   CF E!F&4E EE 6E7E ;FEFE E F	F'F;E><FFFc                    y)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#   r6   r7   r5   s      r%   r>   z%BaseRetriever._get_relevant_documentsD  s    r$   c               l   K   t        d| j                  ||j                                d{   S 7 w)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)r5   )r   r>   get_syncrt   s      r%   r@   z&BaseRetriever._aget_relevant_documentsQ  s:      %((#,,.	
 
 	
 
s   +424z0.1.46ro   z1.0)sincealternativeremoval)rW   r.   r0   r^   c               f    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.
        rW   r.   r0   r^   )ro   r6   r7   rW   r.   r0   r^   rG   rj   s           r%   r:   z$BaseRetriever.get_relevant_documentsc  sU    B "$"+F;!F6N!)F:!)F:t{{5&3F33r$   rr   c                  K   i }|r||d<   |r||d<   |r||d<   |r||d<    | j                   ||fi | d{   S 7 w)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.
        rW   r.   r0   r^   N)rr   r{   s           r%   r4   z%BaseRetriever.aget_relevant_documents  s_     B "$"+F;!F6N!)F:!)F:!T\\%:6::::s   6?=?)rG   r   returnNone)rG   r   r}   r   )N)ri   r   rj   zOptional[RunnableConfig]rG   r   r}   list[Document])r7   r   r5   r   r}   r   )r7   r   r5   r   r}   r   )r7   r   rW   r   r.   r-   r0   r/   r^   r   rG   r   r}   r   )r   r   r    r!   r
   model_configr+   r"   r,   r.   r0   r9   rT   ro   rr   r   r>   r@   r   r:   r4   __classcell__)rJ   s   @r%   r(   r(   D   s   :x  $L  %$ %% $D
$ *.H&-$
L >B::"::MP:	:~ ,0== )= 	=
 
=~ 

*H
	
 


*M
	
$ hHeD
  $$(-1"&)4)4 	)4
 ")4 +)4  )4 )4 
)4 E)4V hIuE
  $$(-1"&);); 	);
 "); +);  ); ); 
); F);r$   r(   )(r!   
__future__r   r;   abcr   r   inspectr   typingr   r   r	   pydanticr
   typing_extensionsr   langchain_core._apir   langchain_core.documentsr   langchain_core.runnablesr   r   r   r   langchain_core.runnables.configr   rb   r   r   r   r   RetrieverInputlistRetrieverOutputRetrieverLikeRetrieverOutputLiker   r(   r#   r$   r%   <module>r      s   * #  #  / /  ' * -  <  x.89sO34 
y 
u;()HI3 u;r$   