### Install Treequest Development Dependencies with uv Source: https://github.com/sakanaai/treequest/blob/main/CONTRIBUTING.md This snippet outlines the steps to set up the development environment for the Treequest project. It includes instructions for installing `uv` (if not already present), cloning the repository, and using `uv sync` to install all necessary development dependencies and extras. ```bash # Install uv if you don't have it yet brew install uv # clone repository git clone https://github.com/SakanaAI/treequest.git cd treequest # uv sync creates .venv and insatall all the dependencies uv sync --all-extras --all-groups ``` -------------------------------- ### Format Treequest Code with ruff Source: https://github.com/sakanaai/treequest/blob/main/CONTRIBUTING.md This command formats the source code of the Treequest project using `ruff`. Running this ensures a consistent code style across the entire codebase, which significantly improves readability and maintainability for all contributors. ```bash uv run ruff format ``` -------------------------------- ### Add Pre-commit Hook for Treequest Source: https://github.com/sakanaai/treequest/blob/main/CONTRIBUTING.md This command installs the pre-commit hooks for the Treequest project. It ensures that code quality checks and formatting are run automatically before each commit, helping maintain consistent code standards. ```bash uv run pre-commit install ``` -------------------------------- ### Run Treequest Tests with pytest Source: https://github.com/sakanaai/treequest/blob/main/CONTRIBUTING.md This command executes the test suite for the Treequest project. It leverages `uv run pytest` with the `-n auto` flag to automatically determine the optimal number of parallel processes for efficient and fast test execution. ```bash uv run pytest tests -n auto ``` -------------------------------- ### Install TreeQuest with uv and ABMCTS-M Dependencies Source: https://github.com/sakanaai/treequest/blob/main/README.md This Bash command installs the TreeQuest library using the `uv` package manager. The `[abmcts-m]` extra specifies that additional dependencies required for the ABMCTS-M algorithm, such as PyMC, should also be installed. ```Bash uv add "treequest[abmcts-m]" ``` -------------------------------- ### Install TreeQuest with pip and ABMCTS-M Dependencies Source: https://github.com/sakanaai/treequest/blob/main/README.md This Bash command installs the TreeQuest library using the `pip` package manager. The `[abmcts-m]` extra ensures that all necessary dependencies for running the ABMCTS-M algorithm, including PyMC, are installed alongside the core library. ```Bash pip install "treequest[abmcts-m]" ``` -------------------------------- ### Using the ABMCTS-M Algorithm in TreeQuest Source: https://github.com/sakanaai/treequest/blob/main/README.md This Python code illustrates how to instantiate and run the `ABMCTSM` algorithm within the TreeQuest framework. It sets up a search tree and iteratively applies the algorithm's step function, leveraging `generate_fns` (as shown in the multi-LLM example) for node generation, noting the requirement for extra dependencies like PyMC. ```Python import treequest as tq # Instantiate the ABMCTS-M algorithm. ab_mcts_m = tq.ABMCTSM() search_tree = ab_mcts_m.init_tree() for _ in range(30): search_tree = ab_mcts_m.step(search_tree, generate_fns) ``` -------------------------------- ### Using the ABMCTS-A Algorithm in TreeQuest Source: https://github.com/sakanaai/treequest/blob/main/README.md This Python snippet demonstrates the instantiation and basic usage of the `ABMCTSA` algorithm from the TreeQuest library. It initializes a search tree and performs multiple steps of the search, utilizing a predefined `generate_fns` (as shown in the multi-LLM example) for node generation with adaptive branching and node aggregation. ```Python import treequest as tq # Instantiate the ABMCTS-A algorithm. ab_mcts_a = tq.ABMCTSA() search_tree = ab_mcts_a.init_tree() for _ in range(50): search_tree = ab_mcts_a.step(search_tree, generate_fns) ``` -------------------------------- ### Using an LLM as a Node Generator in TreeQuest Source: https://github.com/sakanaai/treequest/blob/main/README.md This comprehensive Python example illustrates how to integrate an LLM as a node generator within a TreeQuest search. It defines a `State` dataclass, implements a `generate` function that simulates LLM calls for initial generation or refinement, and then executes an ABMCTS-M search, logging interim best states and the final optimal answer. ```Python import dataclasses import treequest as tq @dataclasses.dataclass class State: llm_answer: str score: float def generate(parent_state: State | None) -> tuple[State, float]: """Generate a new node by calling an LLM.""" if parent_state is None: state = initial_generation() else: state = refine_answer(parent_state.llm_answer, parent_state.score) return state, state.score def initial_generation() -> State: """ Call LLM API to generate an initial answer. """ ... def refine_answer(llm_answer: str, score: float) -> State: """ Call LLM API to refine an answer. """ ... algo = tq.ABMCTSM() search_tree = algo.init_tree() for i in range(20): search_tree = algo.step(search_tree, {'Action Label': generate}) # Logging best node during the search. if (i + 1) % 5 == 0: best_interim_state, _ = tq.top_k(search_tree, algo, k=1)[0] print(f"Iteration {i+1}: Best state so far = {best_interim_state}") best_state, _ = tq.top_k(search_tree, algo, k=1)[0] print(f"Best Answer: {best_state.llm_answer}, Best Score: {best_state.score}") ``` -------------------------------- ### Implementing Multi-LLM Node Generation in TreeQuest Source: https://github.com/sakanaai/treequest/blob/main/README.md This Python code demonstrates how to use multiple LLMs or different action types as node generators within TreeQuest. It defines a generic `generate` function parameterized by LLM name and creates a dictionary of partial functions, allowing the `algo.step` method to dynamically select different generation strategies during the search process. ```Python from functools import partial import treequest as tq def generate(llm_name: str, parent_state=None): """ Call LLM API using litellm, vllm, etc., to generate a new node """ ... return new_state, new_score llm_names = ["o4-mini", "gemini-2.5-pro"] # Create dict of different actions backed by different LLMs. generate_fns = {llm_name: partial(generate, llm_name=llm_name) for llm_name in llm_names} algo = tq.StandardMCTS() search_tree = algo.init_tree() for _ in range(20): search_tree = algo.step(search_tree, generate_fns) ``` -------------------------------- ### Perform AB-MCTS Search with TreeQuest in Python Source: https://github.com/sakanaai/treequest/blob/main/README.md This Python snippet demonstrates how to use the TreeQuest library to perform an AB-MCTS (Adaptive Branching Monte Carlo Tree Search) algorithm. It defines a state generation function, initializes the AB-MCTS algorithm, and runs a search for a specified budget, which is useful for tasks like LLM inference-time scaling. ```python import random import treequest as tq # Each node is associated with a user-definable `state`. State = str # 1. Define a function to be used for node generation. def generate(parent_state: State | None) -> tuple[State, float]: """Generates new states and scores based on the parent state.""" if parent_state is None: # None represents the expansion from root. new_state = "Initial state" else: new_state = f"State after {parent_state}" score = random.random() # A score for the new state; It should be normalized to the [0, 1] range. return new_state, score # 2. Instantiate the algorithm and a search tree object. algo = tq.ABMCTSA() search_tree = algo.init_tree() # 3. Run the search with a generation budget (10 in this case). for _ in range(10): search_tree = algo.step(search_tree, {'Action A': generate}) ``` -------------------------------- ### BibTeX Citation for TreeQuest Research Paper Source: https://github.com/sakanaai/treequest/blob/main/README.md This BibTeX entry provides the academic citation details for the research paper titled "Wider or Deeper? Scaling LLM Inference-Time Compute with Adaptive Branching Tree Search." This paper describes the underlying algorithms and theoretical foundations implemented in the TreeQuest library. ```BibTeX @article{inoue2025wider, title={Wider or Deeper? Scaling LLM Inference-Time Compute with Adaptive Branching Tree Search}, author={Inoue, Yuichi and Misaki, Kou and Imajuku, Yuki and Kuroki, So and Nakamura, Taishi and Akiba, Takuya}, journal={arXiv preprint arXiv:2503.04412}, year={2025} } ``` -------------------------------- ### Extracting Best State and Score from TreeQuest Search Source: https://github.com/sakanaai/treequest/blob/main/README.md This Python snippet demonstrates how to retrieve the best state and its corresponding score from a completed TreeQuest search tree. It utilizes the `tq.top_k` function to find the top-ranked node and then prints the extracted best state and score to the console for immediate feedback. ```Python best_state, best_node_score = tq.top_k(search_tree, algo, k=1)[0] print(f"Best state: {best_state}, Score: {best_node_score}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.