tree-of-thoughts


Nametree-of-thoughts JSON
Version 0.4.8 PyPI version JSON
download
home_pagehttps://github.com/kyegomez/tree-of-thoughts
SummaryTree of Thoughts - Pytorch
upload_time2024-02-24 08:11:55
maintainer
docs_urlNone
authorKye Gomez
requires_python>=3.6,<4.0
licenseMIT
keywords artificial intelligence deep learning optimizers prompt engineering
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Multi-Modality](imags/agorabanner.png)](https://discord.gg/qUtxnK2NMf)

![Tree of Thoughts Banner](images/treeofthoughts.png)

![Discord](https://img.shields.io/discord/999382051935506503)
[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)](https://twitter.com/intent/tweet?text=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts!%20https://github.com/kyegomez/tree-of-thoughts)
[![LinkedIn](https://img.shields.io/badge/Share-LinkedIn-blue?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)
[![Facebook](https://img.shields.io/badge/Share-Facebook-blue?style=social&logo=facebook)](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)
[![Reddit](https://img.shields.io/badge/Share-Reddit-orange?style=social&logo=reddit)](https://www.reddit.com/submit?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&title=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)
[![Hacker News](https://img.shields.io/badge/Share-Hacker%20News-orange?style=social&logo=y-combinator)](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&t=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)
[![Pinterest](https://img.shields.io/badge/Share-Pinterest-red?style=social&logo=pinterest)](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&media=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts%2Fraw%2Fmain%2Ftree-of-thoughts.jpeg&description=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)
[![WhatsApp](https://img.shields.io/badge/Share-WhatsApp-green?style=social&logo=whatsapp)](https://api.whatsapp.com/send?text=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21%20https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)


[Paper link](https://arxiv.org/pdf/2305.10601.pdf)
[Author's implementation](https://github.com/princeton-nlp/tree-of-thought-llm)

## Introduction

Tree 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!


## Install

```bash
pip install tree-of-thoughts
```

## Usage
```python
import os
from tree_of_thoughts import ToTAgent, MonteCarloSearch
from dotenv import load_dotenv
from swarms import Agent, OpenAIChat

load_dotenv()

# Get the API key from the environment
api_key = os.environ.get("OPENAI_API_KEY")

# Initialize an agent from swarms
agent = 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 key
model = ToTAgent(
    agent,
    strategy="cot",
    evaluation_strategy="value",
    enable_react=True,
    k=3,
)


# Initialize the MonteCarloSearch class with the model
tree_of_thoughts = MonteCarloSearch(model)

# Define the initial prompt
initial_prompt = """


Input: 2 8 8 14
Possible 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 equation
Possible next steps:
"""

# Define the number of thoughts to generate
num_thoughts = 1
max_steps = 3
max_states = 4
pruning_threshold = 0.5


# Generate the thoughts
solution = 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 LLM

To run Hugging Face Transformers with Tree of Thoughts:
```python
import os
from tree_of_thoughts import ToTAgent, MonteCarloSearch
from dotenv import load_dotenv
from swarms import Agent, HuggingfaceLLM

load_dotenv()

# Get the API key from the environment
api_key = os.environ.get("OPENAI_API_KEY")

# Initialize an agent from swarms
agent = 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 key
model = ToTAgent(
    agent,
    strategy="cot",
    evaluation_strategy="value",
    enable_react=True,
    k=3,
)


# Initialize the MonteCarloSearch class with the model
tree_of_thoughts = MonteCarloSearch(model)

# Define the initial prompt
initial_prompt = """


Input: 2 8 8 14
Possible 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 equation
Possible next steps:
"""

# Define the number of thoughts to generate
num_thoughts = 1
max_steps = 3
max_states = 4
pruning_threshold = 0.5


# Generate the thoughts
solution = 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 Prompts
- Copy 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:
```



# Acknowledgements

Thanks 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 Research

# License
Apache
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kyegomez/tree-of-thoughts",
    "name": "tree-of-thoughts",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6,<4.0",
    "maintainer_email": "",
    "keywords": "artificial intelligence,deep learning,optimizers,Prompt Engineering",
    "author": "Kye Gomez",
    "author_email": "kye@apac.ai",
    "download_url": "https://files.pythonhosted.org/packages/c3/6b/cd5e92ec2fc1c329898b6b5a14462712e74e8cd7ed75e0dad4932f748f9b/tree_of_thoughts-0.4.8.tar.gz",
    "platform": null,
    "description": "[![Multi-Modality](imags/agorabanner.png)](https://discord.gg/qUtxnK2NMf)\n\n![Tree of Thoughts Banner](images/treeofthoughts.png)\n\n![Discord](https://img.shields.io/discord/999382051935506503)\n[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)](https://twitter.com/intent/tweet?text=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts!%20https://github.com/kyegomez/tree-of-thoughts)\n[![LinkedIn](https://img.shields.io/badge/Share-LinkedIn-blue?style=social&logo=linkedin)](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)\n[![Facebook](https://img.shields.io/badge/Share-Facebook-blue?style=social&logo=facebook)](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)\n[![Reddit](https://img.shields.io/badge/Share-Reddit-orange?style=social&logo=reddit)](https://www.reddit.com/submit?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&title=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)\n[![Hacker News](https://img.shields.io/badge/Share-Hacker%20News-orange?style=social&logo=y-combinator)](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&t=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)\n[![Pinterest](https://img.shields.io/badge/Share-Pinterest-red?style=social&logo=pinterest)](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts&media=https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts%2Fraw%2Fmain%2Ftree-of-thoughts.jpeg&description=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21)\n[![WhatsApp](https://img.shields.io/badge/Share-WhatsApp-green?style=social&logo=whatsapp)](https://api.whatsapp.com/send?text=Check%20out%20this%20amazing%20project%20on%20improving%20AI%20reasoning%20-%20Tree%20of%20Thoughts%21%20https%3A%2F%2Fgithub.com%2Fkyegomez%2Ftree-of-thoughts)\n\n\n[Paper link](https://arxiv.org/pdf/2305.10601.pdf)\n[Author's implementation](https://github.com/princeton-nlp/tree-of-thought-llm)\n\n## Introduction\n\nTree 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!\n\n\n## Install\n\n```bash\npip install tree-of-thoughts\n```\n\n## Usage\n```python\nimport os\nfrom tree_of_thoughts import ToTAgent, MonteCarloSearch\nfrom dotenv import load_dotenv\nfrom swarms import Agent, OpenAIChat\n\nload_dotenv()\n\n# Get the API key from the environment\napi_key = os.environ.get(\"OPENAI_API_KEY\")\n\n# Initialize an agent from swarms\nagent = Agent(\n    agent_name=\"tree_of_thoughts\",\n    agent_description=\"This agent uses the tree_of_thoughts library to generate thoughts.\",\n    system_prompt=None,\n    llm = OpenAIChat(),   \n)\n\n# Initialize the ToTAgent class with the API key\nmodel = ToTAgent(\n    agent,\n    strategy=\"cot\",\n    evaluation_strategy=\"value\",\n    enable_react=True,\n    k=3,\n)\n\n\n# Initialize the MonteCarloSearch class with the model\ntree_of_thoughts = MonteCarloSearch(model)\n\n# Define the initial prompt\ninitial_prompt = \"\"\"\n\n\nInput: 2 8 8 14\nPossible next steps:\n2 + 8 = 10 (left: 8 10 14)\n8 / 2 = 4 (left: 4 8 14)\n14 + 2 = 16 (left: 8 8 16)\n2 * 8 = 16 (left: 8 14 16)\n8 - 2 = 6 (left: 6 8 14)\n14 - 8 = 6 (left: 2 6 8)\n14 /  2 = 7 (left: 7 8 8)\n14 - 2 = 12 (left: 8 8 12)\nInput: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equation\nPossible next steps:\n\"\"\"\n\n# Define the number of thoughts to generate\nnum_thoughts = 1\nmax_steps = 3\nmax_states = 4\npruning_threshold = 0.5\n\n\n# Generate the thoughts\nsolution = tree_of_thoughts.solve(\n    initial_prompt=initial_prompt,\n    num_thoughts=num_thoughts,\n    max_steps=max_steps,\n    max_states=max_states,\n    pruning_threshold=pruning_threshold,\n    # sleep_time=sleep_time\n)\n\nprint(f\"Solution: {solution}\")\n\n\n```\n\n\n### ToT with HF LLM\n\nTo run Hugging Face Transformers with Tree of Thoughts:\n```python\nimport os\nfrom tree_of_thoughts import ToTAgent, MonteCarloSearch\nfrom dotenv import load_dotenv\nfrom swarms import Agent, HuggingfaceLLM\n\nload_dotenv()\n\n# Get the API key from the environment\napi_key = os.environ.get(\"OPENAI_API_KEY\")\n\n# Initialize an agent from swarms\nagent = Agent(\n    agent_name=\"tree_of_thoughts\",\n    agent_description=(\n        \"This agent uses the tree_of_thoughts library to generate thoughts.\"\n    ),\n    system_prompt=None,\n    llm=HuggingfaceLLM(\n        \"EleutherAI/gpt-neo-2.7B\",\n    ),\n)\n\n# Initialize the ToTAgent class with the API key\nmodel = ToTAgent(\n    agent,\n    strategy=\"cot\",\n    evaluation_strategy=\"value\",\n    enable_react=True,\n    k=3,\n)\n\n\n# Initialize the MonteCarloSearch class with the model\ntree_of_thoughts = MonteCarloSearch(model)\n\n# Define the initial prompt\ninitial_prompt = \"\"\"\n\n\nInput: 2 8 8 14\nPossible next steps:\n2 + 8 = 10 (left: 8 10 14)\n8 / 2 = 4 (left: 4 8 14)\n14 + 2 = 16 (left: 8 8 16)\n2 * 8 = 16 (left: 8 14 16)\n8 - 2 = 6 (left: 6 8 14)\n14 - 8 = 6 (left: 2 6 8)\n14 /  2 = 7 (left: 7 8 8)\n14 - 2 = 12 (left: 8 8 12)\nInput: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equation\nPossible next steps:\n\"\"\"\n\n# Define the number of thoughts to generate\nnum_thoughts = 1\nmax_steps = 3\nmax_states = 4\npruning_threshold = 0.5\n\n\n# Generate the thoughts\nsolution = tree_of_thoughts.solve(\n    initial_prompt=initial_prompt,\n    num_thoughts=num_thoughts,\n    max_steps=max_steps,\n    max_states=max_states,\n    pruning_threshold=pruning_threshold,\n    # sleep_time=sleep_time\n)\n\nprint(f\"Solution: {solution}\")\n\n```\n\n### Basic Prompts\n- Copy and paste this into your llm!\n\n```\n\"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:\n```\n\n\n\n# Acknowledgements\n\nThanks 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!\n\nAnd, thanks to Phil Wang or Lucidrains for inspiring me to devote myself to open source AI Research\n\n# License\nApache",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Tree of Thoughts - Pytorch",
    "version": "0.4.8",
    "project_urls": {
        "Homepage": "https://github.com/kyegomez/tree-of-thoughts"
    },
    "split_keywords": [
        "artificial intelligence",
        "deep learning",
        "optimizers",
        "prompt engineering"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9a0d26090235a9432bec241d9650bfa749d93a7c04c0b5c11a6098d61954ba78",
                "md5": "1e5b8fd37b3c1ba639c440dbf21c5b2f",
                "sha256": "be00245a8b6e51f6eebac8134b8e9868feec9b069d1e530c186d9953ebf0fc7d"
            },
            "downloads": -1,
            "filename": "tree_of_thoughts-0.4.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1e5b8fd37b3c1ba639c440dbf21c5b2f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6,<4.0",
            "size": 14862,
            "upload_time": "2024-02-24T08:11:53",
            "upload_time_iso_8601": "2024-02-24T08:11:53.607830Z",
            "url": "https://files.pythonhosted.org/packages/9a/0d/26090235a9432bec241d9650bfa749d93a7c04c0b5c11a6098d61954ba78/tree_of_thoughts-0.4.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c36bcd5e92ec2fc1c329898b6b5a14462712e74e8cd7ed75e0dad4932f748f9b",
                "md5": "b1b255eaed533314567e9c9fb03eb9bc",
                "sha256": "18ab6ab33ccc21c18babbd0bce44d1cacd4491369989e69a24d4faf1ad7f9ae0"
            },
            "downloads": -1,
            "filename": "tree_of_thoughts-0.4.8.tar.gz",
            "has_sig": false,
            "md5_digest": "b1b255eaed533314567e9c9fb03eb9bc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6,<4.0",
            "size": 16415,
            "upload_time": "2024-02-24T08:11:55",
            "upload_time_iso_8601": "2024-02-24T08:11:55.508769Z",
            "url": "https://files.pythonhosted.org/packages/c3/6b/cd5e92ec2fc1c329898b6b5a14462712e74e8cd7ed75e0dad4932f748f9b/tree_of_thoughts-0.4.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-24 08:11:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kyegomez",
    "github_project": "tree-of-thoughts",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "tree-of-thoughts"
}
        
Elapsed time: 0.19909s