o
    `+ im                     @  s  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
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& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ erddl0m1Z1 ddl2m3Z3 z
ddl4m5Z5m6Z6 W n e7y   ddd Z5dd!d"Z6Y nw d#Z8dd'd(Z9dd*d+Z:dd-d.Z;dd0d1Z<dd3d4Z=dd5d6Z>dd8d9Z?dd;d<Z@dd>d?ZAdd@dAZBddCdDZCddFdGZDddHdIZEddKdLZFddNdOZGddQdRZHddTdUZIddWdXZJddZd[ZKdd]d^ZLi d_eKd`e:dae9dbe>dce?dde@deeLdfeBdgeDdhe;die<dje=dkeAdleCdmeEdneFdoeIeGeHeJdpZMedqdrdsdtddvdwZNedqdrdsdtddzd{ZOdd}d~ZPdS )z!Functionality for loading chains.    )annotationsN)Path)TYPE_CHECKINGAnyUnion)
deprecated)_load_output_parserload_promptload_prompt_from_config)ReduceDocumentsChain)APIChain)Chain)MapReduceDocumentsChain)MapRerankDocumentsChain)RefineDocumentsChain)StuffDocumentsChain)HypotheticalDocumentEmbedder)LLMChain)LLMCheckerChain)LLMMathChain)QAWithSourcesChain)RetrievalQAWithSourcesChain)VectorDBQAWithSourcesChain)RetrievalQA
VectorDBQAGraphCypherQAChainLLMRequestsChain)load_llmload_llm_from_configargsr   kwargsreturnNonec                  O     d}t |)NzTo use this load_llm functionality you must install the langchain_community package. You can install it with `pip install langchain_community`ImportErrorr!   r"   msg r*   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain/chains/loading.pyr   *      r   c                  O  r%   )NzTo use this load_llm_from_config functionality you must install the langchain_community package. You can install it with `pip install langchain_community`r&   r(   r*   r*   r+   r    2   r,   r    zHhttps://raw.githubusercontent.com/hwchase17/langchain-hub/master/chains/configdictr   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|d| v r6|  d}t|}nd| v rBt|  d}nd}t|t|  td	||d| S )
z Load LLM chain from config dict.llmllm_path+One of `llm` or `llm_path` must be present.promptprompt_pathz1One of `prompt` or `prompt_path` must be present.r/   r2   Nr*   )popr    r   
ValueErrorr
   r	   r   r   )r-   r"   
llm_configr/   r)   prompt_configr2   r*   r*   r+   _load_llm_chain>   s    


r9   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|d|v r2| d}nd}t|td||d| S )	z;Load hypothetical document embedder chain from config dict.	llm_chainllm_chain_path7One of `llm_chain` or `llm_chain_path` must be present.
embeddingsz`embeddings` must be present.)r:   Zbase_embeddingsNr*   )r5   load_chain_from_config
load_chainr6   r   )r-   r"   llm_chain_configr:   r)   r=   r*   r*   r+   _load_hyde_chainV   s"   
rA   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|t|ts6d| }t|d| v rD|  d}t|}nd| v rPt|  d}nd}t|td	||d| S )
Nr:   r;   r<   Expected LLMChain, got document_promptdocument_prompt_pathzCOne of `document_prompt` or `document_prompt_path` must be present.)r:   rC   r*   )	r5   r>   r?   r6   
isinstancer   r
   r	   r   )r-   r"   r@   r:   r)   r8   rC   r*   r*   r+   _load_stuff_documents_chainl   s.   




rF   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|t|ts6d| }t|d| v rFt|  dfi |}nd| v rVt|  dfi |}nt| fi |}td||d| S )	Nr:   r;   r<   rB   reduce_documents_chainZreduce_documents_chain_path)r:   rG   r*   )r5   r>   r?   r6   rE   r   _load_reduce_documents_chainr   )r-   r"   r@   r:   r)   rG   r*   r*   r+    _load_map_reduce_documents_chain   s:   


rI   r   c                 K  sR  d }d }d| v r|  d}t|fi |}n8d| v r(|  d}t|fi |}n&d| v r8t|  dfi |}nd| v rHt|  dfi |}nd}t|d| v rg|  d}|d u r^d }nAt|fi |}n8d| v rwt|  dfi |}n(d| v r|  d}|d u rd }nt|fi |}nd	| v rt|  d	fi |}td||d
| S )Ncombine_documents_chainZcombine_document_chaincombine_documents_chain_pathZcombine_document_chain_pathSOne of `combine_documents_chain` or `combine_documents_chain_path` must be present.collapse_documents_chainZcollapse_documents_chain_pathZcollapse_document_chainZcollapse_document_chain_path)rJ   rM   r*   )r5   r>   r?   r6   r   )r-   r"   rJ   rM   Zcombine_document_chain_configr)   Zcollapse_document_chain_configr*   r*   r+   rH      s   



rH   c                 K  r%   )z$Load LLM Bash chain from config dicta   LLMBash Chain is not available through LangChain anymore. The relevant code can be found in langchain_experimental, but it is not appropriate for production usage due to security concerns. Please refer to langchain-experimental repository for more details.NotImplementedErrorr-   r"   r)   r*   r*   r+   _load_llm_bash_chain   s   rQ   r   c                 K  s2  d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|d| v r6|  d}t|}nd| v rAt|  d}d| v rO|  d}t|}nd| v rZt|  d}d| v rh|  d}	t|	}
nd	| v rst|  d	}
d
| v r|  d
}t|}nd| v rt|  d}td||||
|d| S )Nr/   r0   r1   create_draft_answer_promptZcreate_draft_answer_prompt_pathlist_assertions_promptZlist_assertions_prompt_pathcheck_assertions_promptZcheck_assertions_prompt_pathrevised_answer_promptZrevised_answer_prompt_path)r/   rR   rS   rT   rU   r*   )r5   r    r   r6   r
   r	   r   )r-   r"   r7   r/   r)   Z!create_draft_answer_prompt_configrR   Zlist_assertions_prompt_configrS   Zcheck_assertions_prompt_configrT   Zrevised_answer_prompt_configrU   r*   r*   r+   _load_llm_checker_chain   sX   






rV   r   c           	      K  s   d }d| v r|  d}t|fi |}n8d| v r$t|  dfi |}n(d| v r6|  d}t|fi |}nd| v rFt|  dfi |}nd}t|d| v rZ|  d}t|}nd| v ret|  d}|rqtd
||d| S td
||d	| S )Nr:   r;   r/   r0   r<   r2   r3   )r:   r2   r4   r*   )	r5   r>   r?   r    r   r6   r
   r	   r   )	r-   r"   r:   r@   r7   r/   r)   r8   r2   r*   r*   r+   _load_llm_math_chain.  s*   



rW   r   c                 K  b   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|tdd|i| S )Nr:   r;   r<   r*   )r5   r>   r?   r6   r   )r-   r"   r@   r:   r)   r*   r*   r+    _load_map_rerank_documents_chainJ  s   
rY   c                 K  r%   )NzPALChain is not available through LangChain anymore. The relevant code can be found in langchain_experimental, but it is not appropriate for production usage due to security concerns. Please refer to langchain-experimental repository for more details.rN   rP   r*   r*   r+   _load_pal_chainY  s   rZ   r   c           	      K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}nd}t|d| v r:|  d}t|fi |}nd| v rJt|  dfi |}nd}t|d| v r^|  d}t|}nd| v rit|  d}td
|||d	| S )Ninitial_llm_chainZinitial_llm_chain_pathzGOne of `initial_llm_chain` or `initial_llm_chain_path` must be present.refine_llm_chainZrefine_llm_chain_pathzEOne of `refine_llm_chain` or `refine_llm_chain_path` must be present.rC   rD   )r[   r\   rC   r*   )r5   r>   r?   r6   r
   r	   r   )	r-   r"   Zinitial_llm_chain_configr[   r)   Zrefine_llm_chain_configr\   r8   rC   r*   r*   r+   _load_refine_documents_chainc  s4   



r]   r   c                 K  rX   )NrJ   rK   rL   r*   )r5   r>   r?   r6   r   )r-   r"   combine_documents_chain_configrJ   r)   r*   r*   r+   _load_qa_with_sources_chain  s"   
r_   c                 K  r%   )z)Load SQL Database chain from config dict.ae  SQLDatabaseChain is not available through LangChain anymore. The relevant code can be found in langchain_experimental, but it is not appropriate for production usage due to security concerns. Please refer to langchain-experimental repository for more details, or refer to this tutorial for best practices: https://python.langchain.com/docs/tutorials/sql_qa/rN   rP   r*   r*   r+   _load_sql_database_chain  s   r`   r   c                 K     d|v r
| d}nd}t|d| v r"|  d}t|fi |}nd| v r2t|  dfi |}nd}t|td||d| S Nvectorstorez`vectorstore` must be present.rJ   rK   rL   )rJ   rc   r*   )r5   r6   r>   r?   r   r-   r"   rc   r)   r^   rJ   r*   r*   r+   %_load_vector_db_qa_with_sources_chain  4   
re   r   c                 K  ra   N	retrieverz`retriever` must be present.rJ   rK   rL   )rJ   rh   r*   )r5   r6   r>   r?   r   r-   r"   rh   r)   r^   rJ   r*   r*   r+   _load_retrieval_qa  4   
rj   r   c                 K  ra   rg   )r5   r6   r>   r?   r   ri   r*   r*   r+   %_load_retrieval_qa_with_sources_chain  rf   rl   r   c                 K  ra   rb   )r5   r6   r>   r?   r   rd   r*   r*   r+   _load_vector_db_qa  rk   rm   r   c           
   
   K  s   d|v r
| d}nd}t|d| v r"|  d}t|fi |}nd}t|d| v r:|  d}t|fi |}nd}t|zddlm} W n ty[ }	 zd	}t||	d }	~	ww |d|||d
| S )Ngraphz`graph` must be present.cypher_generation_chainz*`cypher_generation_chain` must be present.qa_chainz`qa_chain` must be present.r   r   zTo use this GraphCypherQAChain functionality you must install the langchain_community package. You can install it with `pip install langchain_community`)rn   ro   rp   r*   )r5   r6   r>   *langchain_community.chains.graph_qa.cypherr   r'   )
r-   r"   rn   r)   Zcypher_generation_chain_configro   Zqa_chain_configrp   r   er*   r*   r+   _load_graph_cypher_chain!  sB   


rs   r   c                 K  s   d| v r|  d}t|fi |}nd| v rt|  d}nd}t|d| v r6|  d}t|fi |}nd| v rFt|  dfi |}nd}t|d|v rV| d}nd}t|td
|||d	| S )Napi_request_chainZapi_request_chain_pathzGOne of `api_request_chain` or `api_request_chain_path` must be present.api_answer_chainZapi_answer_chain_pathzEOne of `api_answer_chain` or `api_answer_chain_path` must be present.requests_wrapperz#`requests_wrapper` must be present.)rt   ru   rv   r*   )r5   r>   r?   r6   r   )r-   r"   Zapi_request_chain_configrt   r)   Zapi_answer_chain_configru   rv   r*   r*   r+   _load_api_chainH  s2   

rw   r   c              
   K  s   zddl m} W n ty } zd}t||d }~ww d| v r.| d}t|fi |}nd| v r>t| dfi |}nd}t|d|v rW|d}|d	||d| S |d	d|i| S )
Nr   r   zTo use this LLMRequestsChain functionality you must install the langchain package. You can install it with `pip install langchain`r:   r;   r<   rv   )r:   rv   r*   )langchain.chains.llm_requestsr   r'   r5   r>   r?   r6   )r-   r"   r   rr   r)   r@   r:   rv   r*   r*   r+   _load_llm_requests_chainf  s0   


ry   Z	api_chainZ
hyde_chainr:   Zllm_bash_chainZllm_checker_chainZllm_math_chainZllm_requests_chainZ	pal_chainZqa_with_sources_chainZstuff_documents_chainZmap_reduce_documents_chainrG   Zmap_rerank_documents_chainZrefine_documents_chainZsql_database_chainZvector_db_qa_with_sources_chainZvector_db_qa)Zretrieval_qaZretrieval_qa_with_sources_chainZgraph_cypher_chainz0.2.13zThis function is deprecated and will be removed in langchain 1.0. At that point chains must be imported from their respective modules.z1.0)ZsincemessageZremovalr   c                 K  sR   d| vr
d}t || d}|tvrd| d}t |t| }|| fi |S )zLoad chain from Config Dict._typez#Must specify a chain Type in configzLoading z chain not supported)r6   r5   type_to_loader_dict)r-   r"   r)   Zconfig_typeZchain_loaderr*   r*   r+   r>     s   

r>   pathUnion[str, Path]c                 K  s0   t | tr| drd}t|t| fi |S )zAUnified method for loading a chain from LangChainHub or local fs.zlc://zLoading from the deprecated github-based Hub is no longer supported. Please use the new LangChain Hub at https://smith.langchain.com/hub instead.)rE   str
startswithRuntimeError_load_chain_from_file)r}   r"   r)   r*   r*   r+   r?     s
   
r?   filec                 K  s   t | tr	t| n| }|jdkr*| }t|}W d   n1 s$w   Y  n&|jdrJ| }t	|}W d   n1 sDw   Y  nd}t
|d|v r[|d|d< d|v rf|d|d< t|fi |S )zLoad chain from file.z.jsonN)z.yamlz.ymlzFile type must be json or yamlverboseZmemory)rE   r   r   suffixopenjsonloadendswithyamlZ	safe_loadr6   r5   r>   )r   r"   	file_pathfr-   r)   r*   r*   r+   r     s$   


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-   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"   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#   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   r~   r"   r   r#   r   )Q__doc__
__future__r   r   pathlibr   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.prompts.loadingr   r	   r
   Zlangchain.chainsr   Zlangchain.chains.api.baser   Zlangchain.chains.baser   Z-langchain.chains.combine_documents.map_reducer   Z-langchain.chains.combine_documents.map_rerankr   Z)langchain.chains.combine_documents.refiner   Z(langchain.chains.combine_documents.stuffr   Zlangchain.chains.hyde.baser   Zlangchain.chains.llmr   Z!langchain.chains.llm_checker.baser   Zlangchain.chains.llm_math.baser   Z%langchain.chains.qa_with_sources.baser   Z*langchain.chains.qa_with_sources.retrievalr   Z*langchain.chains.qa_with_sources.vector_dbr   Z"langchain.chains.retrieval_qa.baser   r   rq   r   rx   r   Z langchain_community.llms.loadingr   r    r'   ZURL_BASEr9   rA   rF   rI   rH   rQ   rV   rW   rY   rZ   r]   r_   r`   re   rj   rl   rm   rs   rw   ry   r|   r>   r?   r   r*   r*   r*   r+   <module>   s    





%
E

/







!

!

'
	
