Source code for langchain_experimental.tot.thought_generation
"""We provide two strategies for generating thoughts in the Tree of Thoughts (ToT)framework to avoid repetition:These strategies ensure that the language model generates diverse andnon-repeating thoughts, which are crucial for problem-solving tasks that requireexploration."""fromabcimportabstractmethodfromtypingimportAny,Dict,List,Tuplefromlangchain.chains.llmimportLLMChainfromlangchain_core.prompts.baseimportBasePromptTemplatefrompydanticimportFieldfromlangchain_experimental.tot.promptsimportget_cot_prompt,get_propose_prompt
[docs]classBaseThoughtGenerationStrategy(LLMChain):""" Base class for a thought generation strategy. """c:int=3"""The number of children thoughts to propose at each step."""
[docs]@abstractmethoddefnext_thought(self,problem_description:str,thoughts_path:Tuple[str,...]=(),**kwargs:Any,)->str:""" Generate the next thought given the problem description and the thoughts generated so far. """
[docs]classSampleCoTStrategy(BaseThoughtGenerationStrategy):""" Sample strategy from a Chain-of-Thought (CoT) prompt. This strategy works better when the thought space is rich, such as when each thought is a paragraph. Independent and identically distributed samples lead to diversity, which helps to avoid repetition. """prompt:BasePromptTemplate=Field(default_factory=get_cot_prompt)
[docs]classProposePromptStrategy(BaseThoughtGenerationStrategy):""" Strategy that is sequentially using a "propose prompt". This strategy works better when the thought space is more constrained, such as when each thought is just a word or a line. Proposing different thoughts in the same prompt completion helps to avoid duplication. """prompt:BasePromptTemplate=Field(default_factory=get_propose_prompt)tot_memory:Dict[Tuple[str,...],List[str]]=Field(default_factory=dict)