Files
microsoft--agent-lightning/examples/unsloth/sft_rollout_runners.py
T
wehub-resource-sync 85742ab165
Deploy Documentation / deploy (push) Has been cancelled
CPU Test / Test (Utilities, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (LLM proxy, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (Others, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (Store, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (Utilities, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (Weave, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (AgentOps, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (LLM proxy, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (Others, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (Weave, latest, Python 3.13) (push) Has been cancelled
Dashboard / Chromatic (push) Has been cancelled
CPU Test / Lint - fast (push) Has been cancelled
CPU Test / Lint - next (push) Has been cancelled
CPU Test / Lint - slow (push) Has been cancelled
CPU Test / Lint - JavaScript (push) Has been cancelled
CPU Test / Build documentation (push) Has been cancelled
CPU Test / Test (AgentOps, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (LLM proxy, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (Others, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (Store, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (Weave, legacy, Python 3.10) (push) Has been cancelled
CPU Test / Test (AgentOps, stable, Python 3.11) (push) Has been cancelled
CPU Test / Test (Store, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (Utilities, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (Weave, stable, Python 3.12) (push) Has been cancelled
CPU Test / Test (AgentOps, latest, Python 3.13) (push) Has been cancelled
CPU Test / Test (LLM proxy, latest, Python 3.13) (push) Has been cancelled
CPU Test / Test (Others, latest, Python 3.13) (push) Has been cancelled
CPU Test / Test (Store, latest, Python 3.13) (push) Has been cancelled
CPU Test / Test (Utilities, latest, Python 3.13) (push) Has been cancelled
CPU Test / Test (JavaScript) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:44:17 +08:00

73 lines
1.9 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
"""This sample spawns a set of rollout runners to collect data for SFT.
It communicates with the SFT algorithm via a store server.
Run the store server beforehand:
```bash
agl store --port 4747
```
"""
import asyncio
import multiprocessing
from math_agent import GsmProblem, math_agent
from rich.console import Console
from agentlightning import setup_logging
from agentlightning.runner import LitAgentRunner
from agentlightning.store import LightningStore, LightningStoreClient
from agentlightning.tracer import OtelTracer
console = Console()
def run_rollout(*, store: LightningStore, worker_id: int) -> None:
"""A rollout runner.
Args:
store: The LightningStore instance.
"""
# Since the server side has already used LiteLLM proxy to collect traces,
# a simple OtelTracer to collect the rewards is enough.
tracer = OtelTracer()
runner = LitAgentRunner[GsmProblem](tracer=tracer)
console.print(f"[bold green]Runners: [/bold green] Rollout runner {worker_id} started.")
with runner.run_context(agent=math_agent, store=store, worker_id=worker_id):
asyncio.run(runner.iter())
def spawn_runners(*, store: LightningStore, n_runners: int) -> None:
"""Spawn a set of rollout runners.
It's just replicating the `run_rollout` function in multiple processes.
You can also replace this function with a bash script.
Args:
store: The LightningStore instance.
n_runners: The number of runners to spawn.
"""
runners = [
multiprocessing.Process(target=run_rollout, kwargs={"store": store, "worker_id": worker_id})
for worker_id in range(n_runners)
]
for runner in runners:
runner.start()
for runner in runners:
runner.join()
if __name__ == "__main__":
setup_logging()
store = LightningStoreClient("http://localhost:4747")
spawn_runners(store=store, n_runners=4)