o
    Zhp                     @  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)   O/var/www/html/lang_env/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   Z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   rA   r)   )	r3   r<   r=   r4   
isinstancer   r
   r	   r   )r+   r"   r>   r8   r6   rA   r)   r)   r*   _load_stuff_documents_chainh   s*   



rD   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:   r@   reduce_documents_chainZreduce_documents_chain_path)r8   rE   r)   )r3   r<   r=   r4   rC   r   _load_reduce_documents_chainr   )r+   r"   r>   r8   rE   r)   r)   r*    _load_map_reduce_documents_chain   s6   

rG   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_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)rH   rK   r)   )r3   r<   r=   r4   r   )r+   r"   rH   rK   Zcombine_document_chain_configZcollapse_document_chain_configr)   r)   r*   rF      s   



rF   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)   )
Z$langchain_experimental.llm_bash.baserL   r3   r<   r=   r    r   r4   r
   r	   )	r+   r"   rL   r8   r>   r5   r-   r6   r0   r)   r)   r*   _load_llm_bash_chain   s*   



rN   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_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-   rO   rP   rQ   rR   r)   )r3   r    r   r4   r
   r	   r   )r+   r"   r5   r-   Z!create_draft_answer_prompt_configrO   Zlist_assertions_prompt_configrP   Zcheck_assertions_prompt_configrQ   Zrevised_answer_prompt_configrR   r)   r)   r*   _load_llm_checker_chain   sV   






rS   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   rM   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(   



rT   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   
rV   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)   )Z langchain_experimental.pal_chainrW   r3   r<   r=   r4   )r+   r"   rW   r>   r8   r)   r)   r*   _load_pal_chainT  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 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_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.rA   rB   )rY   rZ   rA   r)   )r3   r<   r=   r4   r
   r	   r   )r+   r"   Zinitial_llm_chain_configrY   Zrefine_llm_chain_configrZ   r6   rA   r)   r)   r*   _load_refine_documents_chaina  s8   



r[   r   c                 K  rU   )NrH   rI   rJ   r)   )r3   r<   r=   r4   r   )r+   r"   combine_documents_chain_configrH   r)   r)   r*   _load_qa_with_sources_chain  s"   
r]   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   r_   r-   r.   r/   r0   r)   )	Zlangchain_experimental.sqlr^   r3   r4   r<   r    r   r
   Zfrom_llm)
r+   r"   r^   r_   r>   chainr5   r-   r6   r0   r)   r)   r*   _load_sql_database_chain  s&   



ra   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.rH   rI   rJ   )rH   rd   r)   )r3   r4   r<   r=   r   r+   r"   rd   r\   rH   r)   r)   r*   %_load_vector_db_qa_with_sources_chain  2   
rf   r   c                 K  rb   N	retrieverz`retriever` must be present.rH   rI   rJ   )rH   ri   r)   )r3   r4   r<   r=   r   r+   r"   ri   r\   rH   r)   r)   r*   _load_retrieval_qa  2   
rk   r   c                 K  rb   rh   )r3   r4   r<   r=   r   rj   r)   r)   r*   %_load_retrieval_qa_with_sources_chain  rg   rm   r   c                 K  rb   rc   )r3   r4   r<   r=   r   re   r)   r)   r*   _load_vector_db_qa   rl   rn   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`)ro   rp   rq   r)   )r3   r4   r<   *langchain_community.chains.graph_qa.cypherr   r'   )r+   r"   ro   Zcypher_generation_chain_configrp   Zqa_chain_configrq   r   r)   r)   r*   _load_graph_cypher_chain  s:   

rs   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_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)   )r3   r<   r=   r4   r   )r+   r"   Zapi_request_chain_configrt   Zapi_answer_chain_configru   rv   r)   r)   r*   _load_api_chain<  s4   

rw   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:   rv   )r8   rv   r)   )langchain.chains.llm_requestsr   r'   r3   r<   r=   r4   )r+   r"   r   r>   r8   rv   r)   r)   r*   _load_llm_requests_chain[  s*   

ry   Z	api_chainZ
hyde_chainr8   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_chainrE   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  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"   Zconfig_typeZ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.)rC   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verboseZmemory)rC   r   r   suffixopenjsonloadendswithyamlZ	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   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   rr   r   rx   r   Z langchain_community.llms.loadingr   r    r'   ZURL_BASEr7   r?   rD   rG   rF   rN   rS   rT   rV   rX   r[   r]   ra   rf   rk   rm   rn   rs   rw   ry   r|   r<   r=   r   r)   r)   r)   r*   <module>   s    





 
<

.



 






"
	
