Skip to content

Commit 2898a18

Browse files
authored
[Integration] Add Cleanlab + Strands Agent integration (#107)
1 parent 1b9a571 commit 2898a18

File tree

6 files changed

+848
-0
lines changed

6 files changed

+848
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ parallel = true
8686
omit = [
8787
"src/cleanlab_codex/__about__.py",
8888
"*/tests/*",
89+
"src/cleanlab_codex/experimental/*",
8990
]
9091

9192
[tool.coverage.paths]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Beta functionality which may not remain backwards compatible."""
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Cleanlab Strands Integration
2+
3+
This module provides integration between Cleanlab validation and the Strands framework, allowing you to wrap any Strands model with Cleanlab's response validation capabilities and pass that model to a Strands Agent.
4+
5+
## Overview
6+
7+
The `CleanlabModel` class wraps existing Strands models to add real-time response validation, guardrails, and expert escalation through Cleanlab's platform. It maintains compatibility with the Strands framework while adding validation capabilities.
8+
9+
## Installation
10+
11+
### 1. Install cleanlab-codex
12+
13+
First, install the cleanlab-codex package:
14+
15+
```bash
16+
pip install cleanlab-codex
17+
```
18+
19+
### 2. Install Strands
20+
21+
Install the Strands framework:
22+
23+
```bash
24+
pip install strands-agents
25+
```
26+
27+
### 3. Install Additional Dependencies
28+
29+
Then install any additional dependencies for the experimental integration:
30+
31+
```bash
32+
pip install -r requirements.txt
33+
```
34+
35+
Alternatively, you can install all dependencies at once:
36+
37+
```bash
38+
pip install cleanlab-codex strands-agents "strands-agents[openai]"
39+
```
40+
41+
**Note**: Users will need to install `cleanlab-codex` separately to create the `Project` instance that gets passed to `CleanlabModel`, but the strands integration itself only requires the packages listed above.
42+
43+
## Basic Usage
44+
45+
```python
46+
import uuid
47+
48+
from strands.agent.agent import Agent
49+
from strands.models.openai import OpenAIModel
50+
from strands.session.file_session_manager import FileSessionManager
51+
52+
from cleanlab_codex.experimental.strands import CleanlabModel
53+
from cleanlab_codex import Project
54+
55+
# Initialize your Cleanlab project
56+
project = Project.from_access_key("your_access_key_here")
57+
58+
SYSTEM_PROMPT = "You are a customer service agent. Be polite and concise in your responses."
59+
FALLBACK_RESPONSE = "Sorry I am unsure. You can try rephrasing your request."
60+
61+
# Create base model
62+
base_model = OpenAIModel(
63+
model_id="gpt-4o-mini",
64+
)
65+
66+
### New code to add for Cleanlab API ###
67+
cleanlab_model = CleanlabModel( # Wrap with Cleanlab validation
68+
underlying_model=base_model,
69+
cleanlab_project=project,
70+
fallback_response=FALLBACK_RESPONSE,
71+
# context_retrieval_tools=["web_search", "get_payment_schedule", "get_total_amount_owed"] # Specify tool(s) that provide context here
72+
)
73+
### End of new code to add for Cleanlab API ###
74+
75+
# Create agent with validated model for normal conversation
76+
agent = Agent(
77+
model=cleanlab_model,
78+
system_prompt=SYSTEM_PROMPT,
79+
# tools=[get_payment_schedule, get_total_amount_owed, web_search], # Add tools in Strands format here
80+
session_manager=FileSessionManager(session_id=uuid.uuid4().hex), # Persist chat history
81+
)
82+
83+
### New code to add for Cleanlab API ###
84+
cleanlab_model.set_agent_reference(agent)
85+
### End of new code to add for Cleanlab API ###
86+
87+
# Use the agent normally - validation happens automatically
88+
response = agent("What's my payment schedule?")
89+
```
90+
91+
## Configuration Options
92+
93+
### CleanlabModel Parameters
94+
95+
- `underlying_model`: The base Strands model to wrap
96+
- `cleanlab_project`: Your Cleanlab project instance
97+
- `fallback_response`: Response to return when guardrails are triggered
98+
- `context_retrieval_tools`: List of tool names that provide context for validation
99+
- `skip_validating_tool_calls`: Skip validation when response contains tool calls (default: True)
100+
101+
## Important Notes
102+
103+
1. **Agent Reference**: Always call `cleanlab_model.set_agent_reference(agent)` after creating your agent for full functionality
104+
2. **Tool Validation**: By default, responses containing tool calls skip validation. Set `skip_validating_tool_calls=False` to validate all responses, including Agent responses with tool calls.
105+
3. **Session Management**: The Codex UI uses session IDs for tracking chat history for validation results when available.
106+
107+
## Dependencies
108+
109+
### Runtime Dependencies
110+
- `cleanlab-tlm>=1.1.14`
111+
- `strands-agents`
112+
113+
### Type Checking Only (Optional)
114+
- `cleanlab-codex` (for Project type hints)
115+
- `codex-sdk` (for ProjectValidateResponse type hints)
116+
- `openai` (for OpenAI type hints)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Methods to integrate with AI Agents built using the AWS Strands framework."""
2+
3+
from cleanlab_codex.experimental.strands.cleanlab_model import CleanlabModel
4+
5+
__all__ = ["CleanlabModel"]

0 commit comments

Comments
 (0)