o
    Zh                  
   @  sf  d Z ddlmZ ddlmZ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fdMd$d%Z$ej!ej%d&d'fdNd+d,Z&ej'ej(ej%d&dd'd'd'd'd-f
dOd:d;Z)ej*ej+ej%d<d=d'd'fdPdBdCZ,e	dDdEdFdG	H	'dQdRdKdLZ-d'S )Sz,Load question answering with sources chains.    )annotations)AnyMapping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	Zd
S )LoadingCallablez2Interface for loading the combine documents chain.llmr   kwargsr   returnr
   c                 K  s   dS )z-Callable to load the combine documents chain.N )selfr   r   r   r   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain/chains/qa_with_sources/loading.py__call__   s    zLoadingCallable.__call__N)r   r   r   r   r   r
   )__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   FcontextZscoreZanswerr   r   promptr	   verbosebooldocument_variable_namestrrank_key
answer_keyr   r   r   r   c                 K  s&   t | ||d}td||||d|S )Nr   r"   r#   )	llm_chainr'   r(   r%   r   )r   r   )r   r"   r#   r%   r'   r(   r   r*   r   r   r   _load_map_rerank_chain%   s   	r+   Z	summariesNdocument_promptOptional[bool]r   c                 K  s&   t | ||d}td||||d|S )Nr)   r*   r%   r,   r#   r   )r   r   )r   r"   r,   r%   r#   r   r*   r   r   r   _load_stuff_chain8   s   r/   i  question_promptcombine_promptcombine_document_variable_name!map_reduce_document_variable_namecollapse_promptOptional[BasePromptTemplate]
reduce_llmOptional[BaseLanguageModel]collapse_llm	token_maxintr   c                 K  s   t | ||	d}|p
| }t |||	d}t||||	d}|d u r)d }|d ur(tdn|p,| }tt |||	d||d}t|||
|	d}td||||	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_chainr9   r#   )r*   reduce_documents_chainr%   r#   r   )r   r   
ValueErrorr   r   )r   r0   r1   r,   r2   r3   r4   r6   r8   r#   r9   r   Z	map_chainZ_reduce_llmZreduce_chainr;   Zcollapse_chainZ_collapse_llmr<   r   r   r   _load_map_reduce_chainJ   sP   	r>   Zcontext_strZexisting_answerrefine_promptinitial_response_name
refine_llmr   c              	   K  s@   t | ||d}	|p
| }
t |
||d}td|	|||||d|S )Nr)   )Zinitial_llm_chainZrefine_llm_chainr%   r@   r,   r#   r   )r   r   )r   r0   r?   r,   r%   r@   rA   r#   r   Zinitial_chainZ_refine_llmZrefine_chainr   r   r   _load_refine_chain   s   rB   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stuff
chain_typer
   c                 K  sJ   t tttd}||vrtd| d|  || }|| 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.

    Returns:
        A chain to use for question answering with sources.
    )rD   Z
map_reduceZrefineZ
map_rerankzGot unsupported chain type: z. Should be one of r#   )r/   r>   rB   r+   r=   keys)r   rE   r#   r   Zloader_mappingZ_funcr   r   r   load_qa_with_sources_chain   s   "rG   )r   r   r"   r	   r#   r$   r%   r&   r'   r&   r(   r&   r   r   r   r   )r   r   r"   r	   r,   r	   r%   r&   r#   r-   r   r   r   r   )r   r   r0   r	   r1   r	   r,   r	   r2   r&   r3   r&   r4   r5   r6   r7   r8   r7   r#   r-   r9   r:   r   r   r   r   )r   r   r0   r	   r?   r	   r,   r	   r%   r&   r@   r&   rA   r7   r#   r-   r   r   r   r   )rD   N)
r   r   rE   r&   r#   r-   r   r   r   r
   ).r    
__future__r   typingr   r   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_PROMPTrB   rG   r   r   r   r   <module>   sl    :