treequest 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. treequest-0.1.0/.github/workflows/checks.yml +44 -0
  2. treequest-0.1.0/.github/workflows/tests.yml +38 -0
  3. treequest-0.1.0/.gitignore +10 -0
  4. treequest-0.1.0/.python-version +1 -0
  5. treequest-0.1.0/CONTRIBUTING.md +25 -0
  6. treequest-0.1.0/LICENSE +201 -0
  7. treequest-0.1.0/PKG-INFO +210 -0
  8. treequest-0.1.0/README.md +193 -0
  9. treequest-0.1.0/images/figure1.png +0 -0
  10. treequest-0.1.0/pyproject.toml +37 -0
  11. treequest-0.1.0/pytest.ini +2 -0
  12. treequest-0.1.0/src/treequest/__init__.py +15 -0
  13. treequest-0.1.0/src/treequest/algos/ab_mcts_a/algo.py +383 -0
  14. treequest-0.1.0/src/treequest/algos/ab_mcts_a/prob_state.py +472 -0
  15. treequest-0.1.0/src/treequest/algos/ab_mcts_m/algo.py +248 -0
  16. treequest-0.1.0/src/treequest/algos/ab_mcts_m/pymc_interface.py +639 -0
  17. treequest-0.1.0/src/treequest/algos/base.py +46 -0
  18. treequest-0.1.0/src/treequest/algos/best_first_search.py +149 -0
  19. treequest-0.1.0/src/treequest/algos/multi_armed_bandit_ucb.py +144 -0
  20. treequest-0.1.0/src/treequest/algos/standard_mcts.py +240 -0
  21. treequest-0.1.0/src/treequest/algos/tree.py +121 -0
  22. treequest-0.1.0/src/treequest/algos/tree_of_thought_bfs.py +204 -0
  23. treequest-0.1.0/src/treequest/imports.py +97 -0
  24. treequest-0.1.0/src/treequest/py.typed +0 -0
  25. treequest-0.1.0/src/treequest/ranker.py +29 -0
  26. treequest-0.1.0/src/treequest/types.py +17 -0
  27. treequest-0.1.0/src/treequest/visualization.py +82 -0
  28. treequest-0.1.0/tests/__init__.py +0 -0
  29. treequest-0.1.0/tests/test_ab_mcts_a.py +186 -0
  30. treequest-0.1.0/tests/test_ab_mcts_a_visualization.py +257 -0
  31. treequest-0.1.0/tests/test_ab_mcts_m_algo.py +76 -0
  32. treequest-0.1.0/tests/test_ab_mcts_m_algo_advanced.py +318 -0
  33. treequest-0.1.0/tests/test_best_first_search.py +72 -0
  34. treequest-0.1.0/tests/test_interface.py +49 -0
  35. treequest-0.1.0/tests/test_multi_armed_bandit_ucb.py +62 -0
  36. treequest-0.1.0/tests/test_standard_mcts.py +301 -0
  37. treequest-0.1.0/tests/test_tot_bfs.py +88 -0
  38. treequest-0.1.0/tests/test_tree.py +232 -0
@@ -0,0 +1,44 @@
1
+ name: Checks
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request: {}
8
+ schedule:
9
+ - cron: '0 23 * * SUN-THU'
10
+
11
+ concurrency:
12
+ group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_number || github.ref }}
13
+ cancel-in-progress: true
14
+
15
+ jobs:
16
+ checks:
17
+ if: (github.event_name == 'schedule' && github.repository == 'SakanaAI/treequest') || (github.event_name != 'schedule')
18
+ runs-on: ubuntu-latest
19
+
20
+ steps:
21
+ - uses: actions/checkout@v4
22
+
23
+ - name: Install uv
24
+ uses: astral-sh/setup-uv@v4
25
+
26
+ - name: Set up Python
27
+ run: uv python install
28
+
29
+ - name: Install the project
30
+ run: uv sync --all-extras --dev
31
+
32
+ - name: Output installed packages
33
+ run: |
34
+ uv pip list
35
+ uv pip check
36
+
37
+ - name: ruff format
38
+ run: uv run ruff format --check
39
+
40
+ - name: ruff check
41
+ run: uv run ruff check
42
+
43
+ - name: mypy
44
+ run: uv run mypy .
@@ -0,0 +1,38 @@
1
+ name: Tests
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request: {}
8
+ schedule:
9
+ - cron: '0 23 * * SUN-THU'
10
+
11
+ concurrency:
12
+ group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_number || github.ref }}
13
+ cancel-in-progress: true
14
+
15
+ jobs:
16
+ checks:
17
+ if: (github.event_name == 'schedule' && github.repository == 'SakanaAI/treequest') || (github.event_name != 'schedule')
18
+ runs-on: ubuntu-latest
19
+
20
+ steps:
21
+ - uses: actions/checkout@v4
22
+
23
+ - name: Install uv
24
+ uses: astral-sh/setup-uv@v4
25
+
26
+ - name: Set up Python
27
+ run: uv python install
28
+
29
+ - name: Install the project
30
+ run: uv sync --all-extras
31
+
32
+ - name: Output installed packages
33
+ run: |
34
+ uv pip list
35
+ uv pip check
36
+
37
+ - name: Tests
38
+ run: uv run pytest tests -n auto
@@ -0,0 +1,10 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
@@ -0,0 +1 @@
1
+ 3.11
@@ -0,0 +1,25 @@
1
+ # Development setup
2
+ ## Installation for Development
3
+ To install extra dependencies for development, we need to specify all extras and all groups as:
4
+
5
+ ```bash
6
+ # Install uv if you don't have it yet
7
+ brew install uv
8
+
9
+ # clone repository
10
+ git clone https://github.com/SakanaAI/treequest.git
11
+ cd treequest
12
+
13
+ # uv sync creates .venv and insatall all the dependencies
14
+ uv sync --all-extras --all-groups
15
+ ```
16
+
17
+ ## Running test
18
+ ```
19
+ uv run pytest tests -n auto
20
+ ```
21
+
22
+ ## Formatting
23
+ ```
24
+ uv run ruff format
25
+ ```
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025 Sakana AI
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: treequest
3
+ Version: 0.1.0
4
+ Summary: A Tree Search Library with Flexible API for LLM Inference-Time Scaling
5
+ Author-email: Kou Misaki <kou.misaki@sakana.ai>, Takuya Akiba <takiba@sakana.ai>
6
+ License-File: LICENSE
7
+ Requires-Python: >=3.11
8
+ Requires-Dist: scipy>=1.0.0
9
+ Provides-Extra: abmcts-m
10
+ Requires-Dist: jax>=0.5.2; extra == 'abmcts-m'
11
+ Requires-Dist: numpy>=1.26.4; extra == 'abmcts-m'
12
+ Requires-Dist: numpyro>=0.17.0; extra == 'abmcts-m'
13
+ Requires-Dist: pandas>=2.2.3; extra == 'abmcts-m'
14
+ Requires-Dist: pymc>=5.21.0; extra == 'abmcts-m'
15
+ Requires-Dist: xarray>=2025.1.2; extra == 'abmcts-m'
16
+ Description-Content-Type: text/markdown
17
+
18
+ # TreeQuest
19
+ [![Python](https://img.shields.io/badge/python-3.11-blue)](https://www.python.org)
20
+ [![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](https://www.apache.org/licenses/LICENSE-2.0)
21
+ [![Checks status](https://github.com/SakanaAI/treequest/actions/workflows/checks.yml/badge.svg?branch=main)](https://github.com/SakanaAI/treequest/actions)
22
+ [![Tests status](https://github.com/SakanaAI/treequest/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/SakanaAI/treequest/actions)
23
+
24
+ [![arXiv](https://img.shields.io/badge/arXiv-2503.04412-B31B1B?logo=data:image/svg+xml;base64,PHN2ZyBpZD0ibG9nb21hcmsiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDE3LjczMiAyNC4yNjkiPjxnIGlkPSJ0aW55Ij48cGF0aCBkPSJNNTczLjU0OSwyODAuOTE2bDIuMjY2LDIuNzM4LDYuNjc0LTcuODRjLjM1My0uNDcuNTItLjcxNy4zNTMtMS4xMTdhMS4yMTgsMS4yMTgsMCwwLDAtMS4wNjEtLjc0OGgwYS45NTMuOTUzLDAsMCwwLS43MTIuMjYyWiIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTU2Ni45ODQgLTI3MS41NDgpIiBmaWxsPSIjYmRiOWI0Ii8+PHBhdGggZD0iTTU3OS41MjUsMjgyLjIyNWwtMTAuNjA2LTEwLjE3NGExLjQxMywxLjQxMywwLDAsMC0uODM0LS41LDEuMDksMS4wOSwwLDAsM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jd4UxhkY+WZSKnGjr8bhJPfpZTj25FyeLLR+0uUZ4lBUHas9o59Xkm9o1TOcc9XHvhFSsqGSPncK6MDOTZPHR8I2eiVQ7FSjx2/LCmPxsqHk3/msHc80NNgJbzmlKDNOw56JY2VF6/WRpdCNsWO2CuVSDnnFYsdPQOO1Tv/+xprmmbUS3+RzqTKrwLjbhP2acjrUr38cJ8osrha4XabKL0qwlkx3hyr8HwjrfK84JX1XtkdKdVI+bjUxqapB/DfUBzM8Vjs2D/54DkXKW95x3d7L79HvhoMst7xk0iJYqUeKR994VgzNfg4ZKMP2Rcp571i3vGv2PH9marZvDOorPVKX6Scj5S3+ot0XRwzCKIcK3yOn3tl1CtJrMSDynOliffBvNFw7e0DFyoPGVha4K8rz3Du4ljkuD8wXjLYAAyZ8Lsg5TfdNQ7JFdrv/zki5QWv1L3jb3HInfOd9WtOD2TLBW7to7nHTIsWLVq0aNEs/wHPLhDZrKccngAAAABJRU5ErkJggg==)](https://sakana.ai/ab-mcts/)
26
+
27
+ ![AB-MCTS](images/figure1.png)
28
+
29
+ A flexible answer tree search library featuring **AB-MCTS**, useful for (but not limited to) LLM inference-time scaling.
30
+
31
+ ## Quick Start
32
+ ```python
33
+ import random
34
+
35
+ import treequest as tq
36
+
37
+ # Each node is associated with a user-definable `state`.
38
+ State = str
39
+
40
+ # 1. Define a function to be used for node generation.
41
+ def generate(parent_state: State | None) -> tuple[State, float]:
42
+ """Generates new states and scores based on the parent state."""
43
+ if parent_state is None: # None represents the expansion from root.
44
+ new_state = "Initial state"
45
+ else:
46
+ new_state = f"State after {parent_state}"
47
+
48
+ score = evaluate(new_state) # A score for the new state; It should be normalized to the [0, 1] range.
49
+ return new_state, score
50
+
51
+ # 2. Instantiate the algorithm and a search tree object.
52
+ algo = tq.ABMCTSA()
53
+ search_tree = algo.init_tree()
54
+
55
+ # 3. Run the search with a generation budget (10 in this case).
56
+ for _ in range(10):
57
+ search_tree = algo.step(search_tree, {'Action A': generate})
58
+
59
+ # 4. Extract the best score and state.
60
+ best_state, best_node_score = tq.top_k(search_tree, algo, k=1)[0]
61
+ print(f"Best state: {best_state}, Score: {best_node_score}")
62
+ ```
63
+
64
+ ## Features
65
+ - Easy-to-use API with customizable node generation and node scoring logic.
66
+ - **AB-MCTS-A** and **AB-MCTS-M**, as well as **Multi-LLM AB-MCTS** support (See [our paper](https://arxiv.org/abs/2503.04412) for algorithm details).
67
+ - Checkpointing and resuming searches.
68
+
69
+ ## Installation
70
+ ### uv
71
+ First, install [`uv`](https://github.com/astral-sh/uv?tab=readme-ov-file#installation). Then you can install TreeQuest with the following command:
72
+ ```bash
73
+ uv add "treequest[abmcts-m]" "https://github.com/SakanaAI/treequest.git@main"
74
+ ```
75
+
76
+ ### pip
77
+ Alternatively, you can use pip to install TreeQuest:
78
+ ```bash
79
+ pip install "treequest[abmcts-m] @ git+https://github.com/SakanaAI/treequest.git"
80
+ ```
81
+
82
+ ## Usage
83
+ ### Using an LLM as a Node Generator
84
+ You can use any object as a node state. You only need to define a generating function that returns a `(state, score)` tuple and takes the parent state as an argument:
85
+ ```python
86
+ import treequest as tq
87
+ import dataclasses
88
+
89
+ @dataclasses.dataclass
90
+ class State:
91
+ llm_answer: str
92
+ score: float
93
+
94
+ def generate(parent_state: State | None) -> tuple[State, float]:
95
+ """Generate a new node by calling an LLM."""
96
+ if parent_state is None:
97
+ state = initial_generation()
98
+ else:
99
+ state = refine_answer(parent_state.llm_answer, parent_state.score)
100
+
101
+ return state, state.score
102
+
103
+ def initial_generation() -> State:
104
+ """
105
+ Call LLM API to generate an initial answer.
106
+ """
107
+ ...
108
+
109
+ def refine_answer(llm_answer: str, score: float) -> State:
110
+ """
111
+ Call LLM API to refine an answer.
112
+ """
113
+ ...
114
+
115
+
116
+ algo = tq.ABMCTSM()
117
+ search_tree = algo.init_tree()
118
+ for i in range(20):
119
+ search_tree = algo.step(search_tree, {'Action Label': generate})
120
+ # Logging best node during the search.
121
+ if (i + 1) % 5 == 0:
122
+ best_interim_state, _ = tq.top_k(search_tree, algo, k=1)[0]
123
+ print(f"Iteration {i+1}: Best state so far = {best_interim_state}")
124
+
125
+ best_state, _ = tq.top_k(search_tree, algo, k=1)[0]
126
+ print(f"Best Answer: {best_state.llm_answer}, Best Score: {best_state.score}")
127
+ ```
128
+
129
+ ### Using Multiple LLMs (and Beyond)
130
+
131
+ TreeQuest supports multiple action types. For example, you can provide multiple generation functions backed by different LLMs to represent different action types:
132
+
133
+ ```python
134
+ from functools import partial
135
+
136
+ import treequest as tq
137
+
138
+ def generate(llm_name: str, parent_state=None):
139
+ """
140
+ Call LLM API using litellm, vllm, etc., to generate a new node
141
+ """
142
+ ...
143
+ return new_state, new_score
144
+
145
+ llm_names = ["o4-mini", "gemini-2.5-pro"]
146
+ # Create dict of different actions backed by different LLMs.
147
+ generate_fns = {llm_name: partial(generate, llm_name=llm_name) for llm_name in llm_names}
148
+
149
+ algo = tq.StandardMCTS()
150
+ search_tree = algo.init_tree()
151
+ for _ in range(20):
152
+ search_tree = algo.step(search_tree, generate_fns)
153
+ ```
154
+ The variation is not limited to LLM types; you can use different prompts, actions, scoring logic, etc. in `generate_fns`.
155
+
156
+ ## Algorithms
157
+
158
+ ### ABMCTS-A: ABMCTS with Node Aggregation
159
+
160
+ ABMCTS-A uses node aggregation for adaptive branching:
161
+
162
+ ```python
163
+ import treequest as tq
164
+
165
+ # Instantiate the ABMCTS-A algorithm.
166
+ ab_mcts_a = tq.ABMCTSA()
167
+
168
+ search_tree = ab_mcts_a.init_tree()
169
+ for _ in range(50):
170
+ search_tree = ab_mcts_a.step(search_tree, generate_fns)
171
+ ```
172
+
173
+ ### ABMCTS-M: ABMCTS with Mixed Models
174
+
175
+ ABMCTS-M leverages PyMC's mixed modeling capabilities:
176
+
177
+ ```python
178
+ import treequest as tq
179
+
180
+ # Instantiate the ABMCTS-M algorithm.
181
+ ab_mcts_m = tq.ABMCTSM()
182
+
183
+ search_tree = ab_mcts_m.init_tree()
184
+ for _ in range(30):
185
+ search_tree = ab_mcts_m.step(search_tree, generate_fns)
186
+ ```
187
+
188
+ **NOTE**: To run AB-MCTS-M, you need to install extra dependencies with the `treequest[abmcts-m]` option.
189
+
190
+ ## Requirements
191
+
192
+ - Python 3.11+
193
+
194
+ ## Contributing
195
+
196
+ Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for development tips.
197
+
198
+ ## Citation
199
+ ```bibtex
200
+ @article{inoue2025wider,
201
+ title={Wider or Deeper? Scaling LLM Inference-Time Compute with Adaptive Branching Tree Search},
202
+ author={Inoue, Yuichi and Misaki, Kou and Imajuku, Yuki and Kuroki, So and Nakamura, Taishi and Akiba, Takuya},
203
+ journal={arXiv preprint arXiv:2503.04412},
204
+ year={2025}
205
+ }
206
+ ```
207
+
208
+ ## License
209
+
210
+ [Apache 2.0](./LICENSE)