Source code for langchain.chains.llm_summarization_checker.base
"""Chain for summarization with self-verification."""from__future__importannotationsimportwarningsfrompathlibimportPathfromtypingimportAny,Dict,List,Optionalfromlangchain_core._apiimportdeprecatedfromlangchain_core.callbacksimportCallbackManagerForChainRunfromlangchain_core.language_modelsimportBaseLanguageModelfromlangchain_core.prompts.promptimportPromptTemplatefrompydanticimportConfigDict,model_validatorfromlangchain.chains.baseimportChainfromlangchain.chains.llmimportLLMChainfromlangchain.chains.sequentialimportSequentialChainPROMPTS_DIR=Path(__file__).parent/"prompts"CREATE_ASSERTIONS_PROMPT=PromptTemplate.from_file(PROMPTS_DIR/"create_facts.txt")CHECK_ASSERTIONS_PROMPT=PromptTemplate.from_file(PROMPTS_DIR/"check_facts.txt")REVISED_SUMMARY_PROMPT=PromptTemplate.from_file(PROMPTS_DIR/"revise_summary.txt")ARE_ALL_TRUE_PROMPT=PromptTemplate.from_file(PROMPTS_DIR/"are_all_true_prompt.txt")def_load_sequential_chain(llm:BaseLanguageModel,create_assertions_prompt:PromptTemplate,check_assertions_prompt:PromptTemplate,revised_summary_prompt:PromptTemplate,are_all_true_prompt:PromptTemplate,verbose:bool=False,)->SequentialChain:chain=SequentialChain(chains=[LLMChain(llm=llm,prompt=create_assertions_prompt,output_key="assertions",verbose=verbose,),LLMChain(llm=llm,prompt=check_assertions_prompt,output_key="checked_assertions",verbose=verbose,),LLMChain(llm=llm,prompt=revised_summary_prompt,output_key="revised_summary",verbose=verbose,),LLMChain(llm=llm,output_key="all_true",prompt=are_all_true_prompt,verbose=verbose,),],input_variables=["summary"],output_variables=["all_true","revised_summary"],verbose=verbose,)returnchain
[docs]@deprecated(since="0.2.13",message=("See LangGraph guides for a variety of self-reflection and corrective ""strategies for question-answering and other tasks: ""https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_self_rag/"),removal="1.0",)classLLMSummarizationCheckerChain(Chain):"""Chain for question-answering with self-verification. Example: .. code-block:: python from langchain_community.llms import OpenAI from langchain.chains import LLMSummarizationCheckerChain llm = OpenAI(temperature=0.0) checker_chain = LLMSummarizationCheckerChain.from_llm(llm) """sequential_chain:SequentialChainllm:Optional[BaseLanguageModel]=None"""[Deprecated] LLM wrapper to use."""create_assertions_prompt:PromptTemplate=CREATE_ASSERTIONS_PROMPT"""[Deprecated]"""check_assertions_prompt:PromptTemplate=CHECK_ASSERTIONS_PROMPT"""[Deprecated]"""revised_summary_prompt:PromptTemplate=REVISED_SUMMARY_PROMPT"""[Deprecated]"""are_all_true_prompt:PromptTemplate=ARE_ALL_TRUE_PROMPT"""[Deprecated]"""input_key:str="query"#: :meta private:output_key:str="result"#: :meta private:max_checks:int=2"""Maximum number of times to check the assertions. Default to double-checking."""model_config=ConfigDict(arbitrary_types_allowed=True,extra="forbid",)@model_validator(mode="before")@classmethoddefraise_deprecation(cls,values:Dict)->Any:if"llm"invalues:warnings.warn("Directly instantiating an LLMSummarizationCheckerChain with an llm is ""deprecated. Please instantiate with"" sequential_chain argument or using the from_llm class method.")if"sequential_chain"notinvaluesandvalues["llm"]isnotNone:values["sequential_chain"]=_load_sequential_chain(values["llm"],values.get("create_assertions_prompt",CREATE_ASSERTIONS_PROMPT),values.get("check_assertions_prompt",CHECK_ASSERTIONS_PROMPT),values.get("revised_summary_prompt",REVISED_SUMMARY_PROMPT),values.get("are_all_true_prompt",ARE_ALL_TRUE_PROMPT),verbose=values.get("verbose",False),)returnvalues@propertydefinput_keys(self)->List[str]:"""Return the singular input key. :meta private: """return[self.input_key]@propertydefoutput_keys(self)->List[str]:"""Return the singular output key. :meta private: """return[self.output_key]def_call(self,inputs:Dict[str,Any],run_manager:Optional[CallbackManagerForChainRun]=None,)->Dict[str,str]:_run_manager=run_managerorCallbackManagerForChainRun.get_noop_manager()all_true=Falsecount=0output=Noneoriginal_input=inputs[self.input_key]chain_input=original_inputwhilenotall_trueandcount<self.max_checks:output=self.sequential_chain({"summary":chain_input},callbacks=_run_manager.get_child())count+=1ifoutput["all_true"].strip()=="True":breakifself.verbose:print(output["revised_summary"])# noqa: T201chain_input=output["revised_summary"]ifnotoutput:raiseValueError("No output from chain")return{self.output_key:output["revised_summary"].strip()}@propertydef_chain_type(self)->str:return"llm_summarization_checker_chain"