"""Functionality for loading chains."""from__future__importannotationsimportjsonfrompathlibimportPathfromtypingimportTYPE_CHECKING,Any,Unionimportyamlfromlangchain_core._apiimportdeprecatedfromlangchain_core.prompts.loadingimport(_load_output_parser,load_prompt,load_prompt_from_config,)fromlangchain.chainsimportReduceDocumentsChainfromlangchain.chains.api.baseimportAPIChainfromlangchain.chains.baseimportChainfromlangchain.chains.combine_documents.map_reduceimportMapReduceDocumentsChainfromlangchain.chains.combine_documents.map_rerankimportMapRerankDocumentsChainfromlangchain.chains.combine_documents.refineimportRefineDocumentsChainfromlangchain.chains.combine_documents.stuffimportStuffDocumentsChainfromlangchain.chains.hyde.baseimportHypotheticalDocumentEmbedderfromlangchain.chains.llmimportLLMChainfromlangchain.chains.llm_checker.baseimportLLMCheckerChainfromlangchain.chains.llm_math.baseimportLLMMathChainfromlangchain.chains.qa_with_sources.baseimportQAWithSourcesChainfromlangchain.chains.qa_with_sources.retrievalimportRetrievalQAWithSourcesChainfromlangchain.chains.qa_with_sources.vector_dbimportVectorDBQAWithSourcesChainfromlangchain.chains.retrieval_qa.baseimportRetrievalQA,VectorDBQAifTYPE_CHECKING:fromlangchain_community.chains.graph_qa.cypherimportGraphCypherQAChainfromlangchain.chains.llm_requestsimportLLMRequestsChaintry:fromlangchain_community.llms.loadingimportload_llm,load_llm_from_configexceptImportError:defload_llm(*args:Any,**kwargs:Any)->None:# type: ignoreraiseImportError("To use this load_llm functionality you must install the ""langchain_community package. ""You can install it with `pip install langchain_community`")defload_llm_from_config(# type: ignore*args:Any,**kwargs:Any)->None:raiseImportError("To use this load_llm_from_config functionality you must install the ""langchain_community package. ""You can install it with `pip install langchain_community`")URL_BASE="https://raw.githubusercontent.com/hwchase17/langchain-hub/master/chains/"def_load_llm_chain(config:dict,**kwargs:Any)->LLMChain:"""Load LLM chain from config dict."""if"llm"inconfig:llm_config=config.pop("llm")llm=load_llm_from_config(llm_config,**kwargs)elif"llm_path"inconfig:llm=load_llm(config.pop("llm_path"),**kwargs)else:raiseValueError("One of `llm` or `llm_path` must be present.")if"prompt"inconfig:prompt_config=config.pop("prompt")prompt=load_prompt_from_config(prompt_config)elif"prompt_path"inconfig:prompt=load_prompt(config.pop("prompt_path"))else:raiseValueError("One of `prompt` or `prompt_path` must be present.")_load_output_parser(config)returnLLMChain(llm=llm,prompt=prompt,**config)def_load_hyde_chain(config:dict,**kwargs:Any)->HypotheticalDocumentEmbedder:"""Load hypothetical document embedder chain from config dict."""if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")if"embeddings"inkwargs:embeddings=kwargs.pop("embeddings")else:raiseValueError("`embeddings` must be present.")returnHypotheticalDocumentEmbedder(llm_chain=llm_chain,# type: ignore[arg-type]base_embeddings=embeddings,**config,# type: ignore[arg-type])def_load_stuff_documents_chain(config:dict,**kwargs:Any)->StuffDocumentsChain:if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")ifnotisinstance(llm_chain,LLMChain):raiseValueError(f"Expected LLMChain, got {llm_chain}")if"document_prompt"inconfig:prompt_config=config.pop("document_prompt")document_prompt=load_prompt_from_config(prompt_config)elif"document_prompt_path"inconfig:document_prompt=load_prompt(config.pop("document_prompt_path"))else:raiseValueError("One of `document_prompt` or `document_prompt_path` must be present.")returnStuffDocumentsChain(llm_chain=llm_chain,document_prompt=document_prompt,**config)def_load_map_reduce_documents_chain(config:dict,**kwargs:Any)->MapReduceDocumentsChain:if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")ifnotisinstance(llm_chain,LLMChain):raiseValueError(f"Expected LLMChain, got {llm_chain}")if"reduce_documents_chain"inconfig:reduce_documents_chain=load_chain_from_config(config.pop("reduce_documents_chain"),**kwargs)elif"reduce_documents_chain_path"inconfig:reduce_documents_chain=load_chain(config.pop("reduce_documents_chain_path"),**kwargs)else:reduce_documents_chain=_load_reduce_documents_chain(config,**kwargs)returnMapReduceDocumentsChain(llm_chain=llm_chain,reduce_documents_chain=reduce_documents_chain,# type: ignore[arg-type]**config,)def_load_reduce_documents_chain(config:dict,**kwargs:Any)->ReduceDocumentsChain:# type: ignore[valid-type]combine_documents_chain=Nonecollapse_documents_chain=Noneif"combine_documents_chain"inconfig:combine_document_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_document_chain_config,**kwargs)elif"combine_document_chain"inconfig:combine_document_chain_config=config.pop("combine_document_chain")combine_documents_chain=load_chain_from_config(combine_document_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)elif"combine_document_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_document_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")if"collapse_documents_chain"inconfig:collapse_document_chain_config=config.pop("collapse_documents_chain")ifcollapse_document_chain_configisNone:collapse_documents_chain=Noneelse:collapse_documents_chain=load_chain_from_config(collapse_document_chain_config,**kwargs)elif"collapse_documents_chain_path"inconfig:collapse_documents_chain=load_chain(config.pop("collapse_documents_chain_path"),**kwargs)elif"collapse_document_chain"inconfig:collapse_document_chain_config=config.pop("collapse_document_chain")ifcollapse_document_chain_configisNone:collapse_documents_chain=Noneelse:collapse_documents_chain=load_chain_from_config(collapse_document_chain_config,**kwargs)elif"collapse_document_chain_path"inconfig:collapse_documents_chain=load_chain(config.pop("collapse_document_chain_path"),**kwargs)returnReduceDocumentsChain(# type: ignore[misc]combine_documents_chain=combine_documents_chain,collapse_documents_chain=collapse_documents_chain,**config,)def_load_llm_bash_chain(config:dict,**kwargs:Any)->Any:fromlangchain_experimental.llm_bash.baseimportLLMBashChainllm_chain=Noneif"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)# llm attribute is deprecated in favor of llm_chain, here to support old configselif"llm"inconfig:llm_config=config.pop("llm")llm=load_llm_from_config(llm_config,**kwargs)# llm_path attribute is deprecated in favor of llm_chain_path,# its to support old configselif"llm_path"inconfig:llm=load_llm(config.pop("llm_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")if"prompt"inconfig:prompt_config=config.pop("prompt")prompt=load_prompt_from_config(prompt_config)elif"prompt_path"inconfig:prompt=load_prompt(config.pop("prompt_path"))ifllm_chain:returnLLMBashChain(llm_chain=llm_chain,prompt=prompt,**config)# type: ignore[arg-type]else:returnLLMBashChain(llm=llm,prompt=prompt,**config)def_load_llm_checker_chain(config:dict,**kwargs:Any)->LLMCheckerChain:if"llm"inconfig:llm_config=config.pop("llm")llm=load_llm_from_config(llm_config,**kwargs)elif"llm_path"inconfig:llm=load_llm(config.pop("llm_path"),**kwargs)else:raiseValueError("One of `llm` or `llm_path` must be present.")if"create_draft_answer_prompt"inconfig:create_draft_answer_prompt_config=config.pop("create_draft_answer_prompt")create_draft_answer_prompt=load_prompt_from_config(create_draft_answer_prompt_config)elif"create_draft_answer_prompt_path"inconfig:create_draft_answer_prompt=load_prompt(config.pop("create_draft_answer_prompt_path"))if"list_assertions_prompt"inconfig:list_assertions_prompt_config=config.pop("list_assertions_prompt")list_assertions_prompt=load_prompt_from_config(list_assertions_prompt_config)elif"list_assertions_prompt_path"inconfig:list_assertions_prompt=load_prompt(config.pop("list_assertions_prompt_path"))if"check_assertions_prompt"inconfig:check_assertions_prompt_config=config.pop("check_assertions_prompt")check_assertions_prompt=load_prompt_from_config(check_assertions_prompt_config)elif"check_assertions_prompt_path"inconfig:check_assertions_prompt=load_prompt(config.pop("check_assertions_prompt_path"))if"revised_answer_prompt"inconfig:revised_answer_prompt_config=config.pop("revised_answer_prompt")revised_answer_prompt=load_prompt_from_config(revised_answer_prompt_config)elif"revised_answer_prompt_path"inconfig:revised_answer_prompt=load_prompt(config.pop("revised_answer_prompt_path"))returnLLMCheckerChain(llm=llm,create_draft_answer_prompt=create_draft_answer_prompt,# type: ignore[arg-type]list_assertions_prompt=list_assertions_prompt,# type: ignore[arg-type]check_assertions_prompt=check_assertions_prompt,# type: ignore[arg-type]revised_answer_prompt=revised_answer_prompt,# type: ignore[arg-type]**config,)def_load_llm_math_chain(config:dict,**kwargs:Any)->LLMMathChain:llm_chain=Noneif"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)# llm attribute is deprecated in favor of llm_chain, here to support old configselif"llm"inconfig:llm_config=config.pop("llm")llm=load_llm_from_config(llm_config,**kwargs)# llm_path attribute is deprecated in favor of llm_chain_path,# its to support old configselif"llm_path"inconfig:llm=load_llm(config.pop("llm_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")if"prompt"inconfig:prompt_config=config.pop("prompt")prompt=load_prompt_from_config(prompt_config)elif"prompt_path"inconfig:prompt=load_prompt(config.pop("prompt_path"))ifllm_chain:returnLLMMathChain(llm_chain=llm_chain,prompt=prompt,**config)# type: ignore[arg-type]else:returnLLMMathChain(llm=llm,prompt=prompt,**config)def_load_map_rerank_documents_chain(config:dict,**kwargs:Any)->MapRerankDocumentsChain:if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")returnMapRerankDocumentsChain(llm_chain=llm_chain,**config)# type: ignore[arg-type]def_load_pal_chain(config:dict,**kwargs:Any)->Any:fromlangchain_experimental.pal_chainimportPALChainif"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")returnPALChain(llm_chain=llm_chain,**config)# type: ignore[arg-type]def_load_refine_documents_chain(config:dict,**kwargs:Any)->RefineDocumentsChain:if"initial_llm_chain"inconfig:initial_llm_chain_config=config.pop("initial_llm_chain")initial_llm_chain=load_chain_from_config(initial_llm_chain_config,**kwargs)elif"initial_llm_chain_path"inconfig:initial_llm_chain=load_chain(config.pop("initial_llm_chain_path"),**kwargs)else:raiseValueError("One of `initial_llm_chain` or `initial_llm_chain_path` must be present.")if"refine_llm_chain"inconfig:refine_llm_chain_config=config.pop("refine_llm_chain")refine_llm_chain=load_chain_from_config(refine_llm_chain_config,**kwargs)elif"refine_llm_chain_path"inconfig:refine_llm_chain=load_chain(config.pop("refine_llm_chain_path"),**kwargs)else:raiseValueError("One of `refine_llm_chain` or `refine_llm_chain_path` must be present.")if"document_prompt"inconfig:prompt_config=config.pop("document_prompt")document_prompt=load_prompt_from_config(prompt_config)elif"document_prompt_path"inconfig:document_prompt=load_prompt(config.pop("document_prompt_path"))returnRefineDocumentsChain(initial_llm_chain=initial_llm_chain,# type: ignore[arg-type]refine_llm_chain=refine_llm_chain,# type: ignore[arg-type]document_prompt=document_prompt,**config,)def_load_qa_with_sources_chain(config:dict,**kwargs:Any)->QAWithSourcesChain:if"combine_documents_chain"inconfig:combine_documents_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_documents_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")returnQAWithSourcesChain(combine_documents_chain=combine_documents_chain,**config)# type: ignore[arg-type]def_load_sql_database_chain(config:dict,**kwargs:Any)->Any:fromlangchain_experimental.sqlimportSQLDatabaseChainif"database"inkwargs:database=kwargs.pop("database")else:raiseValueError("`database` must be present.")if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")chain=load_chain_from_config(llm_chain_config,**kwargs)returnSQLDatabaseChain(llm_chain=chain,database=database,**config)# type: ignore[arg-type]if"llm"inconfig:llm_config=config.pop("llm")llm=load_llm_from_config(llm_config,**kwargs)elif"llm_path"inconfig:llm=load_llm(config.pop("llm_path"),**kwargs)else:raiseValueError("One of `llm` or `llm_path` must be present.")if"prompt"inconfig:prompt_config=config.pop("prompt")prompt=load_prompt_from_config(prompt_config)else:prompt=NonereturnSQLDatabaseChain.from_llm(llm,database,prompt=prompt,**config)def_load_vector_db_qa_with_sources_chain(config:dict,**kwargs:Any)->VectorDBQAWithSourcesChain:if"vectorstore"inkwargs:vectorstore=kwargs.pop("vectorstore")else:raiseValueError("`vectorstore` must be present.")if"combine_documents_chain"inconfig:combine_documents_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_documents_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")returnVectorDBQAWithSourcesChain(combine_documents_chain=combine_documents_chain,# type: ignore[arg-type]vectorstore=vectorstore,**config,)def_load_retrieval_qa(config:dict,**kwargs:Any)->RetrievalQA:if"retriever"inkwargs:retriever=kwargs.pop("retriever")else:raiseValueError("`retriever` must be present.")if"combine_documents_chain"inconfig:combine_documents_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_documents_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")returnRetrievalQA(combine_documents_chain=combine_documents_chain,# type: ignore[arg-type]retriever=retriever,**config,)def_load_retrieval_qa_with_sources_chain(config:dict,**kwargs:Any)->RetrievalQAWithSourcesChain:if"retriever"inkwargs:retriever=kwargs.pop("retriever")else:raiseValueError("`retriever` must be present.")if"combine_documents_chain"inconfig:combine_documents_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_documents_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")returnRetrievalQAWithSourcesChain(combine_documents_chain=combine_documents_chain,# type: ignore[arg-type]retriever=retriever,**config,)def_load_vector_db_qa(config:dict,**kwargs:Any)->VectorDBQA:if"vectorstore"inkwargs:vectorstore=kwargs.pop("vectorstore")else:raiseValueError("`vectorstore` must be present.")if"combine_documents_chain"inconfig:combine_documents_chain_config=config.pop("combine_documents_chain")combine_documents_chain=load_chain_from_config(combine_documents_chain_config,**kwargs)elif"combine_documents_chain_path"inconfig:combine_documents_chain=load_chain(config.pop("combine_documents_chain_path"),**kwargs)else:raiseValueError("One of `combine_documents_chain` or ""`combine_documents_chain_path` must be present.")returnVectorDBQA(combine_documents_chain=combine_documents_chain,# type: ignore[arg-type]vectorstore=vectorstore,**config,)def_load_graph_cypher_chain(config:dict,**kwargs:Any)->GraphCypherQAChain:if"graph"inkwargs:graph=kwargs.pop("graph")else:raiseValueError("`graph` must be present.")if"cypher_generation_chain"inconfig:cypher_generation_chain_config=config.pop("cypher_generation_chain")cypher_generation_chain=load_chain_from_config(cypher_generation_chain_config,**kwargs)else:raiseValueError("`cypher_generation_chain` must be present.")if"qa_chain"inconfig:qa_chain_config=config.pop("qa_chain")qa_chain=load_chain_from_config(qa_chain_config,**kwargs)else:raiseValueError("`qa_chain` must be present.")try:fromlangchain_community.chains.graph_qa.cypherimportGraphCypherQAChainexceptImportError:raiseImportError("To use this GraphCypherQAChain functionality you must install the ""langchain_community package. ""You can install it with `pip install langchain_community`")returnGraphCypherQAChain(graph=graph,cypher_generation_chain=cypher_generation_chain,# type: ignore[arg-type]qa_chain=qa_chain,# type: ignore[arg-type]**config,)def_load_api_chain(config:dict,**kwargs:Any)->APIChain:if"api_request_chain"inconfig:api_request_chain_config=config.pop("api_request_chain")api_request_chain=load_chain_from_config(api_request_chain_config,**kwargs)elif"api_request_chain_path"inconfig:api_request_chain=load_chain(config.pop("api_request_chain_path"))else:raiseValueError("One of `api_request_chain` or `api_request_chain_path` must be present.")if"api_answer_chain"inconfig:api_answer_chain_config=config.pop("api_answer_chain")api_answer_chain=load_chain_from_config(api_answer_chain_config,**kwargs)elif"api_answer_chain_path"inconfig:api_answer_chain=load_chain(config.pop("api_answer_chain_path"),**kwargs)else:raiseValueError("One of `api_answer_chain` or `api_answer_chain_path` must be present.")if"requests_wrapper"inkwargs:requests_wrapper=kwargs.pop("requests_wrapper")else:raiseValueError("`requests_wrapper` must be present.")returnAPIChain(api_request_chain=api_request_chain,# type: ignore[arg-type]api_answer_chain=api_answer_chain,# type: ignore[arg-type]requests_wrapper=requests_wrapper,**config,)def_load_llm_requests_chain(config:dict,**kwargs:Any)->LLMRequestsChain:try:fromlangchain.chains.llm_requestsimportLLMRequestsChainexceptImportError:raiseImportError("To use this LLMRequestsChain functionality you must install the ""langchain package. ""You can install it with `pip install langchain`")if"llm_chain"inconfig:llm_chain_config=config.pop("llm_chain")llm_chain=load_chain_from_config(llm_chain_config,**kwargs)elif"llm_chain_path"inconfig:llm_chain=load_chain(config.pop("llm_chain_path"),**kwargs)else:raiseValueError("One of `llm_chain` or `llm_chain_path` must be present.")if"requests_wrapper"inkwargs:requests_wrapper=kwargs.pop("requests_wrapper")returnLLMRequestsChain(llm_chain=llm_chain,requests_wrapper=requests_wrapper,**config)else:returnLLMRequestsChain(llm_chain=llm_chain,**config)type_to_loader_dict={"api_chain":_load_api_chain,"hyde_chain":_load_hyde_chain,"llm_chain":_load_llm_chain,"llm_bash_chain":_load_llm_bash_chain,"llm_checker_chain":_load_llm_checker_chain,"llm_math_chain":_load_llm_math_chain,"llm_requests_chain":_load_llm_requests_chain,"pal_chain":_load_pal_chain,"qa_with_sources_chain":_load_qa_with_sources_chain,"stuff_documents_chain":_load_stuff_documents_chain,"map_reduce_documents_chain":_load_map_reduce_documents_chain,"reduce_documents_chain":_load_reduce_documents_chain,"map_rerank_documents_chain":_load_map_rerank_documents_chain,"refine_documents_chain":_load_refine_documents_chain,"sql_database_chain":_load_sql_database_chain,"vector_db_qa_with_sources_chain":_load_vector_db_qa_with_sources_chain,"vector_db_qa":_load_vector_db_qa,"retrieval_qa":_load_retrieval_qa,"retrieval_qa_with_sources_chain":_load_retrieval_qa_with_sources_chain,"graph_cypher_chain":_load_graph_cypher_chain,}
[docs]@deprecated(since="0.2.13",message=("This function is deprecated and will be removed in langchain 1.0. ""At that point chains must be imported from their respective modules."),removal="1.0",)defload_chain_from_config(config:dict,**kwargs:Any)->Chain:"""Load chain from Config Dict."""if"_type"notinconfig:raiseValueError("Must specify a chain Type in config")config_type=config.pop("_type")ifconfig_typenotintype_to_loader_dict:raiseValueError(f"Loading {config_type} chain not supported")chain_loader=type_to_loader_dict[config_type]returnchain_loader(config,**kwargs)
[docs]@deprecated(since="0.2.13",message=("This function is deprecated and will be removed in langchain 1.0. ""At that point chains must be imported from their respective modules."),removal="1.0",)defload_chain(path:Union[str,Path],**kwargs:Any)->Chain:"""Unified method for loading a chain from LangChainHub or local fs."""ifisinstance(path,str)andpath.startswith("lc://"):raiseRuntimeError("Loading from the deprecated github-based Hub is no longer supported. ""Please use the new LangChain Hub at https://smith.langchain.com/hub ""instead.")return_load_chain_from_file(path,**kwargs)
def_load_chain_from_file(file:Union[str,Path],**kwargs:Any)->Chain:"""Load chain from file."""# Convert file to Path object.ifisinstance(file,str):file_path=Path(file)else:file_path=file# Load from either json or yaml.iffile_path.suffix==".json":withopen(file_path)asf:config=json.load(f)eliffile_path.suffix.endswith((".yaml",".yml")):withopen(file_path,"r")asf:config=yaml.safe_load(f)else:raiseValueError("File type must be json or yaml")# Override default 'verbose' and 'memory' for the chainif"verbose"inkwargs:config["verbose"]=kwargs.pop("verbose")if"memory"inkwargs:config["memory"]=kwargs.pop("memory")# Load the chain from the config now.returnload_chain_from_config(config,**kwargs)