-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_usage.py
More file actions
executable file
·103 lines (82 loc) · 2.83 KB
/
basic_usage.py
File metadata and controls
executable file
·103 lines (82 loc) · 2.83 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
#!/usr/bin/env python3
"""
Agents - Real Usage Examples
Demonstrates actual agent capabilities:
- Agent client interfaces
- Orchestrator stubs
"""
import sys
from pathlib import Path
# Ensure codomyrmex is in path
try:
import codomyrmex
except ImportError:
project_root = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
from codomyrmex.utils.cli_helpers import (
print_error,
print_info,
print_success,
setup_logging,
)
try:
from codomyrmex.agents import (
AgentCapabilities,
AgentOrchestrator,
AgentRequest,
AgentResponse,
BaseAgent,
)
except ImportError:
# Handle missing optional dependencies (e.g., aiohttp)
def main():
setup_logging()
print_info(f"Agents module dependencies not available: {e}")
print_info("Install with: pip install aiohttp")
print_info("Skipping agents examples - success.")
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
else:
# Let the import error propagate if not running as main
raise
# 1. Define a Mock Agent for demonstration (to avoid requiring real API keys in example)
class DemoAgent(BaseAgent):
def __init__(self, name="demo_agent"):
super().__init__(name=name, capabilities=[AgentCapabilities.TEXT_COMPLETION])
def _execute_impl(self, request: AgentRequest) -> AgentResponse:
self.logger.info(
f"Agent {self.name} processing prompt: {request.prompt[:20]}..."
)
return AgentResponse(
content=f"Response from {self.name} for: {request.prompt}",
metadata={"agent": self.name},
)
def main():
setup_logging()
print_info("Running Agents Examples...")
# 1. Orchestrator and Client Usage
print_info("Initializing real AgentOrchestrator with demo agents...")
agent_a = DemoAgent(name="Agent_A")
agent_b = DemoAgent(name="Agent_B")
orchestrator = AgentOrchestrator(agents=[agent_a, agent_b])
request = AgentRequest(prompt="What is the capital of France?")
# 2. Parallel Execution
print_info("Executing request in parallel across orchestrated agents...")
responses = orchestrator.execute_parallel(request)
for resp in responses:
if resp.is_success():
print_success(
f" {resp.metadata.get('agent', 'Unknown agent')}: {resp.content}"
)
else:
print_error(f" Agent failed: {resp.error}")
# 3. Fallback Execution
print_info("Executing with fallback strategy...")
fallback_response = orchestrator.execute_with_fallback(request)
print_success(f" Fallback Result: {fallback_response.content}")
print_success("Agents examples completed successfully")
return 0
if __name__ == "__main__":
sys.exit(main())