a
    •˜ëja  ã                   @  sÒ  d Z ddl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 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 ddlmZmZm Z  ddl!m"Z# G dd„ deƒZ$e#dddddœdddddddddœdd „Z%e j"e j&d!d"d#œddddd$dd%d&œd'd(„Z'ej(ej)ej&d!dd"d"d"d"d)d*œ
ddddddd+d,d,d$d-dd.d/œd0d1„Z*ej+ej,ej&d2d3d"d"d4œddddddd,d$dd5d6œ
d7d8„Z-e
d9d:d;d<�dBddd$dd>d?œd@dA„ƒZ.d"S )Cz,Load question answering with sources chains.é    )Úannotations)ÚMapping)ÚAnyÚOptionalÚProtocol)Ú
deprecated)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚBaseCombineDocumentsChain)ÚMapReduceDocumentsChain)ÚMapRerankDocumentsChain)ÚReduceDocumentsChain)ÚRefineDocumentsChain)ÚStuffDocumentsChain)ÚLLMChain)Úmap_reduce_promptÚrefine_promptsÚstuff_prompt)ÚPROMPTc                   @  s"   e Zd ZdZddddœdd„ZdS )	ÚLoadingCallablez2Interface for loading the combine documents chain.r   r   r
   )ÚllmÚkwargsÚreturnc                 K  s   dS )z-Callable to load the combine documents chain.N© )Úselfr   r   r   r   úg/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/qa_with_sources/loading.pyÚ__call__    s    zLoadingCallable.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   FÚcontextZscoreZanswer)ÚpromptÚverboseÚdocument_variable_nameÚrank_keyÚ
answer_keyr   r	   ÚboolÚstrr   r   )r   r"   r#   r$   r%   r&   r   r   c                K  s&   t | ||d�}tf ||||dœ|¤ŽS )N©r   r"   r#   )Ú	llm_chainr%   r&   r$   )r   r   )r   r"   r#   r$   r%   r&   r   r*   r   r   r   Ú_load_map_rerank_chain(   s    
üûr+   Z	summariesN)r"   Údocument_promptr$   r#   zOptional[bool]r   )r   r"   r,   r$   r#   r   r   c                K  s&   t | ||d�}tf ||||dœ|¤ŽS )Nr)   ©r*   r$   r,   r#   )r   r   )r   r"   r,   r$   r#   r   r*   r   r   r   Ú_load_stuff_chain<   s    	üûr.   i¸  )
Úquestion_promptÚcombine_promptr,   Úcombine_document_variable_nameÚ!map_reduce_document_variable_nameÚcollapse_promptÚ
reduce_llmÚcollapse_llmr#   Ú	token_maxzOptional[BasePromptTemplate]zOptional[BaseLanguageModel]Úintr   )r   r/   r0   r,   r1   r2   r3   r4   r5   r#   r6   r   r   c       
         K  sž   t | ||	d�}|p| }t |||	d�}t||||	d�}|d u rVd }|d urvd}t|ƒ‚n |p\| }tt |||	d�||d�}t|||
|	d�}tf ||||	dœ|¤ŽS )Nr)   r-   zfcollapse_llm provided, but collapse_prompt was not: please provide one or stop providing collapse_llm.)r*   r$   r,   )Úcombine_documents_chainZcollapse_documents_chainr6   r#   )r*   Úreduce_documents_chainr$   r#   )r   r   Ú
ValueErrorr   r   )r   r/   r0   r,   r1   r2   r3   r4   r5   r#   r6   r   Z	map_chainZ_reduce_llmZreduce_chainr8   Zcollapse_chainÚmsgZ_collapse_llmr9   r   r   r   Ú_load_map_reduce_chainO   sN    üÿ
ýù	üüûr<   Zcontext_strZexisting_answer)r/   Úrefine_promptr,   r$   Úinitial_response_nameÚ
refine_llmr#   r   )
r   r/   r=   r,   r$   r>   r?   r#   r   r   c             	   K  s@   t | ||d�}	|p| }
t |
||d�}tf |	|||||dœ|¤ŽS )Nr)   )Zinitial_llm_chainZrefine_llm_chainr$   r>   r,   r#   )r   r   )r   r/   r=   r,   r$   r>   r?   r#   r   Zinitial_chainZ_refine_llmZrefine_chainr   r   r   Ú_load_refine_chain‰   s    úùr@   z0.2.13z1.0aD  This function is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/docs/how_to/qa_sources/
See also the following migration guides for replacements based on `chain_type`:
stuff: https://python.langchain.com/docs/versions/migrating_chains/stuff_docs_chain
map_reduce: https://python.langchain.com/docs/versions/migrating_chains/map_reduce_chain
refine: https://python.langchain.com/docs/versions/migrating_chains/refine_chain
map_rerank: https://python.langchain.com/docs/versions/migrating_chains/map_rerank_docs_chain
)ZsinceZremovalÚmessageÚstuffr
   )r   Ú
chain_typer#   r   r   c                 K  sN   t tttdœ}||vr2d|› d| ¡ › �}t|ƒ‚|| }|| fd|i|¤ŽS )a	  Load a question answering with sources chain.

    Args:
        llm: Language Model to use in the chain.
        chain_type: Type of document combining chain to use. Should be one of "stuff",
            "map_reduce", "refine" and "map_rerank".
        verbose: Whether chains should be run in verbose mode or not. Note that this
            applies to all chains that make up the final chain.
        **kwargs: Additional keyword arguments.

    Returns:
        A chain to use for question answering with sources.
    )rB   Z
map_reduceZrefineZ
map_rerankzGot unsupported chain type: z. Should be one of r#   )r.   r<   r@   r+   Úkeysr:   )r   rC   r#   r   Zloader_mappingr;   Z_funcr   r   r   Úload_qa_with_sources_chain£   s    #üÿÿrE   )rB   N)/r    Ú
__future__r   Úcollections.abcr   Útypingr   r   r   Zlangchain_core._apir   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr	   Z'langchain.chains.combine_documents.baser
   Z-langchain.chains.combine_documents.map_reducer   Z-langchain.chains.combine_documents.map_rerankr   Z)langchain.chains.combine_documents.reducer   Z)langchain.chains.combine_documents.refiner   Z(langchain.chains.combine_documents.stuffr   Zlangchain.chains.llmr   Z langchain.chains.qa_with_sourcesr   r   r   Z5langchain.chains.question_answering.map_rerank_promptr   ZMAP_RERANK_PROMPTr   r+   ZEXAMPLE_PROMPTr.   ZQUESTION_PROMPTZCOMBINE_PROMPTr<   ZDEFAULT_TEXT_QA_PROMPTZDEFAULT_REFINE_PROMPTr@   rE   r   r   r   r   Ú<module>   sl   ù úô*=÷$ü  ý