cryogenic22 commited on
Commit
260c85c
·
verified ·
1 Parent(s): 344e37b

Create core/agents/crewai_agent.py

Browse files
Files changed (1) hide show
  1. core/agents/crewai_agent.py +143 -0
core/agents/crewai_agent.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any, List, Optional
2
+ from .base import BaseAgent, TaskInput, AgentTool
3
+ import crewai
4
+ from crewai import Agent as CrewAgent, Task as CrewTask, Process
5
+ from datetime import datetime
6
+ import asyncio
7
+
8
+ class CrewAIAgent(BaseAgent):
9
+ """CrewAI-based agent implementation"""
10
+
11
+ async def initialize(self) -> None:
12
+ """Initialize CrewAI agent"""
13
+ await super().initialize()
14
+
15
+ try:
16
+ # Convert tools to CrewAI format
17
+ crew_tools = [
18
+ self._convert_tool_to_crew(tool)
19
+ for tool in self.agent_config.tools
20
+ ]
21
+
22
+ # Create CrewAI agent
23
+ self.crew_agent = CrewAgent(
24
+ role=self.agent_config.role.value,
25
+ goal=self.agent_config.description,
26
+ backstory=self.agent_config.system_message,
27
+ tools=crew_tools,
28
+ verbose=True
29
+ )
30
+
31
+ except Exception as e:
32
+ self.logger.error(f"Failed to initialize CrewAI agent: {str(e)}")
33
+ raise
34
+
35
+ def _convert_tool_to_crew(self, tool: AgentTool) -> crewai.tools.Tool:
36
+ """Convert Lattice tool to CrewAI tool"""
37
+ return crewai.tools.Tool(
38
+ name=tool.name,
39
+ description=tool.description,
40
+ func=tool.function
41
+ )
42
+
43
+ async def _execute_implementation(self, task: TaskInput) -> Dict[str, Any]:
44
+ """Execute task using CrewAI"""
45
+ try:
46
+ # Create CrewAI task
47
+ crew_task = CrewTask(
48
+ description=task.description,
49
+ agent=self.crew_agent,
50
+ expected_output=task.inputs.get('expected_output'),
51
+ tools=task.tools if task.tools else None
52
+ )
53
+
54
+ # Create crew with single agent
55
+ crew = crewai.Crew(
56
+ agents=[self.crew_agent],
57
+ tasks=[crew_task],
58
+ process=Process.sequential
59
+ )
60
+
61
+ # Execute task
62
+ result = await asyncio.to_thread(crew.kickoff)
63
+
64
+ return {
65
+ 'output': result,
66
+ 'metadata': {
67
+ 'framework': 'crewai',
68
+ 'process': 'sequential'
69
+ }
70
+ }
71
+
72
+ except Exception as e:
73
+ self.logger.error(f"CrewAI task execution failed: {str(e)}")
74
+ raise
75
+
76
+ class CrewAIWorkflow:
77
+ """CrewAI workflow implementation"""
78
+
79
+ def __init__(self, config: Dict[str, Any]):
80
+ self.config = config
81
+ self.logger = logging.getLogger("lattice.agent.crewai.workflow")
82
+
83
+ async def execute_workflow(
84
+ self,
85
+ agents: List[CrewAIAgent],
86
+ tasks: List[TaskInput]
87
+ ) -> Dict[str, Any]:
88
+ """Execute a workflow with multiple agents"""
89
+ try:
90
+ # Convert to CrewAI format
91
+ crew_agents = [agent.crew_agent for agent in agents]
92
+ crew_tasks = [
93
+ CrewTask(
94
+ description=task.description,
95
+ agent=agents[i].crew_agent,
96
+ expected_output=task.inputs.get('expected_output'),
97
+ tools=task.tools
98
+ )
99
+ for i, task in enumerate(tasks)
100
+ ]
101
+
102
+ # Create crew
103
+ crew = crewai.Crew(
104
+ agents=crew_agents,
105
+ tasks=crew_tasks,
106
+ process=Process.sequential
107
+ )
108
+
109
+ # Execute workflow
110
+ results = await asyncio.to_thread(crew.kickoff)
111
+
112
+ return {
113
+ 'results': results,
114
+ 'metadata': {
115
+ 'framework': 'crewai',
116
+ 'agent_count': len(agents),
117
+ 'task_count': len(tasks)
118
+ },
119
+ 'timestamp': datetime.now().isoformat()
120
+ }
121
+
122
+ except Exception as e:
123
+ self.logger.error(f"CrewAI workflow execution failed: {str(e)}")
124
+ raise
125
+
126
+ # Tool conversion utilities
127
+ def convert_tool_to_crew(tool: AgentTool) -> crewai.tools.Tool:
128
+ """Convert a Lattice tool to CrewAI format"""
129
+ return crewai.tools.Tool(
130
+ name=tool.name,
131
+ description=tool.description,
132
+ func=tool.function
133
+ )
134
+
135
+ def convert_crew_to_tool(crew_tool: crewai.tools.Tool) -> AgentTool:
136
+ """Convert a CrewAI tool to Lattice format"""
137
+ return AgentTool(
138
+ name=crew_tool.name,
139
+ description=crew_tool.description,
140
+ function=crew_tool.func,
141
+ required_permissions=[], # Add appropriate permissions
142
+ metadata={'framework': 'crewai'}
143
+ )