-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcrew.py
More file actions
349 lines (310 loc) · 11.2 KB
/
crew.py
File metadata and controls
349 lines (310 loc) · 11.2 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
import os
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
# might need global `magic` installed: brew install magic
from crewai.knowledge.source.text_file_knowledge_source import TextFileKnowledgeSource
from crewai import LLM
from crewai_tools import (SerperDevTool, WebsiteSearchTool, FileReadTool,
CodeInterpreterTool, FileWriterTool, ScrapeWebsiteTool,
DirectorySearchTool)
from crewai_tools.tools.code_docs_search_tool.code_docs_search_tool import CodeDocsSearchTool
from helpers.get_docs_string import LocalTxTFileKnowledgeSource
from helpers.helper import validate_and_save_yaml_from_pydantic_list, write_review_changes_callback
from models import TasksModel, AgentsModel
from tools.files_langchain import FileManagementTool
from tools.shell_tool import ShellCommandTool
from tools.playwright_tool import PlaywrightTool
shell_tool = ShellCommandTool()
file_read_tool = FileReadTool()
file_write_tool = FileWriterTool()
search_tool = SerperDevTool()
web_rag_tool = WebsiteSearchTool()
code_interpreter_tool = CodeInterpreterTool()
playwright_tool = PlaywrightTool()
scrape_website_tool = ScrapeWebsiteTool()
docsSearchTool = DirectorySearchTool(directory='./docs_crewai')
docsSearchRagTool = CodeDocsSearchTool(docs_url='https://docs.crewai.com')
docs_singlefile = TextFileKnowledgeSource(
file_paths=["singlefile.txt"]
)
gpt4o_mini = LLM(
model="gpt-4o-mini",
)
gpt4o = LLM(
model="gpt-4o",
)
gpt1o_mini = LLM(
model="o1-mini-2024-09-12",
)
gpt1o = LLM(
model="o1-preview",
)
gemini2 = LLM(
model="gemini/gemini-2.0-flash-exp",
# temperature=0.7,
)
gemini2think = LLM(
model="gemini/gemini-2.0-flash-thinking-exp-1219",
# temperature=0.7,
)
claude = LLM(
model="anthropic/claude-3-5-sonnet-20241022",
)
@CrewBase
class DesignCrew:
agents_config = 'config/design_crew/agents.yaml'
tasks_config = 'config/design_crew/tasks.yaml'
def __init__(self, crew_name=None):
self.crew_name = 'default_name' if crew_name is None else crew_name
self.file_toolkit = FileManagementTool(
root_dir=f"./{self.crew_name}",
selected_tools=["read_file", "write_file", "list_directory"]
)
@agent
def content_designer(self):
content_designer = Agent(
config=self.agents_config['content_designer'],
tools=[search_tool, scrape_website_tool, file_read_tool, file_write_tool],
allow_delegation=False,
verbose=True,
llm=gpt1o_mini,
max_iter=3,
)
return content_designer
@agent
def content_designer_gemini2(self):
content_designer = Agent(
config=self.agents_config['content_designer'],
tools=[search_tool, scrape_website_tool, file_read_tool, file_write_tool],
allow_delegation=False,
verbose=True,
llm=gemini2,
max_iter=3,
)
return content_designer
@agent
def qa_expert(self):
return Agent(
config=self.agents_config['qa_expert'],
tools=[search_tool, web_rag_tool, file_write_tool],
verbose=True,
llm=claude,
max_iter=3
)
@task
def design_crew_input(self) -> Task:
return Task(
config=self.tasks_config['design_crew_input'],
tools=[search_tool, scrape_website_tool],
output_file=f"output_{self.crew_name}/src/input.json",
)
@task
def design_tasks(self) -> Task:
return Task(
config=self.tasks_config['design_tasks'],
context=[self.design_crew_input()],
tools=[search_tool, scrape_website_tool],
output_pydantic=TasksModel,
callback=lambda output: validate_and_save_yaml_from_pydantic_list(output,
f"output_{self.crew_name}/config/tasks.yaml"),
)
@task
def design_agents(self) -> Task:
return Task(
config=self.tasks_config['design_agents'],
context=[self.design_crew_input(), self.design_tasks()],
tools=[search_tool, scrape_website_tool],
output_pydantic=AgentsModel,
callback=lambda output: validate_and_save_yaml_from_pydantic_list(output,
f"output_{self.crew_name}/config/agents.yaml"),
)
@task
def review_tasks_and_agents(self) -> Task:
return Task(
config=self.tasks_config['review_tasks_and_agents'],
context=[self.design_crew_input(), self.design_tasks(), self.design_agents()],
output_file=f"output_{self.crew_name}/config/review.md",
)
@task
def prepare_review_changes(self) -> Task:
return Task(
config=self.tasks_config['prepare_review_changes'],
context=[self.design_tasks(), self.design_agents(), self.review_tasks_and_agents()],
tools=[file_read_tool, file_write_tool]
)
@task
def write_review_changes(self) -> Task:
return Task(
config=self.tasks_config['write_review_changes'],
context=[self.prepare_review_changes()],
tools=[file_write_tool],
callback=lambda output: write_review_changes_callback(f"output_{self.crew_name}/config"),
output_file=f"output_{self.crew_name}/config/design_result.yaml",
)
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=[
self.design_crew_input(),
self.design_tasks(),
self.design_agents(),
self.review_tasks_and_agents(),
self.prepare_review_changes(),
self.write_review_changes(),
],
process=Process.sequential,
# memory=True,
verbose=True,
knowledge_sources=[docs_singlefile],
)
@CrewBase
class CodingCrew:
agents_config = 'config/coding_crew/agents.yaml'
tasks_config = 'config/coding_crew/tasks.yaml'
def __init__(self, crew_name=None):
self.crew_name = crew_name or 'default_name'
self.output_dir = f"output_{self.crew_name}"
self.directorySearchTool = DirectorySearchTool(directory=self.output_dir)
self.review_iteration = 0
self.max_iterations = 2
@agent
def architect(self) -> Agent:
return Agent(
config=self.agents_config['architect'],
tools=[search_tool, file_read_tool, web_rag_tool],
llm=gpt1o_mini,
max_iter=3
)
@agent
def developer(self) -> Agent:
return Agent(
config=self.agents_config['developer'],
tools=[
file_read_tool,
file_write_tool,
code_interpreter_tool,
self.directorySearchTool,
search_tool
],
llm=gemini2,
max_iter=2
)
@agent
def technical_lead(self) -> Agent:
return Agent(
config=self.agents_config['technical_lead'],
tools=[
file_read_tool,
docsSearchRagTool,
search_tool
],
llm=claude,
max_iter=3
)
@agent
def qa_engineer(self) -> Agent:
return Agent(
config=self.agents_config['qa_engineer'],
tools=[
file_read_tool,
self.directorySearchTool,
code_interpreter_tool,
shell_tool
],
llm=gpt1o_mini,
max_iter=3
)
@task
def select_tools(self) -> Task:
return Task(
config=self.tasks_config['select_tools'],
tools=[file_read_tool, DirectorySearchTool(directory=self.output_dir)]
)
@task
def implement_crew_logic(self) -> Task:
return Task(
config=self.tasks_config['implement_crew_logic'],
context=[self.select_tools()],
tools=[file_write_tool, code_interpreter_tool],
output_file=f"{self.output_dir}/crew.py"
)
@task
def implement_support_files(self) -> Task:
return Task(
config=self.tasks_config['implement_support_files'],
context=[self.implement_crew_logic()],
tools=[file_write_tool, code_interpreter_tool]
)
@task
def review_implementation(self) -> Task:
return Task(
config=self.tasks_config['review_implementation'],
context=[self.select_tools(), self.implement_crew_logic(), self.implement_support_files()],
tools=[file_read_tool, docsSearchRagTool, self.directorySearchTool],
# callback=self.implement_review_changes
)
@task
def implement_review_changes(self) -> Task:
return Task(
config=self.tasks_config['implement_review_changes'],
context=[self.review_implementation()],
tools=[file_write_tool, code_interpreter_tool],
# callback=self._track_review_iteration
)
# def _track_review_iteration(self, output):
# self.review_iteration = getattr(self, 'review_iteration', 0) + 1
# if self.review_iteration < 2 and 'ISSUES FOUND:' in output.raw:
# return self.review_implementation()
# return output
# @task
# def verify_changes(self) -> Task:
# return Task(
# config=self.tasks_config['verify_changes'],
# context=[self.implement_review_changes()],
# tools=[FileReadTool(), DirectoryReadTool(), CodeInterpreterTool()]
# )
#
# @task
# def implement_tests(self) -> Task:
# return Task(
# config=self.tasks_config['implement_tests'],
# context=[self.implement_crew_logic(), self.implement_support_files()],
# tools=[FileWriterTool(), CodeInterpreterTool()],
# output_file=f"{self.output_dir}/tests/test_crew.py"
# )
#
# @task
# def execute_tests(self) -> Task:
# return Task(
# config=self.tasks_config['execute_tests'],
# context=[self.implement_tests()],
# tools=[ShellCommandTool(), CodeInterpreterTool()],
# output_file=f"{self.output_dir}/tests/test_results.md"
# )
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=[
self.select_tools(),
self.implement_crew_logic(),
self.implement_support_files(),
self.review_implementation(),
self.implement_review_changes(),
# self.verify_changes(),
# self.implement_tests(),
# self.execute_tests()
],
process=Process.sequential,
# memory=True,
verbose=True,
knowledge_sources=[docs_singlefile],
embedder={
"provider": "google",
"config": {
"model": "models/text-embedding-004",
"api_key": os.environ.get("GEMINI_API_KEY"),
}
}
)