bstraehle commited on
Commit
2538f72
·
verified ·
1 Parent(s): 3d55f04

Update crew.py

Browse files
Files changed (1) hide show
  1. crew.py +107 -155
crew.py CHANGED
@@ -6,6 +6,8 @@
6
  import os
7
  import pandas as pd
8
  from crewai import Agent, Crew, Process, Task
 
 
9
  from crewai.tools import tool
10
  #from crewai_tools import StagehandTool
11
  from google import genai
@@ -13,6 +15,7 @@ from google.genai import types
13
  from openinference.instrumentation.crewai import CrewAIInstrumentor
14
  from phoenix.otel import register
15
  from tools import add, subtract, multiply, divide, modulus
 
16
  from utils import read_file_json, read_docx_text, read_pptx_text, is_ext
17
 
18
  ## LLMs
@@ -46,9 +49,11 @@ tracer_provider = register(
46
 
47
  #CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
48
 
49
- def run_crew(question, file_path):
50
- # Tools
51
-
 
 
52
  @tool("Web Search Tool")
53
  def web_search_tool(question: str) -> str:
54
  """Given a question only, search the web to answer the question.
@@ -311,175 +316,122 @@ def run_crew(question, file_path):
311
  except Exception as e:
312
  raise RuntimeError(f"Processing failed: {str(e)}")
313
 
314
- # Agents
315
-
316
- web_search_agent = Agent(
317
- role="Web Search Agent",
318
- goal="Given a question only, search the web and answer the question: {question}",
319
- backstory="As an expert web search assistant, you search the web to answer the question.",
320
- allow_delegation=False,
321
- llm=AGENT_MODEL,
322
- max_iter=2,
323
- tools=[web_search_tool],
324
- verbose=True
325
- )
326
-
327
- #with StagehandTool(
328
- # api_key=os.environ["BROWSERBASE_API_KEY"],
329
- # project_id=os.environ["BROWSERBASE_PROJECT_ID"],
330
- # model_api_key=os.environ["OPENAI_API_KEY"],
331
- # model_name="gpt-4.1"
332
- #) as web_browser_tool:
333
- # web_browser_agent = Agent(
334
- # role="Web Browser Agent",
335
- # goal="Given a question and URL, load the URL and act, extract, navigate, observe, and answer the question: {question}",
336
- # backstory="As an expert browser assistant, you load the URL and act, extract, navigate, observe to answer the question.",
337
- # allow_delegation=False,
338
- # llm=AGENT_MODEL,
339
- # max_iter=2,
340
- # tools=[web_browser_tool],
341
- # verbose=True
342
- # )
343
-
344
- image_analysis_agent = Agent(
345
- role="Image Analysis Agent",
346
- goal="Given a question and image file, analyze the image and answer the question: {question}",
347
- backstory="As an expert image analysis assistant, you analyze the image to answer the question.",
348
- allow_delegation=False,
349
- llm=AGENT_MODEL,
350
- max_iter=2,
351
- tools=[image_analysis_tool],
352
- verbose=True
353
- )
354
-
355
- audio_analysis_agent = Agent(
356
- role="Audio Analysis Agent",
357
- goal="Given a question and audio file, analyze the audio and answer the question: {question}",
358
- backstory="As an expert audio analysis assistant, you analyze the audio to answer the question.",
359
- allow_delegation=False,
360
- llm=AGENT_MODEL,
361
- max_iter=2,
362
- tools=[audio_analysis_tool],
363
- verbose=True
364
- )
365
-
366
- video_analysis_agent = Agent(
367
- role="Video Analysis Agent",
368
- goal="Given a question and video file, analyze the video and answer the question: {question}",
369
- backstory="As an expert video analysis assistant, you analyze the video file to answer the question.",
370
- allow_delegation=False,
371
- llm=AGENT_MODEL,
372
- max_iter=2,
373
- tools=[video_analysis_tool],
374
- verbose=True
375
- )
376
 
377
- youtube_analysis_agent = Agent(
378
- role="YouTube Analysis Agent",
379
- goal="Given a question and YouTube URL, analyze the video and answer the question: {question}",
380
- backstory="As an expert YouTube analysis assistant, you analyze the video to answer the question.",
381
- allow_delegation=False,
382
- llm=AGENT_MODEL,
383
- max_iter=2,
384
- tools=[youtube_analysis_tool],
385
- verbose=True
386
- )
387
-
388
- document_analysis_agent = Agent(
389
- role="Document Analysis Agent",
390
- goal="Given a question and document file, analyze the document and answer the question: {question}",
391
- backstory="As an expert document analysis assistant, you analyze the document to answer the question.",
392
- allow_delegation=False,
393
- llm=AGENT_MODEL,
394
- max_iter=2,
395
- tools=[document_analysis_tool],
396
- verbose=True
397
- )
398
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  arithmetic_agent = Agent(
400
- role="Arithmetic Agent",
401
- goal="Given a question and two numbers, perform the calculation and answer the question: {question}",
402
- backstory="As an expert arithmetic assistant, you perform the calculation to answer the question.",
403
  allow_delegation=False,
404
  llm=AGENT_MODEL,
405
  max_iter=2,
406
  tools=[arithmetic_tool],
407
  verbose=True
408
  )
409
-
410
- code_execution_agent = Agent(
411
- role="Code Execution Agent",
412
- goal="Given a question and Python file, execute the file to answer the question: {question}",
413
- backstory="As an expert Python code execution assistant, you execute code to answer the question.",
414
- allow_delegation=False,
415
- llm=AGENT_MODEL,
416
- max_iter=3,
417
- tools=[code_execution_tool],
418
- verbose=True
419
- )
420
-
421
- code_generation_agent = Agent(
422
- role="Code Generation Agent",
423
- goal="Given a question and JSON data, generate and execute code to answer the question: {question}",
424
- backstory="As an expert Python code generation assistant, you generate and execute code to answer the question.",
425
- allow_delegation=False,
426
- llm=AGENT_MODEL,
427
- max_iter=3,
428
- tools=[code_generation_tool],
429
- verbose=True
430
- )
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  manager_agent = Agent(
433
- role="Manager Agent",
434
- goal="Answer the following question. If needed, delegate to one of your coworkers. Question: {question}",
435
- backstory="As an expert manager assistant, you answer the question.",
436
  allow_delegation=True,
437
  llm=MANAGER_MODEL,
438
  max_iter=5,
439
  verbose=True
440
  )
441
 
442
- # Task
443
-
444
- manager_task = Task(
445
- agent=manager_agent,
446
- description="Answer the following question. If needed, delegate to one of your coworkers:\n"
447
- "- Web Search Agent requires a question only.\n"
448
- "- Web Browser Agent requires a question and **URL**.\n"
449
- "- Image Analysis Agent requires a question and **.png, .jpeg, .webp, .heic, or .heif image file**.\n"
450
- "- Audio Analysis Agent requires a question and **.wav, .mp3, .aiff, .aac, .ogg, or .flac audio file**.\n"
451
- "- Video Analysis Agent requires a question and **.mp4, .mpeg, .mov, .avi, .x-flv, .mpg, .webm, .wmv, or .3gpp video file**.\n"
452
- "- YouTube Analysis Agent requires a question and **YouTube URL**.\n"
453
- "- Document Analysis Agent requires a question and **.docx, .pptx, .pdf, .txt, .html, css, .js, .md, .xml, or .rtf document file**.\n"
454
- "- Arithmetic Agent requires a question and **two numbers to add, subtract, multiply, divide, or get the modulus**. "
455
- " In case there are more than two numbers, use the Code Generation Agent instead.\n"
456
- "- Code Execution Agent requires a question and **Python file**.\n"
457
- "- Code Generation Agent requires a question and **JSON data**.\n"
458
- "In case you cannot answer the question and there is not a good coworker, delegate to the Code Generation Agent.\n"
459
- "Question: {question}",
460
- expected_output="The answer to the question."
461
- )
462
-
463
- # Crew
464
-
465
- crew = Crew(
466
- agents=[web_search_agent,
467
- #web_browser_agent,
468
- image_analysis_agent,
469
- audio_analysis_agent,
470
- video_analysis_agent,
471
- youtube_analysis_agent,
472
- document_analysis_agent,
473
- arithmetic_agent,
474
- code_execution_agent,
475
- code_generation_agent],
476
- manager_agent=manager_agent,
477
- tasks=[manager_task],
478
- verbose=True
479
- )
480
 
481
- # Process
 
 
 
 
 
 
482
 
 
483
  final_question = question
484
 
485
  if file_path:
@@ -489,7 +441,7 @@ def run_crew(question, file_path):
489
  else:
490
  final_question = f"{question} File path: {file_path}."
491
 
492
- answer = crew.kickoff(inputs={"question": final_question})
493
  final_answer = get_final_answer(FINAL_ANSWER_MODEL, question, str(answer))
494
 
495
  print(f"=> Initial question: {question}")
 
6
  import os
7
  import pandas as pd
8
  from crewai import Agent, Crew, Process, Task
9
+ from crewai.agents.agent_builder.base_agent import BaseAgent
10
+ from crewai.project import CrewBase, agent, crew, task
11
  from crewai.tools import tool
12
  #from crewai_tools import StagehandTool
13
  from google import genai
 
15
  from openinference.instrumentation.crewai import CrewAIInstrumentor
16
  from phoenix.otel import register
17
  from tools import add, subtract, multiply, divide, modulus
18
+ from typing import List
19
  from utils import read_file_json, read_docx_text, read_pptx_text, is_ext
20
 
21
  ## LLMs
 
49
 
50
  #CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
51
 
52
+ @CrewBase
53
+ class GAIACrew():
54
+ agents: List[BaseAgent]
55
+ tasks: List[Task]
56
+
57
  @tool("Web Search Tool")
58
  def web_search_tool(question: str) -> str:
59
  """Given a question only, search the web to answer the question.
 
316
  except Exception as e:
317
  raise RuntimeError(f"Processing failed: {str(e)}")
318
 
319
+ @agent
320
+ def web_search_agent(self) -> Agent:
321
+ return Agent(
322
+ allow_delegation=False,
323
+ llm=AGENT_MODEL,
324
+ max_iter=2,
325
+ tools=[web_search_tool],
326
+ verbose=True
327
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
+ @agent
330
+ def image_analysis_agent(self) -> Agent:
331
+ return Agent(
332
+ allow_delegation=False,
333
+ llm=AGENT_MODEL,
334
+ max_iter=2,
335
+ tools=[image_analysis_tool],
336
+ verbose=True
337
+ )
338
+
339
+ @agent
340
+ def audio_analysis_agent(self) -> Agent:
341
+ return Agent(
342
+ allow_delegation=False,
343
+ llm=AGENT_MODEL,
344
+ max_iter=2,
345
+ tools=[audio_analysis_tool],
346
+ verbose=True
347
+ )
348
+
349
+ @agent
350
+ def video_analysis_agent(self) -> Agent:
351
+ return Agent(
352
+ allow_delegation=False,
353
+ llm=AGENT_MODEL,
354
+ max_iter=2,
355
+ tools=[video_analysis_tool],
356
+ verbose=True
357
+ )
358
+
359
+ @agent
360
+ def youtube_analysis_agent(self) -> Agent:
361
+ return Agent(
362
+ allow_delegation=False,
363
+ llm=AGENT_MODEL,
364
+ max_iter=2,
365
+ tools=[youtube_analysis_tool],
366
+ verbose=True
367
+ )
368
+
369
+ @agent
370
+ def document_analysis_agent(self) -> Agent:
371
+ return Agent(
372
+ allow_delegation=False,
373
+ llm=AGENT_MODEL,
374
+ max_iter=2,
375
+ tools=[document_analysis_tool],
376
+ verbose=True
377
+ )
378
+
379
+ @agent
380
+ def arithmetic_agent(self) -> Agent:
381
+ return Agent(
382
  arithmetic_agent = Agent(
 
 
 
383
  allow_delegation=False,
384
  llm=AGENT_MODEL,
385
  max_iter=2,
386
  tools=[arithmetic_tool],
387
  verbose=True
388
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
 
390
+ @agent
391
+ def code_execution_agent(self) -> Agent:
392
+ return Agent(
393
+ allow_delegation=False,
394
+ llm=AGENT_MODEL,
395
+ max_iter=3,
396
+ tools=[code_execution_tool],
397
+ verbose=True
398
+ )
399
+
400
+ @agent
401
+ def code_generation_agent(self) -> Agent:
402
+ return Agent(
403
+ allow_delegation=False,
404
+ llm=AGENT_MODEL,
405
+ max_iter=3,
406
+ tools=[code_generation_tool],
407
+ verbose=True
408
+ )
409
+
410
+ @agent
411
+ def image_analysis_agent(self) -> Agent:
412
+ return Agent(
413
  manager_agent = Agent(
 
 
 
414
  allow_delegation=True,
415
  llm=MANAGER_MODEL,
416
  max_iter=5,
417
  verbose=True
418
  )
419
 
420
+ @task
421
+ def manager_task(self) -> Task:
422
+ return Task(
423
+ config=self.tasks_config['manager_task']
424
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
 
426
+ @crew
427
+ def crew(self) -> Crew:
428
+ return Crew(
429
+ agents=self.agents,
430
+ tasks=self.tasks,
431
+ verbose=True
432
+ )
433
 
434
+ def run_crew(question, file_path):
435
  final_question = question
436
 
437
  if file_path:
 
441
  else:
442
  final_question = f"{question} File path: {file_path}."
443
 
444
+ answer = GAIACrew().crew().kickoff(inputs={"question": final_question})
445
  final_answer = get_final_answer(FINAL_ANSWER_MODEL, question, str(answer))
446
 
447
  print(f"=> Initial question: {question}")