Package: tree-of-thoughts
Paper linkAuthor's implementationIntroductionTree of Thoughts (ToT) is a powerful and flexible algorithm that significantly advances model reasoning by up to 70%. This plug-and-play version allows you to connect your own models and experience superintelligence!Installpipinstalltree-of-thoughtsUsageimportosfromtree_of_thoughtsimportToTAgent,MonteCarloSearchfromdotenvimportload_dotenvfromswarmsimportAgent,OpenAIChatload_dotenv()# Get the API key from the environmentapi_key=os.environ.get("OPENAI_API_KEY")# Initialize an agent from swarmsagent=Agent(agent_name="tree_of_thoughts",agent_description="This agent uses the tree_of_thoughts library to generate thoughts.",system_prompt=None,llm=OpenAIChat(),)# Initialize the ToTAgent class with the API keymodel=ToTAgent(agent,strategy="cot",evaluation_strategy="value",enable_react=True,k=3,)# Initialize the MonteCarloSearch class with the modeltree_of_thoughts=MonteCarloSearch(model)# Define the initial promptinitial_prompt="""Input: 2 8 8 14Possible next steps:2 + 8 = 10 (left: 8 10 14)8 / 2 = 4 (left: 4 8 14)14 + 2 = 16 (left: 8 8 16)2 * 8 = 16 (left: 8 14 16)8 - 2 = 6 (left: 6 8 14)14 - 8 = 6 (left: 2 6 8)14 /  2 = 7 (left: 7 8 8)14 - 2 = 12 (left: 8 8 12)Input: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equationPossible next steps:"""# Define the number of thoughts to generatenum_thoughts=1max_steps=3max_states=4pruning_threshold=0.5# Generate the thoughtssolution=tree_of_thoughts.solve(initial_prompt=initial_prompt,num_thoughts=num_thoughts,max_steps=max_steps,max_states=max_states,pruning_threshold=pruning_threshold,# sleep_time=sleep_time)print(f"Solution:{solution}")ToT with HF LLMTo run Hugging Face Transformers with Tree of Thoughts:importosfromtree_of_thoughtsimportToTAgent,MonteCarloSearchfromdotenvimportload_dotenvfromswarmsimportAgent,HuggingfaceLLMload_dotenv()# Get the API key from the environmentapi_key=os.environ.get("OPENAI_API_KEY")# Initialize an agent from swarmsagent=Agent(agent_name="tree_of_thoughts",agent_description=("This agent uses the tree_of_thoughts library to generate thoughts."),system_prompt=None,llm=HuggingfaceLLM("EleutherAI/gpt-neo-2.7B",),)# Initialize the ToTAgent class with the API keymodel=ToTAgent(agent,strategy="cot",evaluation_strategy="value",enable_react=True,k=3,)# Initialize the MonteCarloSearch class with the modeltree_of_thoughts=MonteCarloSearch(model)# Define the initial promptinitial_prompt="""Input: 2 8 8 14Possible next steps:2 + 8 = 10 (left: 8 10 14)8 / 2 = 4 (left: 4 8 14)14 + 2 = 16 (left: 8 8 16)2 * 8 = 16 (left: 8 14 16)8 - 2 = 6 (left: 6 8 14)14 - 8 = 6 (left: 2 6 8)14 /  2 = 7 (left: 7 8 8)14 - 2 = 12 (left: 8 8 12)Input: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equationPossible next steps:"""# Define the number of thoughts to generatenum_thoughts=1max_steps=3max_states=4pruning_threshold=0.5# Generate the thoughtssolution=tree_of_thoughts.solve(initial_prompt=initial_prompt,num_thoughts=num_thoughts,max_steps=max_steps,max_states=max_states,pruning_threshold=pruning_threshold,# sleep_time=sleep_time)print(f"Solution:{solution}")Basic PromptsCopy and paste this into your llm!"Three experts with exceptional logical thinking skills are collaboratively answering a question using the tree of thoughts method. Each expert will share their thought process in detail, taking into account the previous thoughts of others and admitting any errors. They will iteratively refine and expand upon each other's ideas, giving credit where it's due. The process continues until a conclusive answer is found. Organize the entire response in a markdown table format. The task is:AcknowledgementsThanks to: Shunyu Yao Princeton University, Dian Yu Google DeepMind, Jeffrey Zhao, Google DeepMind, Izhak Shafran Google DeepMind, Thomas L. Griffiths, Princeton University, Yuan Cao Google DeepMind, Karthik Narasimha, Princeton University for sharing this amazing work with the world!And, thanks to Phil Wang or Lucidrains for inspiring me to devote myself to open source AI ResearchLicenseApache