Spaces:
Sleeping
Sleeping
File size: 3,032 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 | # LangChain Integration
The `LangChain` runner wraps any LangChain Runnable (chains, `AgentExecutor`, LangGraph graphs) with ACE learning. The runner extracts execution traces and learns strategies from them.
## Installation
```bash
uv add ace-framework[langchain]
```
## Quick Start
```python
from ace import LangChain
runner = LangChain.from_model(your_chain, ace_model="gpt-4o-mini")
results = runner.run([
{"input": "Summarize this document"},
{"input": "Extract key entities"},
])
runner.save("chain_expert.json")
```
## Parameters
### from_model()
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `runnable` | `Any` | β | LangChain Runnable (chain, AgentExecutor, graph) |
| `ace_model` | `str` | `"gpt-4o-mini"` | Model for Reflector + SkillManager |
| `ace_max_tokens` | `int` | `2048` | Max tokens for ACE LLM responses |
| `ace_temperature` | `float` | `0.0` | Sampling temperature for ACE roles |
### from_roles()
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `runnable` | `Any` | β | LangChain Runnable |
| `reflector` | `ReflectorLike` | β | Reflector instance |
| `skill_manager` | `SkillManagerLike` | β | SkillManager instance |
| `skillbook_path` | `str` | `None` | Load saved skillbook |
| `output_parser` | `Callable` | `None` | Custom output extraction |
| `dedup_config` | `DeduplicationConfig` | `None` | Deduplication config |
| `checkpoint_dir` | `str` | `None` | Checkpoint directory |
## Methods
```python
results = runner.run(inputs, epochs=1) # Run with learning
results = runner.invoke(single_input) # Single input convenience
runner.save("path.json") # Save skillbook
runner.wait_for_background() # Wait for async learning
```
## How It Works
1. **INJECT** β Skillbook strategies are added to the chain input
2. **EXECUTE** β LangChain runs the chain normally
3. **Extract trace** β ACE extracts intermediate steps, tool calls, and reasoning
4. **LEARN** β Reflector analyzes the trace, SkillManager updates the skillbook
The runner handles simple chains, `AgentExecutor` (with `intermediate_steps`), and LangGraph graphs automatically.
## Input Types
The runner accepts any input your chain expects:
```python
# String input
runner.run(["What is ACE?"])
# Dict input
runner.run([{"input": "query", "context": "..."}])
# Message list
runner.run([[HumanMessage(content="Hello")]])
```
## Resuming from a Saved Skillbook
```python
runner = LangChain.from_model(
your_chain,
ace_model="gpt-4o-mini",
skillbook_path="chain_expert.json",
)
```
## What to Read Next
- [Integration Pattern](../guides/integration.md) β how the INJECT/EXECUTE/LEARN pattern works
- [Insight Levels](../concepts/insight-levels.md) β meso-level learning from traces
- [Opik Observability](opik.md) β monitor chain execution costs
|