Skip to content

Commit eefe7ed

Browse files
authored
Merge pull request #40 from microsoft/0.1.3.5
Update to 0.1.3.5. Change default to use LiteLLM backend. (Autogen backend is still supported). Add custom openai backend support too.
2 parents 49e9cff + 07cf80f commit eefe7ed

File tree

16 files changed

+162
-77
lines changed

16 files changed

+162
-77
lines changed

README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ Or for development, clone the repo and run the following.
3131

3232
pip install -e .
3333

34-
The library requires Python >= 3.9. The installation script will git
35-
clone [AutoGen](https://github.com/microsoft/autogen). You may require [Git Large File Storage](https://git-lfs.com/) if
36-
git is unable to clone the repository otherwise.
34+
The library requires Python >= 3.9. By default (starting with v0.1.3.5), we use [LiteLLM](https://github.com/BerriAI/litellm) as the backend of LLMs. For backward compatibility, we provide backend-support with [AutoGen](https://github.com/microsoft/autogen); when installing, users can add `[autogen]` tag to install a compatible AutoGen version (e.g., `pip install trace-opt[autogen]`). You may require [Git Large File Storage](https://git-lfs.com/) if
35+
git is unable to clone the repository.
3736

3837
## Updates
3938
- **2025.2.7** Trace was featured in the [G-Research NeurIPS highlight](https://www.gresearch.com/news/neurips-paper-reviews-2024-8/) by the Science Director Hugh Salimbeni.
4039
- **2024.12.10** Trace was demoed in person at NeurIPS 2024 Expo.
41-
- **2024.11.05** Ching-An Cheng gave a talk at UW Robotics Colloquium on Trace: [video](https://www.youtube.com/watch?v=T2g1Vo3u_9g).
40+
- **2024.11.05** Ching-An Cheng gave a talk at UW Robotics Colloquium on Trace: [video](https://www.youtube.com/watch?v=T2g1Vo3u_9g).
4241
- **2024.10.21** New [paper](https://arxiv.org/abs/2410.15625) by Nvidia, Stanford, Visa, & Intel applies Trace to
4342
optimize for mapper code of parallel programming (for scientific computing and matrix multiplication). Trace (OptoPrime) learns code achieving 1.3X speed up under 10
4443
minutes, compared to the code optimized by a system engineer expert.
@@ -103,7 +102,6 @@ Now, after declaring what is trainable and what isn't, and use `node` and `bundl
103102
can use the optimizer to optimize the computation graph.
104103

105104
```python
106-
import autogen
107105
from opto.optimizers import OptoPrime
108106

109107

@@ -121,8 +119,7 @@ test_input = [1, 2, 3, 4]
121119

122120
epoch = 2
123121

124-
optimizer = OptoPrime(strange_sort_list.parameters(),
125-
config_list=autogen.config_list_from_json("OAI_CONFIG_LIST"))
122+
optimizer = OptoPrime(strange_sort_list.parameters())
126123

127124
for i in range(epoch):
128125
print(f"Training Epoch {i}")
@@ -215,16 +212,16 @@ def train():
215212
agent = train()
216213
```
217214

218-
Defining and training an agent through Trace will give you more flexibility and control over what the agent learns.
215+
Defining and training an agent through Trace will give you more flexibility and control over what the agent learns.
219216

220217
## Tutorials
221218

222219
| **Level** | **Tutorial** | **Run in Colab** | **Description** |
223-
| --- |-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
220+
| --- |-------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
224221
| Beginner | [Getting Started](https://microsoft.github.io/Trace/quickstart/quick_start.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/quickstart/quick_start.ipynb) | Introduces basic primitives like `node` and `bundle`. Showcases a code optimization pipeline. |
225-
| Beginner | [Adaptive AI Agent](https://microsoft.github.io/Trace/quickstart/quick_start_2.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/quickstart/quick_start_2.ipynb) | Introduce primitive `model` that allows anyone to build self-improving agents that react to environment feedback. Shows how an LLM agent learns to place a shot in a Battleship game.
226-
| Intermediate | [Multi-Agent Collaboration](https://microsoft.github.io/Trace/quickstart/virtualhome.html) | N/A | Demonstrates how Trace can be used for multi-agent collaboration environment in Virtualhome.
227-
| Intermediate | [NLP Prompt Optimization](https://microsoft.github.io/Trace/examples/nlp/bigbench_hard.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/examples/nlp/bigbench_hard.ipynb) | Shows how Trace can optimizes prompt and code together jointly for BigBench-Hard 23 tasks.
222+
| Beginner | [Adaptive AI Agent](https://microsoft.github.io/Trace/quickstart/quick_start_2.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/quickstart/quick_start_2.ipynb) | Introduce primitive `model` that allows anyone to build self-improving agents that react to environment feedback. Shows how an LLM agent learns to place a shot in a Battleship game.
223+
| Intermediate | [Multi-Agent Collaboration](https://microsoft.github.io/Trace/quickstart/virtualhome.html) | N/A | Demonstrates how Trace can be used for multi-agent collaboration environment in Virtualhome.
224+
| Intermediate | [NLP Prompt Optimization](https://microsoft.github.io/Trace/examples/nlp/bigbench_hard.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/examples/nlp/bigbench_hard.ipynb) | Shows how Trace can optimizes prompt and code together jointly for BigBench-Hard 23 tasks.
228225
| Advanced | [Robotic Arm Control](https://microsoft.github.io/Trace/examples/robotics/metaworld.html) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/microsoft/Trace/blob/website/docs/examples/robotics/metaworld.ipynb) | Trace can optimize code to control a robotic arm after observing a full trajectory of interactions. |
229226

230227

@@ -276,8 +273,9 @@ with TraceGraph coming soon).
276273

277274
## LLM API Setup
278275

279-
Currently we rely on AutoGen for LLM caching and API-Key management.
280-
AutoGen relies on `OAI_CONFIG_LIST`, which is a file you put in your working directory. It has the format of:
276+
Currently we rely on LiteLLM or AutoGen for LLM caching and API-Key management.
277+
By default, LiteLLM is used. Please the documentation there to set the right environment variables for keys and end-point urls.
278+
On the other hand, AutoGen relies on `OAI_CONFIG_LIST`, which is a file you put in your working directory. It has the format of:
281279

282280
```json lines
283281
[

docs/examples/game/battleship.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@
923923
}
924924
],
925925
"source": [
926-
"optimizer = OptoPrime(policy.parameters(), memory_size=0, config_list=config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
926+
"optimizer = OptoPrime(policy.parameters(), memory_size=0)\n",
927927
"\n",
928928
"\n",
929929
"feedback = \"\"\n",

docs/quickstart/quick_start.ipynb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,7 @@
381381
"\n",
382382
"epoch = 2\n",
383383
"\n",
384-
"optimizer = OptoPrime(strange_sort_list.parameters(),\n",
385-
" config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
384+
"optimizer = OptoPrime(strange_sort_list.parameters())\n",
386385
"\n",
387386
"for i in range(epoch):\n",
388387
" print(f\"Training Epoch {i}\")\n",
@@ -933,10 +932,8 @@
933932
"epoch = 2\n",
934933
"\n",
935934
"code_optimizer = OptoPrime(strange_sort_list.parameters(),\n",
936-
" config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"),\n",
937935
" ignore_extraction_error=True)\n",
938936
"verifier_optimizer = OptoPrime(verifier.parameters(),\n",
939-
" config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"),\n",
940937
" ignore_extraction_error=True)\n",
941938
"\n",
942939
"for i in range(epoch):\n",

docs/quickstart/quick_start_2.ipynb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,26 @@
127127
"metadata": {},
128128
"outputs": [],
129129
"source": [
130-
"import sys\n",
131130
"import os\n",
131+
"import urllib.request\n",
132+
"import importlib.util\n",
132133
"\n",
133-
"# Get the absolute path of the examples folder\n",
134-
"examples_path = os.path.abspath(os.path.join('..', '..', 'examples'))\n",
134+
"# Define the raw URL for downloading\n",
135+
"raw_url = \"https://raw.githubusercontent.com/microsoft/Trace/main/examples/battleship.py\"\n",
135136
"\n",
136-
"# Add the examples folder to the Python path\n",
137-
"sys.path.append(examples_path)"
137+
"# Define the local file path\n",
138+
"local_file = \"battleship.py\"\n",
139+
"\n",
140+
"# Download the file\n",
141+
"urllib.request.urlretrieve(raw_url, local_file)\n",
142+
"print(f\"Downloaded {local_file}\")\n",
143+
"\n",
144+
"# Load the module dynamically\n",
145+
"spec = importlib.util.spec_from_file_location(\"battleship\", local_file)\n",
146+
"battleship = importlib.util.module_from_spec(spec)\n",
147+
"spec.loader.exec_module(battleship)\n",
148+
"\n",
149+
"print(\"battleship module loaded successfully!\")"
138150
]
139151
},
140152
{
@@ -2189,7 +2201,7 @@
21892201
"\n",
21902202
"agent = Agent()\n",
21912203
"obs = node(board.get_shots(), trainable=False)\n",
2192-
"optimizer = OptoPrime(agent.parameters(), config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
2204+
"optimizer = OptoPrime(agent.parameters())\n",
21932205
"\n",
21942206
"feedback, terminal, cum_reward = \"\", False, 0\n",
21952207
"\n",

docs/tutorials/optimization_tutorial.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
"\n",
178178
"# One-step optimization example\n",
179179
"x = node(-1.0, trainable=True)\n",
180-
"optimizer = OptoPrime([x], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
180+
"optimizer = OptoPrime([x])\n",
181181
"output = foobar(x)\n",
182182
"feedback = user(output.data)\n",
183183
"optimizer.zero_feedback()\n",
@@ -415,7 +415,7 @@
415415
"# A small example of how to use the optimizer in a loop\n",
416416
"GRAPH.clear()\n",
417417
"x = node(-1.0, trainable=True)\n",
418-
"optimizer = OptoPrime([x], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
418+
"optimizer = OptoPrime([x])\n",
419419
"\n",
420420
"history = [x.data]\n",
421421
"feedback = \"\"\n",
@@ -466,7 +466,7 @@
466466
"# A small example of how to include constraints on parameters\n",
467467
"GRAPH.clear()\n",
468468
"x = node(-1.0, trainable=True, constraint=\"The value should be greater than 2.0\")\n",
469-
"optimizer = OptoPrime([x], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
469+
"optimizer = OptoPrime([x])\n",
470470
"\n",
471471
"history = [x.data]\n",
472472
"feedback = \"\"\n",
@@ -600,7 +600,7 @@
600600
"source": [
601601
"GRAPH.clear()\n",
602602
"x = node(\"negative point one\", trainable=True)\n",
603-
"optimizer = OptoPrime([x], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
603+
"optimizer = OptoPrime([x])\n",
604604
"\n",
605605
"history = [x.data]\n",
606606
"feedback = \"\"\n",
@@ -675,7 +675,7 @@
675675
"\n",
676676
"\n",
677677
"x = node(-1, trainable=False)\n",
678-
"optimizer = OptoPrime([my_fun.parameter], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
678+
"optimizer = OptoPrime([my_fun.parameter])\n",
679679
"\n",
680680
"feedback = \"\"\n",
681681
"while feedback != \"Success.\":\n",
@@ -917,7 +917,7 @@
917917
],
918918
"source": [
919919
"x = node(0.005, trainable=True)\n",
920-
"optimizer = OptoPrime([x], config_list=autogen.config_list_from_json(\"OAI_CONFIG_LIST\"))\n",
920+
"optimizer = OptoPrime([x])\n",
921921
"\n",
922922
"history = []\n",
923923
"bestScore = None\n",

opto/optimizers/optoprime.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Any, List, Dict, Union, Tuple
22
from dataclasses import dataclass, asdict
33
from textwrap import dedent, indent
4-
import autogen
54
import warnings
65
import json
76
import re
@@ -11,7 +10,7 @@
1110
from opto.trace.propagators.propagators import Propagator
1211
from opto.optimizers.optimizer import Optimizer
1312
from opto.optimizers.buffers import FIFOBuffer
14-
from opto.utils.llm import AutoGenLLM
13+
from opto.utils.llm import AbstractModel, LLM
1514

1615

1716
def get_fun_name(node: MessageNode):
@@ -250,7 +249,7 @@ class OptoPrime(Optimizer):
250249
def __init__(
251250
self,
252251
parameters: List[ParameterNode],
253-
llm: AutoGenLLM = None,
252+
llm: AbstractModel = None,
254253
*args,
255254
propagator: Propagator = None,
256255
objective: Union[None, str] = None,
@@ -260,12 +259,11 @@ def __init__(
260259
max_tokens=4096,
261260
log=True,
262261
prompt_symbols=None,
263-
filter_dict: Dict = None, # autogen filter_dict
264262
**kwargs,
265263
):
266264
super().__init__(parameters, *args, propagator=propagator, **kwargs)
267265
self.ignore_extraction_error = ignore_extraction_error
268-
self.llm = llm or AutoGenLLM()
266+
self.llm = llm or LLM()
269267
self.objective = objective or self.default_objective
270268
self.example_problem = ProblemInstance.problem_template.format(
271269
instruction=self.default_objective,

opto/optimizers/textgrad.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import json
22
from dataclasses import dataclass
3-
import autogen
43
from typing import Any, List, Dict, Union, Tuple, Optional
54
from opto.optimizers.optimizer import Optimizer
65
from opto.trace.nodes import ParameterNode, Node, MessageNode
76
from opto.trace.propagators import TraceGraph, GraphPropagator, Propagator
87
from opto.trace.utils import escape_json_nested_quotes, remove_non_ascii
9-
from opto.utils.llm import AutoGenLLM
8+
from opto.utils.llm import LLM, AbstractModel
109

1110
from copy import copy
1211
import re
@@ -309,7 +308,7 @@ class TextGrad(Optimizer):
309308
def __init__(
310309
self,
311310
parameters: List[ParameterNode],
312-
llm: AutoGenLLM = None,
311+
llm: AbstractModel = None,
313312
*args,
314313
propagator: Propagator = None,
315314
objective: Union[None, str] = None,
@@ -318,7 +317,7 @@ def __init__(
318317
**kwargs,
319318
):
320319
super().__init__(parameters, *args, **kwargs)
321-
self.llm = llm or AutoGenLLM()
320+
self.llm = llm or LLM()
322321
self.print_limit = 100
323322
self.max_tokens = max_tokens
324323
self.new_variable_tags = ["<IMPROVED_VARIABLE>", "</IMPROVED_VARIABLE>"]

opto/trace/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,7 @@ def __init__(
21812181
def create_feedback(self, style="simple"):
21822182
assert style in ("simple", "full")
21832183
feedback = self._data
2184-
if style in ("line", "full"):
2184+
if style == "full":
21852185
if type(self.info) is dict and self.info.get("error_comment") is not None:
21862186
feedback = self.info["error_comment"]
21872187
return feedback

opto/trace/operators.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,7 @@ def call_llm(system_prompt, *user_prompts, **kwargs):
594594
messages = [{"role": "system", "content": system_prompt}]
595595
for user_prompt in user_prompts:
596596
messages.append({"role": "user", "content": user_prompt})
597-
from opto.utils.llm import AutoGenLLM
598-
599-
llm = AutoGenLLM()
597+
from opto.utils.llm import LLM
598+
llm = LLM()
600599
response = llm(messages=messages, **kwargs)
601600
return response.choices[0].message.content

0 commit comments

Comments
 (0)