Spaces:
Sleeping
Sleeping
File size: 18,076 Bytes
116524e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | #!/usr/bin/env python3
# %% [markdown]
# # ACE Next β Interactive Demo
#
# This notebook walks through the refactored `ace` pipeline.
# It covers:
#
# 1. **Runners** β `ACE` (full pipeline) and `TraceAnalyser` (learning-only)
# 2. **Steps** β individual pipeline steps and `learning_tail()`
# 3. **Manual pipeline construction** β composing steps by hand
# 4. **Custom environments** β writing your own evaluator
# 5. **Checkpointing & deduplication** β production features
# 6. **Observability with Opik** β pipeline traces and LLM cost tracking
# 7. **Skillbook persistence** β save / reload
# 8. **TraceAnalyser** β learning from pre-recorded traces
#
# **Requirements:** `uv sync` from the repo root.
# Set your LLM API key before running:
# ```bash
# export OPENAI_API_KEY="sk-..."
# ```
# %% [markdown]
# ## 1. Setup & Imports
# %%
import os
import sys
import tempfile
from pathlib import Path
import nest_asyncio
nest_asyncio.apply()
# Ensure the project root is on sys.path so `ace`, `ace`, and `pipeline`
# are importable regardless of where the notebook kernel starts.
_here = Path(__file__).resolve().parent if "__file__" in dir() else Path.cwd()
_root = _here
for _p in [_here] + list(_here.parents):
if (_p / "pipeline" / "__init__.py").exists():
_root = _p
break
sys.path.insert(0, str(_root))
from dotenv import load_dotenv
load_dotenv(_root / ".env")
print(f"Project root: {_root}")
print("Setup OK")
# %% [markdown]
# ## 2. Core Imports
#
# Everything lives in `ace` β fully self-contained, zero cross-imports.
# %%
from ace import (
# Runners
ACE,
TraceAnalyser,
# Role implementations
Agent,
Reflector,
SkillManager,
# Core types
Sample,
Skillbook,
SimpleEnvironment,
TaskEnvironment,
EnvironmentResult,
)
from ace.core import AgentOutput, ACEStepContext, SkillbookView
print("All imports OK")
# %% [markdown]
# ## 3. Configure the LLM Client
#
# We use LiteLLM which supports 100+ providers. Swap the model string
# for any provider: `gpt-4o-mini`, `claude-sonnet-4-5-20250929`,
# `bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0`, etc.
# %%
MODEL = os.getenv("ACE_MODEL", "us.anthropic.claude-haiku-4-5-20251001-v1:0")
print(f"Model: {MODEL}")
# %% [markdown]
# ## 4. Build Roles
#
# The three ACE roles share the same LLM client. Each is independently
# customisable (prompt templates, retries, etc.).
# %%
agent = Agent(MODEL)
reflector = Reflector(MODEL)
skill_manager = SkillManager(MODEL)
print("Roles created: Agent, Reflector, SkillManager")
# %% [markdown]
# ## 5. Define Training Samples
# %%
samples = [
Sample(question="What is the capital of France?", ground_truth="Paris"),
Sample(question="What is the capital of Japan?", ground_truth="Tokyo"),
Sample(question="What is the capital of Brazil?", ground_truth="Brasilia"),
Sample(question="What is the capital of Australia?", ground_truth="Canberra"),
Sample(question="What is the capital of Nigeria?", ground_truth="Abuja"),
]
print(f"Prepared {len(samples)} training samples")
# %% [markdown]
# ---
# ## 6. ACE Runner β Full Adaptive Pipeline
#
# The `ACE` runner is the full closed-loop pipeline:
# ```
# Agent β Evaluate β Reflect β Tag β Update β Apply
# ```
#
# It takes `Sample` objects and an optional `TaskEnvironment`.
# %% [markdown]
# ### 6a. With SimpleEnvironment
#
# `SimpleEnvironment` checks if the ground truth appears in the agent's
# answer (case-insensitive substring match).
# %%
skillbook = Skillbook()
ace = ACE.from_roles(
agent=agent,
reflector=reflector,
skill_manager=skill_manager,
environment=SimpleEnvironment(),
skillbook=skillbook,
)
results = ace.run(samples[:3], epochs=1)
print(f"Processed {len(results)} samples\n")
for r in results:
if r.error:
print(f" ERROR at {r.failed_at}: {r.error}")
elif r.output:
ctx: ACEStepContext = r.output
answer = ctx.agent_output.final_answer if ctx.agent_output else "N/A"
print(f" Q: {r.sample.question}")
print(f" A: {answer}")
# %%
print(f"\nSkillbook after 1 epoch:")
print(f" Stats: {skillbook.stats()}")
for skill in skillbook.skills()[:5]:
print(f" - [{skill.id}] {skill.content}")
# %% [markdown]
# ### 6b. Custom Environment
#
# Create your own evaluator by subclassing `TaskEnvironment`.
# %%
class ExactMatchEnvironment(TaskEnvironment):
"""Strict evaluation: answer must exactly match ground truth."""
def evaluate(self, sample: Sample, agent_output: AgentOutput) -> EnvironmentResult:
expected = (sample.ground_truth or "").strip().lower()
predicted = agent_output.final_answer.strip().lower()
correct = expected in predicted
return EnvironmentResult(
feedback=(
"Correct!" if correct else f"Wrong. Expected: {sample.ground_truth}"
),
ground_truth=sample.ground_truth,
metrics={"accuracy": 1.0 if correct else 0.0},
)
print("ExactMatchEnvironment defined")
# %%
skillbook2 = Skillbook()
ace2 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=ExactMatchEnvironment(),
skillbook=skillbook2,
)
results2 = ace2.run(samples[:2], epochs=1)
for r in results2:
if r.output:
ctx = r.output
print(f" Q: {r.sample.question}")
print(f" A: {ctx.agent_output.final_answer if ctx.agent_output else 'N/A'}")
if ctx.reflections:
print(f" Insight: {ctx.reflections[0].key_insight}")
print()
# %% [markdown]
# ### 6c. Without Environment
#
# When no environment is provided, `EvaluateStep` is a no-op. The Reflector
# still learns from ground-truth comparison in the trace.
# %%
skillbook3 = Skillbook()
ace3 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
skillbook=skillbook3,
# No environment β EvaluateStep passes through
)
results3 = ace3.run(samples[:2], epochs=1)
print(f"Processed {len(results3)} samples (no environment)")
print(f"Skills learned: {skillbook3.stats()}")
# %% [markdown]
# ### 6d. Multi-Epoch Training
#
# Multiple epochs let the agent revisit samples with an evolving skillbook.
# Skills accumulate and refine across passes.
# %%
skillbook4 = Skillbook()
ace4 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=SimpleEnvironment(),
skillbook=skillbook4,
)
results4 = ace4.run(samples, epochs=2)
print(f"Total results across 2 epochs: {len(results4)}")
print(f"Skills learned: {skillbook4.stats()}")
# Print per-epoch accuracy
for epoch in range(1, 3):
epoch_results = [r for r in results4 if r.output and r.output.epoch == epoch]
correct = sum(
1
for r in epoch_results
if r.output
and r.output.agent_output
and (r.sample.ground_truth or "").lower()
in r.output.agent_output.final_answer.lower()
)
print(f" Epoch {epoch}: {correct}/{len(epoch_results)} correct")
# %% [markdown]
# ---
# ## 7. Manual Step-by-Step Pipeline
#
# Under the hood, runners compose `Pipeline` objects from individual steps.
# Here we build one by hand to see exactly what each step does.
# All pipeline classes and steps are importable directly from `ace`.
# %%
from ace import (
Pipeline,
AgentStep,
EvaluateStep,
learning_tail,
)
skillbook5 = Skillbook()
env = SimpleEnvironment()
# Build the full pipeline manually
pipe = Pipeline(
[
AgentStep(Agent(MODEL), skillbook5),
EvaluateStep(env),
*learning_tail(Reflector(MODEL), SkillManager(MODEL), skillbook5),
]
)
print(f"Pipeline steps: {len(pipe._steps)}")
print(f" requires: {pipe.requires}")
print(f" provides: {pipe.provides}")
# %% [markdown]
# ### Run a single sample through the manual pipeline
# %%
sample = samples[0]
# Build the context the same way ACE._build_context() does
ctx = ACEStepContext(
sample=sample,
skillbook=SkillbookView(skillbook5),
epoch=1,
total_epochs=1,
step_index=0,
total_steps=1,
global_sample_index=0,
)
print(f"Before pipeline:")
print(f" Skills: {skillbook5.stats()}")
print(f" agent_output: {ctx.agent_output}")
# Run the full pipeline on a single context
from pipeline.protocol import SampleResult
results_manual = pipe.run([ctx])
print(f"\nAfter pipeline:")
for r in results_manual:
if r.error:
print(f" ERROR: {r.error}")
elif r.output:
out: ACEStepContext = r.output
print(
f" Agent answer: {out.agent_output.final_answer if out.agent_output else 'N/A'}"
)
print(
f" Reflector insight: {out.reflections[0].key_insight if out.reflections else 'N/A'}"
)
print(f" Skills now: {skillbook5.stats()}")
# %% [markdown]
# ### Using `learning_tail()` as a building block
#
# `learning_tail()` returns the standard learning steps:
# `[ReflectStep, UpdateStep]` (the agentic SkillManager mutates the
# skillbook directly via its tools). Optional deduplication and
# checkpoint steps are appended.
# %%
skillbook6 = Skillbook()
tail = learning_tail(
Reflector(MODEL),
SkillManager(MODEL),
skillbook6,
)
print(f"learning_tail() returns {len(tail)} steps:")
for step in tail:
print(f" - {type(step).__name__}")
# %% [markdown]
# ---
# ## 8. Checkpointing
#
# Save the skillbook every N successful samples so you can resume after
# interruption or compare skillbook evolution over time.
# %%
skillbook7 = Skillbook()
with tempfile.TemporaryDirectory() as tmpdir:
ace7 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=SimpleEnvironment(),
skillbook=skillbook7,
checkpoint_dir=tmpdir,
checkpoint_interval=2, # save every 2 successful samples
)
results7 = ace7.run(samples, epochs=1)
saved = sorted(Path(tmpdir).glob("*.json"))
print("Checkpoint files:")
for f in saved:
print(f" {f.name} ({f.stat().st_size} bytes)")
# %% [markdown]
# ---
# ## 9. Deduplication
#
# Merge near-duplicate skills to keep the skillbook compact. The
# `DeduplicationManager` runs periodically during training.
# %%
from ace import DeduplicationManager, SimilarityDetector
from ace.protocols import DeduplicationConfig
skillbook8 = Skillbook()
dedup = DeduplicationManager(DeduplicationConfig(similarity_threshold=0.85))
ace8 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=SimpleEnvironment(),
skillbook=skillbook8,
dedup_manager=dedup,
dedup_interval=3, # run dedup every 3 samples
)
results8 = ace8.run(samples, epochs=1)
print(f"Skills after training with dedup: {skillbook8.stats()}")
# %% [markdown]
# ---
# ## 10. Skillbook Persistence β Save & Reload
#
# Save the learned skillbook to disk and reload it in a future session.
# %%
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "learned_skillbook.json"
# Save
skillbook.save_to_file(str(path))
print(f"Saved to {path.name} ({path.stat().st_size} bytes)")
# Reload
reloaded = Skillbook.load_from_file(str(path))
print(f"Reloaded: {reloaded.stats()}")
print(f"Stats match: {reloaded.stats() == skillbook.stats()}")
# %% [markdown]
# ---
# ## 11. TraceAnalyser β Learning from Pre-Recorded Traces
#
# `TraceAnalyser` runs the learning tail only β no Agent, no Evaluate.
# Feed it raw trace dicts (the same shape ReflectStep expects) and it
# builds a skillbook from historical data.
# %%
# Simulate some pre-recorded traces (e.g., from browser-use history logs)
traces = [
{
"question": "Book a flight from NYC to London",
"reasoning": "Step 1: Opened booking site. Step 2: Searched flights. Step 3: Selected cheapest option.",
"answer": "Booked flight AA100 for $450",
"skill_ids": [],
"feedback": "Task succeeded in 3 steps",
"ground_truth": None,
},
{
"question": "Find the cheapest hotel in Paris",
"reasoning": "Step 1: Opened hotel site. Step 2: Set filters. Step 3: Sorted by price. Step 4: Cookie popup blocked view.",
"answer": "Failed: could not dismiss cookie popup",
"skill_ids": [],
"feedback": "Task failed β cookie popup blocked interaction after step 3",
"ground_truth": None,
},
{
"question": "Check weather in Tokyo",
"reasoning": "Step 1: Navigated to weather.com. Step 2: Searched Tokyo. Step 3: Read forecast.",
"answer": "Tokyo: 22C, partly cloudy",
"skill_ids": [],
"feedback": "Task succeeded in 3 steps β fast and accurate",
"ground_truth": None,
},
]
skillbook9 = Skillbook()
analyser = TraceAnalyser.from_roles(
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
skillbook=skillbook9,
)
results9 = analyser.run(traces, epochs=1)
print(f"Analysed {len(results9)} traces")
print(f"Skills learned: {skillbook9.stats()}")
for skill in skillbook9.skills()[:5]:
print(f" - [{skill.section}] {skill.content}")
# %% [markdown]
# ### Multi-epoch trace analysis
#
# Each epoch re-processes all traces with the evolving skillbook.
# Early epochs extract obvious patterns; later epochs refine.
# %%
skillbook10 = Skillbook()
analyser2 = TraceAnalyser.from_roles(
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
skillbook=skillbook10,
)
results10 = analyser2.run(traces, epochs=2)
print(f"Total results across 2 epochs: {len(results10)}")
print(f"Skills after 2 epochs: {skillbook10.stats()}")
# %% [markdown]
# ---
# ## 12. Mixed Workflow β TraceAnalyser then ACE
#
# A common pattern: build an initial skillbook from historical traces,
# then deploy with live learning.
# %%
# Phase 1: Build skillbook from historical data
shared_skillbook = Skillbook()
analyser_phase1 = TraceAnalyser.from_roles(
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
skillbook=shared_skillbook,
)
analyser_phase1.run(traces, epochs=1)
print(f"Phase 1 β TraceAnalyser:")
print(f" Skills from traces: {shared_skillbook.stats()}")
# Phase 2: Deploy with live ACE learning (reuse the evolved skillbook)
ace_phase2 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=SimpleEnvironment(),
skillbook=shared_skillbook,
)
results_phase2 = ace_phase2.run(samples[:3], epochs=1)
print(f"\nPhase 2 β ACE live learning:")
print(f" Processed {len(results_phase2)} samples")
print(f" Skills after live learning: {shared_skillbook.stats()}")
# %% [markdown]
# ---
# ## 13. Error Handling
#
# Failed samples are captured in `SampleResult.error` β the pipeline
# never drops a sample silently. Other samples continue processing.
# %%
bad_samples = [
samples[0],
Sample(question="", ground_truth=""), # edge case: empty question
samples[1],
]
skillbook11 = Skillbook()
ace11 = ACE.from_roles(
agent=Agent(MODEL),
reflector=Reflector(MODEL),
skill_manager=SkillManager(MODEL),
environment=SimpleEnvironment(),
skillbook=skillbook11,
)
results11 = ace11.run(bad_samples, epochs=1)
for i, r in enumerate(results11, 1):
status = "OK" if r.error is None else f"FAIL ({r.failed_at})"
if r.output and r.output.agent_output:
answer = r.output.agent_output.final_answer
else:
answer = "N/A"
print(f" [{i}] {status:20s} answer={answer}")
# %% [markdown]
# ---
# ## 14. Inspecting the SkillbookView
#
# Steps receive a read-only `SkillbookView` on the context.
# This prevents accidental mutations from within pipeline steps.
# %%
sb = Skillbook()
view = SkillbookView(sb)
print(f"SkillbookView: {view}")
print(f" len: {len(view)}")
print(f" stats: {view.stats()}")
print(f" prompt: {view.as_prompt()[:200]}...")
# Iterate over skills in the view
for skill in view:
print(f" - {skill.id}: {skill.content}")
# %% [markdown]
# ---
# ## Summary
#
# | What | How |
# |------|-----|
# | Full pipeline | `ACE.from_roles(agent=..., reflector=..., skill_manager=...)` |
# | With environment | `ACE.from_roles(..., environment=SimpleEnvironment())` |
# | Without environment | `ACE.from_roles(...)` β EvaluateStep is a no-op |
# | Multi-epoch | `ace.run(samples, epochs=3)` |
# | Checkpointing | `ACE.from_roles(..., checkpoint_dir="./ckpts", checkpoint_interval=10)` |
# | Deduplication | `ACE.from_roles(..., dedup_manager=dedup, dedup_interval=5)` |
# | Trace analysis | `TraceAnalyser.from_roles(reflector=..., skill_manager=...)` |
# | Save skillbook | `ace.save("path.json")` or `skillbook.save_to_file("path.json")` |
# | Load skillbook | `Skillbook.load_from_file("path.json")` |
# | Manual steps | `Pipeline([AgentStep(a), EvaluateStep(e), *learning_tail(r, sm, sb)])` |
# | Learning tail | `learning_tail(reflector, skill_manager, skillbook)` |
#
# **Pipeline:**
# ```
# ACE: Agent β Evaluate β Reflect β Tag β Update β Apply β [Dedup] β [Checkpoint] β [Opik]
# TraceAnalyser: Reflect β Tag β Update β Apply β [Dedup] β [Checkpoint] β [Opik]
# ```
|