a
    agp                     @  st  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r6ddl0m1Z1 ddl2m3Z3 zddl4m5Z5m6Z6 W n8 e7y   ddddddZ5dddddd Z6Y n0 d!Z8d"dd#d$d%d&Z9d"dd'd$d(d)Z:d"dd*d$d+d,Z;d"dd-d$d.d/Z<d"dd0d$d1d2Z=d"ddd$d3d4Z>d"dd5d$d6d7Z?d"dd8d$d9d:Z@d"dd;d$d<d=ZAd"ddd$d>d?ZBd"dd@d$dAdBZCd"ddCd$dDdEZDd"ddd$dFdGZEd"ddHd$dIdJZFd"ddKd$dLdMZGd"ddNd$dOdPZHd"ddQd$dRdSZId"ddTd$dUdVZJd"ddWd$dXdYZKd"ddZd$d[d\ZLeKe:e9e>e?e@eLeBeDe;e<e=eAeCeEeFeIeGeHeJd]ZMed^d_d`dad"ddbd$dcddZNed^d_d`dadeddbdfdgdhZOdeddbdidjdkZPdS )lz!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_configr   None)argskwargsreturnc                  O  s   t dd S )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(   f/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/chains/loading.pyr   *   s    r   c                  O  s   t dd S )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/dictr   )configr#   r$   c                 K  s   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdd| v rh|  d}t|}n d| v rt|  d}ntdt|  tf ||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,   r/   )popr    r   
ValueErrorr
   r	   r   r   )r+   r#   
llm_configr,   prompt_configr/   r(   r(   r)   _load_llm_chain>   s    


r6   r   c                 K  s|   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdd|v r`| d}ntdtf ||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.)r7   Zbase_embeddings)r2   load_chain_from_config
load_chainr3   r   )r+   r#   llm_chain_configr7   r:   r(   r(   r)   _load_hyde_chainT   s    
r>   r   c                 K  s   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdt|tsdtd| d| v r|  d}t|}n d| v rt|  d}ntdtf ||d| S )	Nr7   r8   r9   Expected LLMChain, got document_promptdocument_prompt_pathzCOne of `document_prompt` or `document_prompt_path` must be present.)r7   r@   )	r2   r;   r<   r3   
isinstancer   r
   r	   r   )r+   r#   r=   r7   r5   r@   r(   r(   r)   _load_stuff_documents_chainh   s*    



rC   r   c                 K  s   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdt|tsdtd| d| v rt|  dfi |}n0d| v rt|  dfi |}nt| fi |}tf ||d| S )Nr7   r8   r9   r?   reduce_documents_chainZreduce_documents_chain_path)r7   rD   )r2   r;   r<   r3   rB   r   _load_reduce_documents_chainr   )r+   r#   r=   r7   rD   r(   r(   r)    _load_map_reduce_documents_chain   s6    

rF   r   c                 K  sT  d }d }d| v r,|  d}t|fi |}nld| v rP|  d}t|fi |}nHd| v rpt|  dfi |}n(d| v rt|  dfi |}ntdd| v r|  d}|d u rd }nt|fi |}nvd| v rt|  dfi |}nVd| v r |  d}|d u rd }nt|fi |}n d	| v r@t|  d	fi |}tf ||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)rG   rJ   )r2   r;   r<   r3   r   )r+   r#   rG   rJ   Zcombine_document_chain_configZcollapse_document_chain_configr(   r(   r)   rE      s    






rE   c           	      K  s  ddl m} d }d| v r4| d}t|fi |}nld| v rTt| dfi |}nLd| v rx| d}t|fi |}n(d| v rt| dfi |}ntdd| v r| d}t|}nd	| v rt	| d	}|r|f ||d
| S |f ||d| S d S )Nr   )LLMBashChainr7   r8   r,   r-   r9   r/   r0   r7   r/   r1   )
Z$langchain_experimental.llm_bash.baserK   r2   r;   r<   r    r   r3   r
   r	   )	r+   r#   rK   r7   r=   r4   r,   r5   r/   r(   r(   r)   _load_llm_bash_chain   s*    



rM   r   c                 K  s0  d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdd| v rh|  d}t|}nd| v r~t|  d}d| v r|  d}t|}nd| v rt|  d}d| v r|  d}t|}	nd	| v rt|  d	}	d
| v r|  d
}
t|
}nd| v rt|  d}tf ||||	|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,   rN   rO   rP   rQ   )r2   r    r   r3   r
   r	   r   )r+   r#   r4   r,   Z!create_draft_answer_prompt_configrN   Zlist_assertions_prompt_configrO   Zcheck_assertions_prompt_configrP   Zrevised_answer_prompt_configrQ   r(   r(   r)   _load_llm_checker_chain   sV    







rR   r   c                 K  s   d }d| v r(|  d}t|fi |}nld| v rHt|  dfi |}nLd| v rl|  d}t|fi |}n(d| v rt|  dfi |}ntdd| v r|  d}t|}nd| v rt|  d}|rtf ||d| S tf ||d	| S d S )
Nr7   r8   r,   r-   r9   r/   r0   rL   r1   )	r2   r;   r<   r    r   r3   r
   r	   r   )r+   r#   r7   r=   r4   r,   r5   r/   r(   r(   r)   _load_llm_math_chain+  s(    



rS   r   c                 K  s^   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdtf d|i| S )Nr7   r8   r9   )r2   r;   r<   r3   r   )r+   r#   r=   r7   r(   r(   r)    _load_map_rerank_documents_chainG  s    
rT   c                 K  sj   ddl m} d| v r0| d}t|fi |}n(d| v rPt| dfi |}ntd|f d|i| S )Nr   )PALChainr7   r8   r9   )Z langchain_experimental.pal_chainrU   r2   r;   r<   r3   )r+   r#   rU   r=   r7   r(   r(   r)   _load_pal_chainT  s    
rV   r   c                 K  s   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdd| v rp|  d}t|fi |}n(d| v rt|  dfi |}ntdd| v r|  d}t|}nd| v rt|  d}tf |||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.r@   rA   )rW   rX   r@   )r2   r;   r<   r3   r
   r	   r   )r+   r#   Zinitial_llm_chain_configrW   Zrefine_llm_chain_configrX   r5   r@   r(   r(   r)   _load_refine_documents_chaina  s8    



rY   r   c                 K  s^   d| v r$|  d}t|fi |}n(d| v rDt|  dfi |}ntdtf d|i| S )NrG   rH   rI   )r2   r;   r<   r3   r   )r+   r#   combine_documents_chain_configrG   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 |}|f ||d| S d| v r| d}t|fi |}n(d| v rt| dfi |}ntd	d
| v r| d
}t|}	nd }	|j||fd
|	i| S )Nr   )SQLDatabaseChaindatabasez`database` must be present.r7   )r7   r]   r,   r-   r.   r/   )	Zlangchain_experimental.sqlr\   r2   r3   r;   r    r   r
   Zfrom_llm)
r+   r#   r\   r]   r=   chainr4   r,   r5   r/   r(   r(   r)   _load_sql_database_chain  s&    



r_   r   c                 K  s|   d|v r| d}ntdd| v r@|  d}t|fi |}n(d| v r`t|  dfi |}ntdtf ||d| S Nvectorstorez`vectorstore` must be present.rG   rH   rI   )rG   ra   )r2   r3   r;   r<   r   r+   r#   ra   rZ   rG   r(   r(   r)   %_load_vector_db_qa_with_sources_chain  s2    
rc   r   c                 K  s|   d|v r| d}ntdd| v r@|  d}t|fi |}n(d| v r`t|  dfi |}ntdtf ||d| S N	retrieverz`retriever` must be present.rG   rH   rI   )rG   re   )r2   r3   r;   r<   r   r+   r#   re   rZ   rG   r(   r(   r)   _load_retrieval_qa  s2    
rg   r   c                 K  s|   d|v r| d}ntdd| v r@|  d}t|fi |}n(d| v r`t|  dfi |}ntdtf ||d| S rd   )r2   r3   r;   r<   r   rf   r(   r(   r)   %_load_retrieval_qa_with_sources_chain  s2    
rh   r   c                 K  s|   d|v r| d}ntdd| v r@|  d}t|fi |}n(d| v r`t|  dfi |}ntdtf ||d| S r`   )r2   r3   r;   r<   r   rb   r(   r(   r)   _load_vector_db_qa   s2    
ri   r   c                 K  s   d|v r| d}ntdd| v r@|  d}t|fi |}ntdd| v rl|  d}t|fi |}ntdzddlm} W n ty   td	Y n0 |f |||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`)rj   rk   rl   )r2   r3   r;   *langchain_community.chains.graph_qa.cypherr   r&   )r+   r#   rj   Zcypher_generation_chain_configrk   Zqa_chain_configrl   r   r(   r(   r)   _load_graph_cypher_chain  s8    


rn   r   c                 K  s   d| v r$|  d}t|fi |}n d| v r<t|  d}ntdd| v rh|  d}t|fi |}n(d| v rt|  dfi |}ntdd|v r| d}ntdtf |||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.)ro   rp   rq   )r2   r;   r<   r3   r   )r+   r#   Zapi_request_chain_configro   Zapi_answer_chain_configrp   rq   r(   r(   r)   _load_api_chain<  s4    

rr   r   c                 K  s   zddl m} W n ty*   tdY n0 d| v rP| d}t|fi |}n(d| v rpt| dfi |}ntdd|v r|d}|f ||d| S |f d|i| S d S )	Nr   r   zTo use this LLMRequestsChain functionality you must install the langchain package. You can install it with `pip install langchain`r7   r8   r9   rq   )r7   rq   )langchain.chains.llm_requestsr   r&   r2   r;   r<   r3   )r+   r#   r   r=   r7   rq   r(   r(   r)   _load_llm_requests_chain[  s(    


rt   )Z	api_chainZ
hyde_chainr7   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_chainrD   Zmap_rerank_documents_chainZrefine_documents_chainZsql_database_chainZvector_db_qa_with_sources_chainZvector_db_qaZ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r2t 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)r3   r2   type_to_loader_dict)r+   r#   Zconfig_typeZchain_loaderr(   r(   r)   r;     s    

r;   zUnion[str, Path])pathr#   r$   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.)rB   str
startswithRuntimeError_load_chain_from_file)rx   r#   r(   r(   r)   r<     s
    
r<   )filer#   r$   c                 K  s   t | trt| }n| }|jdkrVt|}t|}W d   q1 sJ0    Y  nJ|jdrt|d}t	|}W d   q1 s0    Y  nt
dd|v r|d|d< d|v r|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)rB   ry   r   suffixopenjsonloadendswithyamlZ	safe_loadr3   r2   r;   )r}   r#   	file_pathfr+   r(   r(   r)   r|     s    



**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   rm   r   rs   r   Z langchain_community.llms.loadingr   r    r&   ZURL_BASEr6   r>   rC   rF   rE   rM   rR   rS   rT   rV   rY   r[   r_   rc   rg   rh   ri   rn   rr   rt   rw   r;   r<   r|   r(   r(   r(   r)   <module>   s   
 <. "