a
    ”˜ëj!  ã                	   @  s   d 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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 dd	lmZ dd
lmZ G dd„ deƒZd!ddddddddœdd„Zd"ddddddddœdd„Zddddœddddddddœdd „ZdS )#zRetriever tool.é    )Úannotations)Úpartial)ÚTYPE_CHECKINGÚLiteralÚOptionalÚUnion)Ú	BaseModelÚField)ÚBasePromptTemplateÚPromptTemplateÚaformat_documentÚformat_document)ÚTool)Ú	Callbacks)ÚDocument)ÚBaseRetrieverc                   @  s$   e Zd ZU dZedd�Zded< dS )ÚRetrieverInputzInput to the retriever.zquery to look up in retriever)ÚdescriptionÚstrÚqueryN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   Ú__annotations__© r   r   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/tools/retriever.pyr      s   
r   NÚcontentr   r   r
   r   z*Literal['content', 'content_and_artifact']z&Union[str, tuple[str, list[Document]]])r   Ú	retrieverÚdocument_promptÚdocument_separatorÚ	callbacksÚresponse_formatÚreturnc                   s>   |j | d|id�}| ‡ fdd„|D ƒ¡}|dkr:||fS |S )Nr!   ©Úconfigc                 3  s   | ]}t |ˆ ƒV  qd S ©N)r   ©Ú.0Údoc©r   r   r   Ú	<genexpr>'   s   z*_get_relevant_documents.<locals>.<genexpr>Úcontent_and_artifact)ZinvokeÚjoin©r   r   r   r    r!   r"   Zdocsr   r   r*   r   Ú_get_relevant_documents   s    ÿr/   c                 ƒ  sJ   |j | d|id�I d H }| ‡ fdd„|D ƒI d H ¡}|dkrF||fS |S )Nr!   r$   c                 “  s   g | ]}t |ˆ ƒI d H ‘qS r&   )r   r'   r*   r   r   Ú
<listcomp>:   ó    z,_aget_relevant_documents.<locals>.<listcomp>r,   )Zainvoker-   r.   r   r*   r   Ú_aget_relevant_documents0   s    ÿr2   z

)r   r    r"   zOptional[BasePromptTemplate]r   )r   Únamer   r   r    r"   r#   c                C  sF   |pt  d¡}tt| |||d�}tt| |||d�}t||||t|d�S )aé  Create a tool to do retrieval of documents.

    Args:
        retriever: The retriever to use for the retrieval
        name: The name for the tool. This will be passed to the language model,
            so should be unique and somewhat descriptive.
        description: The description for the tool. This will be passed to the language
            model, so should be descriptive.
        document_prompt: The prompt to use for the document. Defaults to None.
        document_separator: The separator to use between documents. Defaults to "\n\n".
        response_format: The tool response format. If "content" then the output of
            the tool is interpreted as the contents of a ToolMessage. If
            "content_and_artifact" then the output is expected to be a two-tuple
            corresponding to the (content, artifact) of a ToolMessage (artifact
            being a list of documents in this case). Defaults to "content".

    Returns:
        Tool class to pass to an agent.
    z{page_content})r   r   r    r"   )r3   r   ÚfuncÚ	coroutineZargs_schemar"   )r   Zfrom_templater   r/   r2   r   r   )r   r3   r   r   r    r"   r4   Zafuncr   r   r   Úcreate_retriever_toolC   s.    ûûúr6   )Nr   )Nr   )r   Ú
__future__r   Ú	functoolsr   Útypingr   r   r   r   Zpydanticr   r	   Zlangchain_core.promptsr
   r   r   r   Zlangchain_core.tools.simpler   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.retrieversr   r   r/   r2   r6   r   r   r   r   Ú<module>   s*     ú  úù