-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanthropic_patterns_demo.py
More file actions
476 lines (363 loc) · 14.1 KB
/
Copy pathanthropic_patterns_demo.py
File metadata and controls
476 lines (363 loc) · 14.1 KB
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
"""
Anthropic Agent Patterns - Practical Examples
Demonstrates how to implement Anthropic's three core agent patterns
in the Empathy Framework:
1. Workflows (Sequential)
2. Orchestrator (Dynamic Routing)
3. Evaluator (Self-Correction)
Usage:
python examples/anthropic_patterns_demo.py
Requirements:
pip install empathy-framework[developer]
"""
import asyncio
from typing import Any
from empathy_os.workflows import BaseWorkflow
from empathy_os.models import LLMClient
# ============================================================================
# Pattern 1: Sequential Workflow (Anthropic Pattern)
# ============================================================================
class CodeAnalysisPipeline(BaseWorkflow):
"""Sequential code analysis workflow.
Follows Anthropic's workflow pattern: predefined sequence of agents
with clear input/output contracts.
Stages:
1. Parse (cheap tier) - Extract structure
2. Analyze (capable tier) - Deep analysis
3. Recommend (premium tier) - Strategic recommendations
"""
def __init__(self):
super().__init__(workflow_id="code-analysis-pipeline")
async def execute(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Execute three-stage analysis pipeline.
Args:
inputs: {"code": str} - Source code to analyze
Returns:
{
"parsed": dict, # Structure extraction
"analysis": dict, # Deep analysis
"recommendations": dict, # Improvements
"stages_completed": int
}
"""
code = inputs["code"]
# Stage 1: Parse code structure (cheap tier)
parsed = await self._parse_code(code)
print("✓ Stage 1: Parsed code structure")
# Stage 2: Analyze patterns (capable tier)
analysis = await self._analyze_patterns(parsed, code)
print("✓ Stage 2: Analyzed patterns")
# Stage 3: Generate recommendations (premium tier)
recommendations = await self._generate_recommendations(analysis, code)
print("✓ Stage 3: Generated recommendations")
return {
"parsed": parsed,
"analysis": analysis,
"recommendations": recommendations,
"stages_completed": 3,
}
async def _parse_code(self, code: str) -> dict[str, Any]:
"""Stage 1: Parse code structure (cheap tier)."""
prompt = f"""Extract the structure of this code:
{code}
Return JSON with: functions, classes, imports, complexity"""
response = await self.llm_client.call(
prompt=prompt,
tier="cheap", # Fast, inexpensive parsing
workflow_id=f"{self.workflow_id}:parse",
)
return {"structure": response["content"]}
async def _analyze_patterns(self, parsed: dict, code: str) -> dict[str, Any]:
"""Stage 2: Analyze code patterns (capable tier)."""
prompt = f"""Analyze this code for patterns and issues:
Structure: {parsed['structure']}
Code:
{code}
Identify: anti-patterns, bugs, security issues"""
response = await self.llm_client.call(
prompt=prompt,
tier="capable", # More thorough analysis
workflow_id=f"{self.workflow_id}:analyze",
)
return {"findings": response["content"]}
async def _generate_recommendations(
self, analysis: dict, code: str
) -> dict[str, Any]:
"""Stage 3: Generate strategic recommendations (premium tier)."""
prompt = f"""Given this analysis, provide strategic recommendations:
Analysis: {analysis['findings']}
Original Code:
{code}
Recommend: architectural improvements, refactoring, best practices"""
response = await self.llm_client.call(
prompt=prompt,
tier="premium", # High-quality strategic thinking
workflow_id=f"{self.workflow_id}:recommend",
)
return {"recommendations": response["content"]}
# ============================================================================
# Pattern 2: Orchestrator (Dynamic Routing)
# ============================================================================
class SimpleOrchestrator:
"""Routes tasks to specialist workflows.
Follows Anthropic's orchestrator pattern: single coordinator
that delegates to domain specialists.
"""
def __init__(self):
# Define specialists upfront (Anthropic pattern)
self.specialists = {
"security": self._create_security_specialist(),
"performance": self._create_performance_specialist(),
"quality": self._create_quality_specialist(),
}
def _create_security_specialist(self):
"""Security analysis specialist."""
async def specialist(code: str) -> dict:
client = LLMClient()
response = await client.call(
prompt=f"Find security vulnerabilities:\n{code}",
tier="capable",
workflow_id="security-specialist",
)
return {"type": "security", "findings": response["content"]}
return specialist
def _create_performance_specialist(self):
"""Performance analysis specialist."""
async def specialist(code: str) -> dict:
client = LLMClient()
response = await client.call(
prompt=f"Find performance issues:\n{code}",
tier="capable",
workflow_id="performance-specialist",
)
return {"type": "performance", "findings": response["content"]}
return specialist
def _create_quality_specialist(self):
"""Code quality specialist."""
async def specialist(code: str) -> dict:
client = LLMClient()
response = await client.call(
prompt=f"Review code quality:\n{code}",
tier="capable",
workflow_id="quality-specialist",
)
return {"type": "quality", "findings": response["content"]}
return specialist
async def route(self, task: str, code: str) -> dict:
"""Route task to appropriate specialist.
Args:
task: Natural language task description
code: Code to analyze
Returns:
Specialist analysis result
"""
task_lower = task.lower()
# Keyword-based routing (Anthropic pattern)
if any(word in task_lower for word in ["security", "vuln", "hack"]):
print("→ Routing to security specialist")
return await self.specialists["security"](code)
elif any(word in task_lower for word in ["slow", "perf", "bottleneck"]):
print("→ Routing to performance specialist")
return await self.specialists["performance"](code)
elif any(word in task_lower for word in ["quality", "review", "improve"]):
print("→ Routing to quality specialist")
return await self.specialists["quality"](code)
else:
# Default to quality review
print("→ Routing to quality specialist (default)")
return await self.specialists["quality"](code)
# ============================================================================
# Pattern 3: Evaluator (Self-Correction)
# ============================================================================
class SelfCorrectingCodeGenerator(BaseWorkflow):
"""Code generator with self-evaluation loop.
Follows Anthropic's evaluator pattern: worker agent generates,
evaluator agent assesses quality, loop until good enough.
"""
def __init__(self):
super().__init__(workflow_id="self-correcting-generator")
async def execute(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Generate code with self-correction.
Args:
inputs: {
"task": str, # What to generate
"requirements": str, # Requirements
}
Returns:
{
"code": str, # Generated code
"quality_score": int, # 0-100
"attempts": int, # Number of iterations
"status": str, # success or max_attempts
}
"""
task = inputs["task"]
requirements = inputs.get("requirements", "")
max_attempts = 3
best_code = None
best_score = 0
for attempt in range(max_attempts):
print(f"\n--- Attempt {attempt + 1} ---")
# Worker: Generate code
code = await self._generate_code(
task=task,
requirements=requirements,
previous_code=best_code,
attempt=attempt,
)
# Evaluator: Assess quality
evaluation = await self._evaluate_code(code, requirements)
score = evaluation["score"]
print(f"Quality score: {score}/100")
# Check if good enough
if score >= 85:
print("✓ Quality threshold met!")
return {
"code": code,
"quality_score": score,
"attempts": attempt + 1,
"status": "success",
}
# Track best attempt
if score > best_score:
best_score = score
best_code = code
# Return best after max attempts
print("⚠ Max attempts reached, returning best")
return {
"code": best_code,
"quality_score": best_score,
"attempts": max_attempts,
"status": "max_attempts_reached",
}
async def _generate_code(
self,
task: str,
requirements: str,
previous_code: str | None,
attempt: int,
) -> str:
"""Worker agent: Generate code."""
if attempt == 0:
prompt = f"Generate code for: {task}\n\nRequirements: {requirements}"
else:
prompt = f"""Improve this code:
{previous_code}
Task: {task}
Requirements: {requirements}
Make it better."""
response = await self.llm_client.call(
prompt=prompt,
tier="capable",
workflow_id=f"{self.workflow_id}:generate",
)
return response["content"]
async def _evaluate_code(self, code: str, requirements: str) -> dict:
"""Evaluator agent: Assess code quality."""
prompt = f"""Evaluate this code on a scale of 0-100:
Code:
{code}
Requirements:
{requirements}
Criteria: correctness, readability, efficiency, best practices
Return JSON: {{"score": <number>, "feedback": "<string>"}}"""
response = await self.llm_client.call(
prompt=prompt,
tier="cheap", # Evaluation can be cheaper
workflow_id=f"{self.workflow_id}:evaluate",
)
# Parse score (simple extraction)
content = response["content"]
try:
import json
data = json.loads(content)
return {"score": data["score"], "feedback": data["feedback"]}
except Exception:
# Fallback parsing
score = 50 # Default
if "score" in content:
import re
match = re.search(r'"score":\s*(\d+)', content)
if match:
score = int(match.group(1))
return {"score": score, "feedback": content}
# ============================================================================
# Demo Runner
# ============================================================================
async def demo_pattern_1():
"""Demo: Sequential workflow."""
print("\n" + "=" * 60)
print("PATTERN 1: SEQUENTIAL WORKFLOW")
print("=" * 60)
sample_code = """
def calculate_total(prices):
total = 0
for price in prices:
total = total + price
return total
"""
pipeline = CodeAnalysisPipeline()
result = await pipeline.execute({"code": sample_code})
print("\n--- Results ---")
print(f"Stages completed: {result['stages_completed']}")
print(f"Parsed: {result['parsed']}")
print(f"Analysis: {result['analysis']}")
print(f"Recommendations: {result['recommendations']}")
async def demo_pattern_2():
"""Demo: Orchestrator (dynamic routing)."""
print("\n" + "=" * 60)
print("PATTERN 2: ORCHESTRATOR (DYNAMIC ROUTING)")
print("=" * 60)
sample_code = """
def login(username, password):
query = "SELECT * FROM users WHERE username='" + username + "'"
return execute_query(query)
"""
orchestrator = SimpleOrchestrator()
# Route different tasks
tasks = [
"Check for security vulnerabilities",
"Find performance bottlenecks",
"Review code quality",
]
for task in tasks:
print(f"\nTask: {task}")
result = await orchestrator.route(task, sample_code)
print(f"Result: {result['type']} specialist found issues")
async def demo_pattern_3():
"""Demo: Self-correcting agent."""
print("\n" + "=" * 60)
print("PATTERN 3: SELF-CORRECTING AGENT")
print("=" * 60)
generator = SelfCorrectingCodeGenerator()
result = await generator.execute(
{
"task": "Write a function to validate email addresses",
"requirements": "Must handle edge cases and use regex",
}
)
print("\n--- Final Result ---")
print(f"Status: {result['status']}")
print(f"Quality: {result['quality_score']}/100")
print(f"Attempts: {result['attempts']}")
print(f"Code:\n{result['code']}")
async def main():
"""Run all pattern demonstrations."""
print("\n🚀 ANTHROPIC AGENT PATTERNS DEMO")
print("Demonstrating the three core patterns in Empathy Framework\n")
try:
# Pattern 1: Sequential Workflow
await demo_pattern_1()
# Pattern 2: Orchestrator
await demo_pattern_2()
# Pattern 3: Self-Correcting
await demo_pattern_3()
print("\n" + "=" * 60)
print("✅ ALL PATTERNS DEMONSTRATED SUCCESSFULLY")
print("=" * 60)
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())