optillm 0.0.2__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.
- optillm-0.0.2/LICENSE +201 -0
- optillm-0.0.2/PKG-INFO +326 -0
- optillm-0.0.2/README.md +291 -0
- optillm-0.0.2/optillm/__init__.py +0 -0
- optillm-0.0.2/optillm/bon.py +50 -0
- optillm-0.0.2/optillm/cot_decoding.py +162 -0
- optillm-0.0.2/optillm/cot_reflection.py +64 -0
- optillm-0.0.2/optillm/entropy_decoding.py +234 -0
- optillm-0.0.2/optillm/leap.py +180 -0
- optillm-0.0.2/optillm/mcts.py +187 -0
- optillm-0.0.2/optillm/moa.py +94 -0
- optillm-0.0.2/optillm/plansearch.py +138 -0
- optillm-0.0.2/optillm/pvg.py +194 -0
- optillm-0.0.2/optillm/reread.py +44 -0
- optillm-0.0.2/optillm/rstar.py +334 -0
- optillm-0.0.2/optillm/rto.py +76 -0
- optillm-0.0.2/optillm/self_consistency.py +92 -0
- optillm-0.0.2/optillm/z3_solver.py +310 -0
- optillm-0.0.2/optillm.egg-info/PKG-INFO +326 -0
- optillm-0.0.2/optillm.egg-info/SOURCES.txt +23 -0
- optillm-0.0.2/optillm.egg-info/dependency_links.txt +1 -0
- optillm-0.0.2/optillm.egg-info/requires.txt +21 -0
- optillm-0.0.2/optillm.egg-info/top_level.txt +1 -0
- optillm-0.0.2/setup.cfg +4 -0
- optillm-0.0.2/setup.py +42 -0
optillm-0.0.2/LICENSE
ADDED
|
@@ -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 [yyyy] [name of copyright owner]
|
|
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.
|
optillm-0.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: optillm
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: An optimizing inference proxy for LLMs.
|
|
5
|
+
Home-page: https://github.com/codelion/optillm
|
|
6
|
+
Author: codelion
|
|
7
|
+
Author-email: codelion@okyasoft.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: networkx
|
|
16
|
+
Requires-Dist: openai
|
|
17
|
+
Requires-Dist: z3-solver
|
|
18
|
+
Requires-Dist: aiohttp
|
|
19
|
+
Requires-Dist: flask
|
|
20
|
+
Requires-Dist: torch
|
|
21
|
+
Requires-Dist: transformers
|
|
22
|
+
Requires-Dist: azure-identity
|
|
23
|
+
Requires-Dist: tiktoken
|
|
24
|
+
Requires-Dist: scikit-learn
|
|
25
|
+
Requires-Dist: litellm
|
|
26
|
+
Requires-Dist: requests
|
|
27
|
+
Requires-Dist: beautifulsoup4
|
|
28
|
+
Requires-Dist: lxml
|
|
29
|
+
Requires-Dist: presidio_analyzer
|
|
30
|
+
Requires-Dist: presidio_anonymizer
|
|
31
|
+
Requires-Dist: nbconvert
|
|
32
|
+
Requires-Dist: nbformat
|
|
33
|
+
Requires-Dist: ipython
|
|
34
|
+
Requires-Dist: ipykernel
|
|
35
|
+
|
|
36
|
+
# optillm
|
|
37
|
+
|
|
38
|
+
optillm is an OpenAI API compatible optimizing inference proxy which implements several state-of-the-art techniques that can improve the accuracy and performance of LLMs. The current focus is on implementing techniques that improve reasoning over coding, logical and mathematical queries. It is possible to beat the frontier models using these techniques across diverse tasks by doing additional compute at inference time.
|
|
39
|
+
|
|
40
|
+
[](https://huggingface.co/spaces/codelion/optillm)
|
|
41
|
+
[](https://colab.research.google.com/drive/1SpuUb8d9xAoTh32M-9wJsB50AOH54EaH?usp=sharing)
|
|
42
|
+
[](https://github.com/codelion/optillm/discussions)
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
Clone the repository with `git` and use `pip install` to setup the dependencies.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
git clone https://github.com/codelion/optillm.git
|
|
50
|
+
cd optillm
|
|
51
|
+
python3 -m venv .venv
|
|
52
|
+
source .venv/bin/activate
|
|
53
|
+
pip install -r requirements.txt
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Set up the `OPENAI_API_KEY` environment variable (for OpenAI)
|
|
57
|
+
or the `AZURE_OPENAI_API_KEY`, `AZURE_API_VERSION` and `AZURE_API_BASE` environment variables (for Azure OpenAI)
|
|
58
|
+
or the `AZURE_API_VERSION` and `AZURE_API_BASE` environment variables and login using `az login` for Azure OpenAI with managed identity (see [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/managed-identity)).
|
|
59
|
+
|
|
60
|
+
You can then run the optillm proxy as follows.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
python optillm.py
|
|
64
|
+
2024-09-06 07:57:14,191 - INFO - Starting server with approach: auto
|
|
65
|
+
2024-09-06 07:57:14,191 - INFO - Server configuration: {'approach': 'auto', 'mcts_simulations': 2, 'mcts_exploration': 0.2, 'mcts_depth': 1, 'best_of_n': 3, 'model': 'gpt-4o-mini', 'rstar_max_depth': 3, 'rstar_num_rollouts': 5, 'rstar_c': 1.4, 'base_url': ''}
|
|
66
|
+
* Serving Flask app 'optillm'
|
|
67
|
+
* Debug mode: off
|
|
68
|
+
2024-09-06 07:57:14,212 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
|
|
69
|
+
* Running on all addresses (0.0.0.0)
|
|
70
|
+
* Running on http://127.0.0.1:8000
|
|
71
|
+
* Running on http://192.168.10.48:8000
|
|
72
|
+
2024-09-06 07:57:14,212 - INFO - Press CTRL+C to quit
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Starting the optillm proxy for a local server (e.g. llama.cpp)
|
|
76
|
+
|
|
77
|
+
- Set the `OPENAI_API_KEY` env variable to a placeholder value
|
|
78
|
+
- e.g. `export OPENAI_API_KEY="no_key"`
|
|
79
|
+
- Run `./llama-server -c 4096 -m path_to_model` to start the server with the specified model and a context length of 4096 tokens
|
|
80
|
+
- Run `python3 optillm.py --base_url base_url` to start the proxy
|
|
81
|
+
- e.g. for llama.cpp, run `python3 optillm.py --base_url http://localhost:8080/v1`
|
|
82
|
+
|
|
83
|
+
> [!WARNING]
|
|
84
|
+
> Note that llama-server currently does not support sampling multiple responses from a model, which limits the available approaches to the following:
|
|
85
|
+
> `cot_reflection`, `leap`, `plansearch`, `rstar`, `rto`, `self_consistency`, `re2`, and `z3`.
|
|
86
|
+
|
|
87
|
+
> [!NOTE]
|
|
88
|
+
> You'll later need to specify a model name in the OpenAI client configuration. Since llama-server was started with a single model, you can choose any name you want.
|
|
89
|
+
|
|
90
|
+
## Usage
|
|
91
|
+
|
|
92
|
+
Once the proxy is running, you can use it as a drop in replacement for an OpenAI client by setting the `base_url` as `http://localhost:8000/v1`.
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import os
|
|
96
|
+
from openai import OpenAI
|
|
97
|
+
|
|
98
|
+
OPENAI_KEY = os.environ.get("OPENAI_API_KEY")
|
|
99
|
+
OPENAI_BASE_URL = "http://localhost:8000/v1"
|
|
100
|
+
client = OpenAI(api_key=OPENAI_KEY, base_url=OPENAI_BASE_URL)
|
|
101
|
+
|
|
102
|
+
response = client.chat.completions.create(
|
|
103
|
+
model="moa-gpt-4o",
|
|
104
|
+
messages=[
|
|
105
|
+
{
|
|
106
|
+
"role": "user",
|
|
107
|
+
"content": "Write a Python program to build an RL model to recite text from any position that the user provides, using only numpy."
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
temperature=0.2
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
print(response)
|
|
114
|
+
```
|
|
115
|
+
The code above applies to both OpenAI and Azure OpenAI, just remember to populate the `OPENAI_API_KEY` env variable with the proper key.
|
|
116
|
+
There are multiple ways to control the optimization techniques, they are applied in the follow order of preference:
|
|
117
|
+
|
|
118
|
+
- You can control the technique you use for optimization by prepending the slug to the model name `{slug}-model-name`. E.g. in the above code we are using `moa` or mixture of agents as the optimization approach. In the proxy logs you will see the following showing the `moa` is been used with the base model as `gpt-4o-mini`.
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
2024-09-06 08:35:32,597 - INFO - Using approach moa, with gpt-4o-mini
|
|
122
|
+
2024-09-06 08:35:35,358 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
|
|
123
|
+
2024-09-06 08:35:39,553 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
|
|
124
|
+
2024-09-06 08:35:44,795 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
|
|
125
|
+
2024-09-06 08:35:44,797 - INFO - 127.0.0.1 - - [06/Sep/2024 08:35:44] "POST /v1/chat/completions HTTP/1.1" 200 -
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- Or, you can pass the slug in the `optillm_approach` field in the `extra_body`.
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
response = client.chat.completions.create(
|
|
132
|
+
model="gpt-4o-mini",
|
|
133
|
+
messages=[{ "role": "user","content": "" }],
|
|
134
|
+
temperature=0.2,
|
|
135
|
+
extra_body={"optillm_approach": "bon|moa|mcts"}
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
- Or, you can just mention the approach in either your `system` or `user` prompt, within `<optillm_approach> </optillm_approach>` tags.
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
response = client.chat.completions.create(
|
|
142
|
+
model="gpt-4o-mini",
|
|
143
|
+
messages=[{ "role": "user","content": "<optillm_approach>re2</optillm_approach> How many r's are there in strawberry?" }],
|
|
144
|
+
temperature=0.2
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
> [!TIP]
|
|
149
|
+
> You can also combine different techniques either by using symbols `&` and `|`. When you use `&` the techniques are processed in the order from left to right in a pipeline
|
|
150
|
+
> with response from previous stage used as request to the next. While, with `|` we run all the requests in parallel and generate multiple responses that are returned as a list.
|
|
151
|
+
|
|
152
|
+
Please note that the convention described above works only when the optillm server has been started with inference approach set to `auto`. Otherwise, the `model` attribute in the client request must be set with the model name only.
|
|
153
|
+
|
|
154
|
+
We now suport all LLM providers (by wrapping around the [LiteLLM sdk](https://docs.litellm.ai/docs/#litellm-python-sdk)). E.g. you can use the Gemini Flash model with `moa` by setting passing the api key in the environment variable `os.environ['GEMINI_API_KEY']` and then calling the model `moa-gemini/gemini-1.5-flash-002`. In the output you will then see that LiteLLM is being used to call the base model.
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
9:43:21 - LiteLLM:INFO: utils.py:2952 -
|
|
158
|
+
LiteLLM completion() model= gemini-1.5-flash-002; provider = gemini
|
|
159
|
+
2024-09-29 19:43:21,011 - INFO -
|
|
160
|
+
LiteLLM completion() model= gemini-1.5-flash-002; provider = gemini
|
|
161
|
+
2024-09-29 19:43:21,481 - INFO - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-002:generateContent?key=[redacted] "HTTP/1.1 200 OK"
|
|
162
|
+
19:43:21 - LiteLLM:INFO: utils.py:988 - Wrapper: Completed Call, calling success_handler
|
|
163
|
+
2024-09-29 19:43:21,483 - INFO - Wrapper: Completed Call, calling success_handler
|
|
164
|
+
19:43:21 - LiteLLM:INFO: utils.py:2952 -
|
|
165
|
+
LiteLLM completion() model= gemini-1.5-flash-002; provider = gemini
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
> [!TIP]
|
|
169
|
+
> optillm is a transparent proxy and will work with any LLM API or provider that has an OpenAI API compatible chat completions endpoint, and in turn, optillm also exposes
|
|
170
|
+
the same OpenAI API compatible chat completions endpoint. This should allow you to integrate it into any existing tools or frameworks easily. If the LLM you want to use
|
|
171
|
+
doesn't have an OpenAI API compatible endpoint (like Google or Anthropic) you can use [LiteLLM proxy server](https://docs.litellm.ai/docs/proxy/quick_start) that supports most LLMs.
|
|
172
|
+
|
|
173
|
+
The following sequence diagram illustrates how the request and responses go through optillm.
|
|
174
|
+
|
|
175
|
+

|
|
176
|
+
|
|
177
|
+
In the diagram:
|
|
178
|
+
- `A` is an existing tool (like [oobabooga](https://github.com/oobabooga/text-generation-webui/)), framework (like [patchwork](https://github.com/patched-codes/patchwork))
|
|
179
|
+
or your own code where you want to use the results from optillm. You can use it directly using any OpenAI client sdk.
|
|
180
|
+
- `B` is the optillm service (running directly or in a docker container) that will send requests to the `base_url`.
|
|
181
|
+
- `C` is any service providing an OpenAI API compatible chat completions endpoint.
|
|
182
|
+
|
|
183
|
+
## Implemented techniques
|
|
184
|
+
|
|
185
|
+
| Approach | Slug | Description |
|
|
186
|
+
| ----------------------- | ------------------ | ---------------------------------------------------------------------------------------------- |
|
|
187
|
+
| CoT with Reflection | `cot_reflection` | Implements chain-of-thought reasoning with \<thinking\>, \<reflection> and \<output\> sections |
|
|
188
|
+
| PlanSearch | `plansearch` | Implements a search algorithm over candidate plans for solving a problem in natural language |
|
|
189
|
+
| ReRead | `re2` | Implements rereading to improve reasoning by processing queries twice |
|
|
190
|
+
| Self-Consistency | `self_consistency` | Implements an advanced self-consistency method |
|
|
191
|
+
| Z3 Solver | `z3` | Utilizes the Z3 theorem prover for logical reasoning |
|
|
192
|
+
| R* Algorithm | `rstar` | Implements the R* algorithm for problem-solving |
|
|
193
|
+
| LEAP | `leap` | Learns task-specific principles from few shot examples |
|
|
194
|
+
| Round Trip Optimization | `rto` | Optimizes responses through a round-trip process |
|
|
195
|
+
| Best of N Sampling | `bon` | Generates multiple responses and selects the best one |
|
|
196
|
+
| Mixture of Agents | `moa` | Combines responses from multiple critiques |
|
|
197
|
+
| Monte Carlo Tree Search | `mcts` | Uses MCTS for decision-making in chat responses |
|
|
198
|
+
| PV Game | `pvg` | Applies a prover-verifier game approach at inference time |
|
|
199
|
+
| CoT Decoding | N/A for proxy | Implements chain-of-thought decoding to elicit reasoning without explicit prompting |
|
|
200
|
+
| Entropy Decoding | N/A for proxy | Implements adaptive sampling based on the uncertainy of tokens during generation |
|
|
201
|
+
|
|
202
|
+
## Implemented plugins
|
|
203
|
+
|
|
204
|
+
| Plugin | Slug | Description |
|
|
205
|
+
| ----------------------- | ------------------ | ---------------------------------------------------------------------------------------------- |
|
|
206
|
+
| Memory | `memory` | Implements a short term memory layer, enables you to use unbounded context length with any LLM |
|
|
207
|
+
| Privacy | `privacy` | Anonymize PII data in request and deanonymize it back to original value in response |
|
|
208
|
+
| Read URLs | `readurls` | Reads all URLs found in the request, fetches the content at the URL and adds it to the context |
|
|
209
|
+
| Execute Code | `executecode` | Enables use of code interpreter to execute python code in requests and LLM generated responses |
|
|
210
|
+
|
|
211
|
+
## Available parameters
|
|
212
|
+
|
|
213
|
+
optillm supports various command-line arguments and environment variables for configuration.
|
|
214
|
+
|
|
215
|
+
| Parameter | Description | Default Value |
|
|
216
|
+
|--------------------------|-----------------------------------------------------------------|-----------------|
|
|
217
|
+
| `--approach` | Inference approach to use | `"auto"` |
|
|
218
|
+
| `--simulations` | Number of MCTS simulations | 2 |
|
|
219
|
+
| `--exploration` | Exploration weight for MCTS | 0.2 |
|
|
220
|
+
| `--depth` | Simulation depth for MCTS | 1 |
|
|
221
|
+
| `--best-of-n` | Number of samples for best_of_n approach | 3 |
|
|
222
|
+
| `--model` | OpenAI model to use | `"gpt-4o-mini"` |
|
|
223
|
+
| `--base-url` | Base URL for OpenAI compatible endpoint | `""` |
|
|
224
|
+
| `--rstar-max-depth` | Maximum depth for rStar algorithm | 3 |
|
|
225
|
+
| `--rstar-num-rollouts` | Number of rollouts for rStar algorithm | 5 |
|
|
226
|
+
| `--rstar-c` | Exploration constant for rStar algorithm | 1.4 |
|
|
227
|
+
| `--n` | Number of final responses to be returned | 1 |
|
|
228
|
+
| `--return-full-response` | Return the full response including the CoT with <thinking> tags | `False` |
|
|
229
|
+
| `--port` | Specify the port to run the proxy | 8000 |
|
|
230
|
+
| `--optillm-api-key` | Optional API key for client authentication to optillm | `""` |
|
|
231
|
+
|
|
232
|
+
When using Docker, these can be set as environment variables prefixed with `OPTILLM_`.
|
|
233
|
+
|
|
234
|
+
## Running with Docker
|
|
235
|
+
|
|
236
|
+
optillm can optionally be built and run using Docker and the provided [Dockerfile](./Dockerfile).
|
|
237
|
+
|
|
238
|
+
### Using Docker Compose
|
|
239
|
+
|
|
240
|
+
1. Make sure you have Docker and Docker Compose installed on your system.
|
|
241
|
+
|
|
242
|
+
2. Either update the environment variables in the docker-compose.yaml file or create a `.env` file in the project root directory and add any environment variables you want to set. For example, to set the OpenAI API key, add the following line to the `.env` file:
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
OPENAI_API_KEY=your_openai_api_key_here
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
3. Run the following command to start optillm:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
docker compose up -d
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
This will build the Docker image if it doesn't exist and start the optillm service.
|
|
255
|
+
|
|
256
|
+
4. optillm will be available at `http://localhost:8000`.
|
|
257
|
+
|
|
258
|
+
When using Docker, you can set these parameters as environment variables. For example, to set the approach and model, you would use:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
OPTILLM_APPROACH=mcts
|
|
262
|
+
OPTILLM_MODEL=gpt-4
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
To secure the optillm proxy with an API key, set the `OPTILLM_API_KEY` environment variable:
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
OPTILLM_API_KEY=your_secret_api_key
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
When the API key is set, clients must include it in their requests using the `Authorization` header:
|
|
272
|
+
|
|
273
|
+
```plain
|
|
274
|
+
Authorization: Bearer your_secret_api_key
|
|
275
|
+
```
|
|
276
|
+
## SOTA results on benchmarks with optillm
|
|
277
|
+
|
|
278
|
+
### readurls&memory-gpt-4o-mini on Google FRAMES Benchmark (Oct 2024)
|
|
279
|
+
| Model | Accuracy |
|
|
280
|
+
| ----- | -------- |
|
|
281
|
+
| readlurls&memory-gpt-4o-mini | 65.66 |
|
|
282
|
+
| gpt-4o-mini | 50.0 |
|
|
283
|
+
| readlurls&memory-Gemma2-9b | 30.1 |
|
|
284
|
+
| Gemma2-9b | 5.1 |
|
|
285
|
+
| Gemma2-27b | 30.8 |
|
|
286
|
+
| Gemini Flash 1.5 | 66.5 |
|
|
287
|
+
| Gemini Pro 1.5 | 72.9 |
|
|
288
|
+
|
|
289
|
+
### plansearch-gpt-4o-mini on LiveCodeBench (Sep 2024)
|
|
290
|
+
|
|
291
|
+
| Model | pass@1 | pass@5 | pass@10 |
|
|
292
|
+
| ---------------------- | ------ | ------ | ------- |
|
|
293
|
+
| plansearch-gpt-4o-mini | 44.03 | 59.31 | 63.5 |
|
|
294
|
+
| gpt-4o-mini | 43.9 | 50.61 | 53.25 |
|
|
295
|
+
| claude-3.5-sonnet | 51.3 | | |
|
|
296
|
+
| gpt-4o-2024-05-13 | 45.2 | | |
|
|
297
|
+
| gpt-4-turbo-2024-04-09 | 44.2 | | |
|
|
298
|
+
|
|
299
|
+
### moa-gpt-4o-mini on Arena-Hard-Auto (Aug 2024)
|
|
300
|
+
|
|
301
|
+

|
|
302
|
+
|
|
303
|
+
### optillm with Patchwork (July 2024)
|
|
304
|
+
|
|
305
|
+
Since optillm is a drop-in replacement for OpenAI API you can easily integrate it with existing tools and frameworks using the OpenAI client. We used optillm with [patchwork](https://github.com/patched-codes/patchwork) which is an open-source framework that automates development gruntwork like PR reviews, bug fixing, security patching using workflows
|
|
306
|
+
called patchflows. We saw huge performance gains across all the supported patchflows as shown below when using the mixutre of agents approach (moa).
|
|
307
|
+
|
|
308
|
+

|
|
309
|
+
|
|
310
|
+
## References
|
|
311
|
+
|
|
312
|
+
- [Entropy Based Sampling and Parallel CoT Decoding](https://github.com/xjdr-alt/entropix) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/entropy_decoding.py)
|
|
313
|
+
- [Fact, Fetch, and Reason: A Unified Evaluation of Retrieval-Augmented Generation](https://arxiv.org/abs/2409.12941) - [Evaluation script](https://github.com/codelion/optillm/blob/main/scripts/eval_frames_benchmark.py)
|
|
314
|
+
- [Writing in the Margins: Better Inference Pattern for Long Context Retrieval](https://www.arxiv.org/abs/2408.14906) - [Inspired the implementation of the memory plugin](https://github.com/codelion/optillm/blob/main/optillm/plugins/memory_plugin.py)
|
|
315
|
+
- [Chain-of-Thought Reasoning Without Prompting](https://arxiv.org/abs/2402.10200) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/cot_decoding.py)
|
|
316
|
+
- [Re-Reading Improves Reasoning in Large Language Models](https://arxiv.org/abs/2309.06275) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/reread.py)
|
|
317
|
+
- [In-Context Principle Learning from Mistakes](https://arxiv.org/abs/2402.05403) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/leap.py)
|
|
318
|
+
- [Planning In Natural Language Improves LLM Search For Code Generation](https://arxiv.org/abs/2409.03733) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/plansearch.py)
|
|
319
|
+
- [Self-Consistency Improves Chain of Thought Reasoning in Language Models](https://arxiv.org/abs/2203.11171) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/self_consistency.py)
|
|
320
|
+
- [Mutual Reasoning Makes Smaller LLMs Stronger Problem-Solvers](https://arxiv.org/abs/2408.06195) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/rstar.py)
|
|
321
|
+
- [Mixture-of-Agents Enhances Large Language Model Capabilities](https://arxiv.org/abs/2406.04692) - [Inspired the implementation of moa](https://github.com/codelion/optillm/blob/main/optillm/moa.py)
|
|
322
|
+
- [Prover-Verifier Games improve legibility of LLM outputs](https://arxiv.org/abs/2407.13692) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/pvg.py)
|
|
323
|
+
- [Monte Carlo Tree Search Boosts Reasoning via Iterative Preference Learning](https://arxiv.org/abs/2405.00451) - [Inspired the implementation of mcts](https://github.com/codelion/optillm/blob/main/optillm/mcts.py)
|
|
324
|
+
- [Unsupervised Evaluation of Code LLMs with Round-Trip Correctness](https://arxiv.org/abs/2402.08699) - [Inspired the implementation of rto](https://github.com/codelion/optillm/blob/main/optillm/rto.py)
|
|
325
|
+
- [Patched MOA: optimizing inference for diverse software development tasks](https://arxiv.org/abs/2407.18521) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/moa.py)
|
|
326
|
+
- [Patched RTC: evaluating LLMs for diverse software development tasks](https://arxiv.org/abs/2407.16557) - [Implementation](https://github.com/codelion/optillm/blob/main/optillm/rto.py)
|