Skip to content

Commit fd154b0

Browse files
committed
fix agentops and add docs
1 parent c5bd863 commit fd154b0

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

agentlightning/instrumentation/agentops.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
_original_handle_response: Callable[..., Any] | None = None
2929

3030

31+
def _unwrap_legacy_response(response: Any) -> Any:
32+
if hasattr(response, "parse") and callable(response.parse):
33+
return response.parse()
34+
return response
35+
36+
3137
def _patch_new_agentops():
3238
import agentops.instrumentation.providers.openai.stream_wrapper
3339
import agentops.instrumentation.providers.openai.wrappers.chat
@@ -44,6 +50,11 @@ def _patch_new_agentops():
4450
@no_type_check
4551
def _handle_chat_attributes_with_tokens(args=None, kwargs=None, return_value=None, **kws): # type: ignore
4652
attributes = _original_handle_chat_attributes(args=args, kwargs=kwargs, return_value=return_value, **kws)
53+
54+
# In some cases, response is a openai._legacy_response.LegacyAPIResponse (e.g., LiteLLM, or LangChain),
55+
# This is created by client.with_raw_response.create()
56+
return_value = _unwrap_legacy_response(return_value)
57+
4758
if (
4859
return_value is not None
4960
and hasattr(return_value, "prompt_token_ids")
@@ -89,20 +100,6 @@ def _handle_chat_attributes_with_tokens(args=None, kwargs=None, return_value=Non
89100
[logprob.model_dump() for logprob in first_choice.logprobs.refusal]
90101
)
91102

92-
# For LiteLLM, response is a openai._legacy_response.LegacyAPIResponse
93-
if (
94-
return_value is not None
95-
and hasattr(return_value, "http_response")
96-
and return_value.http_response is not None
97-
and hasattr(return_value.http_response, "json")
98-
):
99-
json_data = return_value.http_response.json()
100-
if isinstance(json_data, dict):
101-
if json_data.get("prompt_token_ids") is not None:
102-
attributes["prompt_token_ids"] = list(json_data["prompt_token_ids"])
103-
if json_data.get("response_token_ids") is not None:
104-
attributes["response_token_ids"] = list(json_data["response_token_ids"][0])
105-
106103
return attributes
107104

108105
agentops.instrumentation.providers.openai.wrappers.chat.handle_chat_attributes = _handle_chat_attributes_with_tokens

docs/how-to/train-sql-agent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ dev_data = pd.read_parquet("data/test_dev_500.parquet").to_dict("records")[:10]
264264
trainer.dev(agent, dev_dataset=dev_data)
265265
```
266266

267-
Run this in a Python session or adapt your script to include a `--dev` flag. Once the spans appear healthy and the rewards are non-zero, switch back to [`trainer.fit(...)`][agentlightning.Trainer.fit] for full RL training.
267+
Run this in a Python session or adapt your script to include a `--dev` flag. Once the spans appear healthy and the rewards are non-zero, switch back to [`trainer.fit(...)`][agentlightning.Trainer.fit] for full RL training. See the [debugging tutorial](../tutorials/debug.md) for more tips on how to debug the agent.
268268

269269
## Running the Sample Code
270270

docs/tutorials/debug.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,48 @@ Swap in an [`AgentOpsTracer`][agentlightning.AgentOpsTracer] instead of [`OtelTr
6666

6767
You can also call [`Runner.step`][agentlightning.Runner.step] to inject ad-hoc rollouts into a running store being used by another algorithm, so that the rollouts can be consumed by the algorithms. This is very recently known as the paradigm of ["online RL"](https://cursor.com/blog/tab-rl). At the moment, no algorithm in the [algorithm zoo](../algorithm-zoo/index.md) consumes externally generated rollouts, but the data flow is available there if you need it.
6868

69+
## Debug with LLM Proxy
70+
71+
If you are dealing with LLM optimization like Reinforcement Learning, we generally recommend using an online stable LLM service for your debugging purposes, like `openai/gpt-4.1-nano`. After the debugging is done, you can switch to a local training endpoint.
72+
73+
However, if you want to use a local LLM features like [getting the token IDs](../deep-dive/serving-llm.md), you can also manually start a local vLLM server by:
74+
75+
```bash
76+
vllm serve Qwen/Qwen2.5-0.5B-Instruct --port 8080
77+
```
78+
79+
Then start the LLM proxy via the following script:
80+
81+
```python
82+
import agentlightning as agl
83+
84+
llm_proxy = agl.LLMProxy(
85+
port=8081,
86+
model_list=[
87+
{
88+
"model_name": "Qwen/Qwen2.5-0.5B-Instruct",
89+
"litellm_params": {
90+
"model": "hosted_vllm/Qwen/Qwen2.5-0.5B-Instruct",
91+
"api_base": "http://localhost:8080/v1",
92+
},
93+
}
94+
],
95+
store=agl.InMemoryLightningStore(),
96+
)
97+
98+
llm_proxy.start()
99+
time.sleep(1000000)
100+
```
101+
102+
You can now use the LLM proxy by specifying environment variables:
103+
104+
```bash
105+
export OPENAI_API_BASE=http://localhost:8081/v1
106+
export OPENAI_API_KEY=dummy
107+
```
108+
109+
You might see warnings about `Missing or invalid rollout_id, attempt_id, or sequence_id` in the LLM proxy logs. This is fine because you don't have a rollout and attempt yet when you are debugging. When you started the training, the algorithm will create the rollouts for you and the warnings will go away.
110+
69111
## Hook into Runner's Lifecycle
70112

71113
[`Runner.run_context`][agentlightning.Runner.run_context] accepts a `hooks` argument so you can observe or augment lifecycle events without editing your agent. Hooks subclass [`Hook`][agentlightning.Hook] and can respond to four asynchronous callbacks: [`on_trace_start`][agentlightning.Hook.on_trace_start], [`on_rollout_start`][agentlightning.Hook.on_rollout_start], [`on_rollout_end`][agentlightning.Hook.on_rollout_end], and [`on_trace_end`][agentlightning.Hook.on_trace_end]. This is useful for:

examples/calc_x/train_calc_agent.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@
3131
import argparse
3232
import os
3333
from datetime import datetime
34-
from importlib.metadata import version
3534
from typing import Any, Dict, Optional, cast
3635

3736
from calc_agent import MathProblem, calc_agent
3837
from datasets import Dataset as HuggingFaceDataset
39-
from packaging import version as packaging_version
4038

4139
import agentlightning as agl
4240

0 commit comments

Comments
 (0)