
    f3fiu+                       d Z ddlmZ ddlmZmZ ddlm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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 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.
    )annotations)ABCabstractmethod)	signature)TYPE_CHECKINGAny)
ConfigDict)Self	TypedDictoverride)AsyncCallbackManagerCallbackManager)Document)RunnableRunnableConfigRunnableSerializableensure_config)run_in_executor)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRunc                  @    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_namez
str | Nonels_vector_store_providerls_embedding_providerls_embedding_modelN)__name__
__module____qualname____doc____annotations__     W/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/retrievers.pyr   r   '   s)    +(( %%""r$   r   F)totalc                       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<   	 ed fd       ZddZe	 d	 	 	 	 	 	 	 dd       Ze	 d	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 dd       Z	 	 	 	 	 	 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 "Retriever that returns the first 5 documents from a list of documents"

        ```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 "Simple retriever based on a scikit-learn vectorizer"

        ```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_argsNzlist[str] | Nonetagszdict[str, Any] | Nonemetadatac                d   t        |   di | t        | j                        j                  }|j                  d      d u| _        | j                  s.| j                  t        j                  k(  r	 	 	 	 	 	 dd}|| _        t        t        |j                               h dz
        dkD  | _        y )Nrun_managerc                L   K   t        d | j                  |       d {   S 7 wN)r   _get_relevant_documents)selfquerys     r%   _aget_relevant_documentszABaseRetriever.__init_subclass__.<locals>._aget_relevant_documents   s$      -T43O3OQVWWWWs   $"$>   r4   r5   r0   r   r#   )r4   r
   r5   r   returnlist[Document])super__init_subclass__r   r3   
parametersgetr+   r6   r(   lensetkeysr,   )clskwargsr;   r6   	__class__s       r%   r:   zBaseRetriever.__init_subclass__   s    !+F+s::;FF
!+!>d!J&&,,0V0VVXX#&XX
 ,DC( 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   )r4   _kwargsdefault_retriever_names      r%   _get_ls_paramszBaseRetriever._get_ls_params   s_    !%!,,[9%;AB%?"#,,[9%;CR%@"!7!=!=!?':PQQr$   c           	        t        |      }i |j                  d      xs i  | j                  di |}t        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.
            **kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.

        Examples:
        ```python
        retriever.invoke("query")
        ```
        r.   	callbacksNverboseFr-   rO   inheritable_tags
local_tagsinheritable_metadatalocal_metadatarun_namerun_idnamerV   r0   r#   )r   r<   rL   r   	configurer-   r.   on_retriever_startrF   popr,   r+   r3   on_retriever_end	Exceptionon_retriever_error
r4   inputconfigrA   rS   callback_managerr0   kwargs_resultes
             r%   invokezBaseRetriever.invoke   sk   * v& 
zz*%+ 
!d!!+F+ 
 +44JJ{#JJy%0#ZZ/yy!5==
 '99J':4==?::h-	 : 
	 $ 8 8fbG&&555'26= 655eGwG
 (( M  	**1-	s   AD2 2	E;EEc           	       K   t        |      }i |j                  d      xs i  | j                  di |}t        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.
            **kwargs: Additional arguments to pass to the retriever.

        Returns:
            List of relevant documents.

        Examples:
        ```python
        await retriever.ainvoke("query")
        ```
        r.   rN   NrO   Fr-   rP   rU   rV   rW   r0   r#   )r   r<   rL   r   rY   r-   r.   rZ   rF   r[   r,   r+   r6   r\   r]   r^   r_   s
             r%   ainvokezBaseRetriever.ainvoke   s    0 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	sx   CFEF$4E EE 4E5E 9FEFE E F	F%F 9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#   r4   r5   r0   s      r%   r3   z%BaseRetriever._get_relevant_documents#  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)r0   )r   r3   get_syncrj   s      r%   r6   z&BaseRetriever._aget_relevant_documents1  s:      %((#,,.	
 
 	
 
s   +424)rA   r   r7   None)rJ   r   r7   r   r2   )r`   r   ra   zRunnableConfig | NonerA   r   r7   r8   )r5   r   r0   r   r7   r8   )r5   r   r0   r   r7   r8   )r   r   r    r!   r	   model_configr+   r"   r,   r-   r.   r   r:   rL   rf   rh   r   r3   r6   __classcell__)rB   s   @r%   r(   r(   4   s-   ;z  $L  %$ %%!D
! '+H#* 
 
*	R :>77"77JM7	7 7r  )-:: &: 	:
 
: :x *H	 

*M
	
r$   r(   N)'r!   
__future__r   abcr   r   inspectr   typingr   r   pydanticr	   typing_extensionsr
   r   r    langchain_core.callbacks.managerr   r   langchain_core.documentsr   langchain_core.runnablesr   r   r   r   langchain_core.runnables.configr   r   r   r   RetrieverInputlistRetrieverOutputRetrieverLikeRetrieverOutputLiker   r(   r#   r$   r%   <module>r      s    # #  %  7 7 R -  <
 x.89sO34 
y 
N
()HI3 N
r$   