o
    Jjgp                     @  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     t d)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"    r)   P/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/chains/loading.pyr   *   s   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    1   s   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tdd| v r4|  d}t|}nd| v r@t|  d}ntd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-   r0   Nr)   )popr    r   
ValueErrorr
   r	   r   r   )r+   r"   
llm_configr-   prompt_configr0   r)   r)   r*   _load_llm_chain>   s   


r7   r   c                 K  s|   d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdd|v r0| d}ntd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.)r8   base_embeddingsNr)   )r3   load_chain_from_config
load_chainr4   r   )r+   r"   llm_chain_configr8   r;   r)   r)   r*   _load_hyde_chainT   s   
r@   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdt|ts2td| d| v r@|  d}t|}nd| v rLt|  d}ntdtd	||d| S )
Nr8   r9   r:   Expected LLMChain, got document_promptdocument_prompt_pathzCOne of `document_prompt` or `document_prompt_path` must be present.)r8   rB   r)   )	r3   r=   r>   r4   
isinstancer   r
   r	   r   )r+   r"   r?   r8   r6   rB   r)   r)   r*   _load_stuff_documents_chainh   s*   



rE   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdt|ts2td| d| v rBt|  dfi |}nd| v rRt|  dfi |}nt| fi |}td||d| S )	Nr8   r9   r:   rA   reduce_documents_chainreduce_documents_chain_path)r8   rF   r)   )r3   r=   r>   r4   rD   r   _load_reduce_documents_chainr   )r+   r"   r?   r8   rF   r)   r)   r*    _load_map_reduce_documents_chain   s6   

rI   r   c                 K  sN  d }d }d| v r|  d}t|fi |}n6d| v r(|  d}t|fi |}n$d| v r8t|  dfi |}nd| v rHt|  dfi |}ntdd| v re|  d}|d u r\d }nAt|fi |}n8d| v rut|  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_chaincombine_document_chaincombine_documents_chain_pathcombine_document_chain_pathSOne of `combine_documents_chain` or `combine_documents_chain_path` must be present.collapse_documents_chaincollapse_documents_chain_pathcollapse_document_chaincollapse_document_chain_path)rJ   rO   r)   )r3   r=   r>   r4   r   )r+   r"   rJ   rO   combine_document_chain_configcollapse_document_chain_configr)   r)   r*   rH      s   



rH   c           	      K  s   ddl m} d }d| v r| d}t|fi |}n6d| v r*t| dfi |}n&d| v r<| d}t|fi |}nd| v rLt| dfi |}ntdd| v r^| d}t|}nd	| v rit	| d	}|ru|d||d
| S |d||d| S )Nr   )LLMBashChainr8   r9   r-   r.   r:   r0   r1   r8   r0   r2   r)   )
$langchain_experimental.llm_bash.baserU   r3   r=   r>   r    r   r4   r
   r	   )	r+   r"   rU   r8   r?   r5   r-   r6   r0   r)   r)   r*   _load_llm_bash_chain   s*   



rX   r   c                 K  s.  d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdd| v r4|  d}t|}nd| v r?t|  d}d| v rM|  d}t|}nd| v rXt|  d}d| v rf|  d}t|}	nd	| v rqt|  d	}	d
| v r|  d
}
t|
}nd| v rt|  d}td||||	|d| S )Nr-   r.   r/   create_draft_answer_promptcreate_draft_answer_prompt_pathlist_assertions_promptlist_assertions_prompt_pathcheck_assertions_promptcheck_assertions_prompt_pathrevised_answer_promptrevised_answer_prompt_path)r-   rY   r[   r]   r_   r)   )r3   r    r   r4   r
   r	   r   )r+   r"   r5   r-   !create_draft_answer_prompt_configrY   list_assertions_prompt_configr[   check_assertions_prompt_configr]   revised_answer_prompt_configr_   r)   r)   r*   _load_llm_checker_chain   sV   






re   r   c                 K  s   d }d| v r|  d}t|fi |}n6d| v r$t|  dfi |}n&d| v r6|  d}t|fi |}nd| v rFt|  dfi |}ntdd| v rX|  d}t|}nd| v rct|  d}|rotd
||d| S td
||d	| S )Nr8   r9   r-   r.   r:   r0   r1   rV   r2   r)   )	r3   r=   r>   r    r   r4   r
   r	   r   )r+   r"   r8   r?   r5   r-   r6   r0   r)   r)   r*   _load_llm_math_chain+  s(   



rf   r   c                 K  ^   d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdtdd|i| S )Nr8   r9   r:   r)   )r3   r=   r>   r4   r   )r+   r"   r?   r8   r)   r)   r*    _load_map_rerank_documents_chainG  s   
rh   c                 K  sj   ddl m} d| v r| d}t|fi |}nd| v r(t| dfi |}ntd|dd|i| S )Nr   )PALChainr8   r9   r:   r)   ) langchain_experimental.pal_chainri   r3   r=   r>   r4   )r+   r"   ri   r?   r8   r)   r)   r*   _load_pal_chainT  s   
rk   r   c                 K  s   d| v r|  d}t|fi |}nd| v r"t|  dfi |}ntdd| v r8|  d}t|fi |}nd| v rHt|  dfi |}ntdd| v rZ|  d}t|}nd| v ret|  d}td
|||d	| S )Ninitial_llm_chaininitial_llm_chain_pathzGOne of `initial_llm_chain` or `initial_llm_chain_path` must be present.refine_llm_chainrefine_llm_chain_pathzEOne of `refine_llm_chain` or `refine_llm_chain_path` must be present.rB   rC   )rl   rn   rB   r)   )r3   r=   r>   r4   r
   r	   r   )r+   r"   initial_llm_chain_configrl   refine_llm_chain_configrn   r6   rB   r)   r)   r*   _load_refine_documents_chaina  s8   



rr   r   c                 K  rg   )NrJ   rL   rN   r)   )r3   r=   r>   r4   r   )r+   r"   combine_documents_chain_configrJ   r)   r)   r*   _load_qa_with_sources_chain  s"   
rt   c           
      K  s   ddl m} d|v r|d}ntdd| v r/| d}t|fi |}|d||d| S d| v rA| d}t|fi |}nd| v rQt| dfi |}ntd	d
| v rc| d
}t|}	nd }	|j||fd
|	i| S )Nr   )SQLDatabaseChaindatabasez`database` must be present.r8   )r8   rv   r-   r.   r/   r0   r)   )	langchain_experimental.sqlru   r3   r4   r=   r    r   r
   from_llm)
r+   r"   ru   rv   r?   chainr5   r-   r6   r0   r)   r)   r*   _load_sql_database_chain  s&   



rz   r   c                 K  |   d|v r
| d}ntdd| v r |  d}t|fi |}nd| v r0t|  dfi |}ntdtd||d| S Nvectorstorez`vectorstore` must be present.rJ   rL   rN   )rJ   r}   r)   )r3   r4   r=   r>   r   r+   r"   r}   rs   rJ   r)   r)   r*   %_load_vector_db_qa_with_sources_chain  2   
r   r   c                 K  r{   N	retrieverz`retriever` must be present.rJ   rL   rN   )rJ   r   r)   )r3   r4   r=   r>   r   r+   r"   r   rs   rJ   r)   r)   r*   _load_retrieval_qa  2   
r   r   c                 K  r{   r   )r3   r4   r=   r>   r   r   r)   r)   r*   %_load_retrieval_qa_with_sources_chain  r   r   r   c                 K  r{   r|   )r3   r4   r=   r>   r   r~   r)   r)   r*   _load_vector_db_qa   r   r   r   c                 K  s   d|v r
| d}ntdd| v r |  d}t|fi |}ntdd| v r6|  d}t|fi |}ntdzddlm} W n tyM   td	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`)r   r   r   r)   )r3   r4   r=   *langchain_community.chains.graph_qa.cypherr   r'   )r+   r"   r   cypher_generation_chain_configr   qa_chain_configr   r   r)   r)   r*   _load_graph_cypher_chain  s:   

r   r   c                 K  s   d| v r|  d}t|fi |}nd| v rt|  d}ntdd| v r4|  d}t|fi |}nd| v rDt|  dfi |}ntdd|v rR| d}ntdtd
|||d	| S )Napi_request_chainapi_request_chain_pathzGOne of `api_request_chain` or `api_request_chain_path` must be present.api_answer_chainapi_answer_chain_pathzEOne of `api_answer_chain` or `api_answer_chain_path` must be present.requests_wrapperz#`requests_wrapper` must be present.)r   r   r   r)   )r3   r=   r>   r4   r   )r+   r"   api_request_chain_configr   api_answer_chain_configr   r   r)   r)   r*   _load_api_chain<  s4   

r   r   c                 K  s   zddl m} W n ty   tdw d| v r&| d}t|fi |}nd| v r6t| dfi |}ntdd|v rM|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`r8   r9   r:   r   )r8   r   r)   )langchain.chains.llm_requestsr   r'   r3   r=   r>   r4   )r+   r"   r   r?   r8   r   r)   r)   r*   _load_llm_requests_chain[  s*   

r   	api_chain
hyde_chainr8   llm_bash_chainllm_checker_chainllm_math_chainllm_requests_chain	pal_chainqa_with_sources_chainstuff_documents_chainmap_reduce_documents_chainrF   map_rerank_documents_chainrefine_documents_chainsql_database_chainvector_db_qa_with_sources_chainvector_db_qa)retrieval_qaretrieval_qa_with_sources_chain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)sincemessageremovalr   c                 K  sJ   d| vrt d| d}|tvrt d| dt| }|| fi |S )zLoad chain from Config Dict._typez#Must specify a chain Type in configzLoading z chain not supported)r4   r3   type_to_loader_dict)r+   r"   config_typechain_loaderr)   r)   r*   r=     s   

r=   pathUnion[str, Path]c                 K  s,   t | tr| drtd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.)rD   str
startswithRuntimeError_load_chain_from_file)r   r"   r)   r)   r*   r>     s
   
r>   filec                 K  s   t | tr
t| }n| }|jdkr+t|}t|}W d   n1 s%w   Y  n%|jdrLt|d}t	|}W d   n1 sFw   Y  nt
dd|v r[|d|d< d|v rf|d|d< t|fi |S )zLoad chain from file.z.jsonN)z.yamlz.ymlrzFile type must be json or yamlverbosememory)rD   r   r   suffixopenjsonloadendswithyaml	safe_loadr4   r3   r=   )r   r"   	file_pathfr+   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   langchain_core._apir   langchain_core.prompts.loadingr   r	   r
   langchain.chainsr   langchain.chains.api.baser   langchain.chains.baser   -langchain.chains.combine_documents.map_reducer   -langchain.chains.combine_documents.map_rerankr   )langchain.chains.combine_documents.refiner   (langchain.chains.combine_documents.stuffr   langchain.chains.hyde.baser   langchain.chains.llmr   !langchain.chains.llm_checker.baser   langchain.chains.llm_math.baser   %langchain.chains.qa_with_sources.baser   *langchain.chains.qa_with_sources.retrievalr   *langchain.chains.qa_with_sources.vector_dbr   "langchain.chains.retrieval_qa.baser   r   r   r   r   r    langchain_community.llms.loadingr   r    r'   URL_BASEr7   r@   rE   rI   rH   rX   re   rf   rh   rk   rr   rt   rz   r   r   r   r   r   r   r   r   r=   r>   r   r)   r)   r)   r*   <module>   s    





 
<

.



 






"
	
