Delete crew.py
Browse files
crew.py
DELETED
|
@@ -1,225 +0,0 @@
|
|
| 1 |
-
# References:
|
| 2 |
-
|
| 3 |
-
# https://docs.crewai.com/introduction
|
| 4 |
-
# https://ai.google.dev/gemini-api/docs
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
from crewai import Agent, Crew, Task
|
| 8 |
-
from crewai.agents.agent_builder.base_agent import BaseAgent
|
| 9 |
-
from crewai.project import CrewBase, agent, crew, task
|
| 10 |
-
from google import genai
|
| 11 |
-
from tools.ai_tools import AITools
|
| 12 |
-
from tools.arithmetic_tools import ArithmeticTools
|
| 13 |
-
from typing import List
|
| 14 |
-
from utils import read_file_json, is_ext
|
| 15 |
-
|
| 16 |
-
# LLMs
|
| 17 |
-
|
| 18 |
-
MANAGER_MODEL = "gpt-4.1"
|
| 19 |
-
AGENT_MODEL = "gpt-4.1-mini"
|
| 20 |
-
|
| 21 |
-
FINAL_ANSWER_MODEL = "gemini-2.5-pro"
|
| 22 |
-
|
| 23 |
-
@CrewBase
|
| 24 |
-
class GAIACrew():
|
| 25 |
-
agents: List[BaseAgent]
|
| 26 |
-
tasks: List[Task]
|
| 27 |
-
|
| 28 |
-
@agent
|
| 29 |
-
def web_search_agent(self) -> Agent:
|
| 30 |
-
return Agent(
|
| 31 |
-
config=self.agents_config["web_search_agent"],
|
| 32 |
-
allow_delegation=False,
|
| 33 |
-
llm=AGENT_MODEL,
|
| 34 |
-
max_iter=2,
|
| 35 |
-
tools=[AITools.web_search_tool],
|
| 36 |
-
verbose=True
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
@agent
|
| 40 |
-
def web_browser_agent(self) -> Agent:
|
| 41 |
-
return Agent(
|
| 42 |
-
config=self.agents_config["web_browser_agent"],
|
| 43 |
-
allow_delegation=False,
|
| 44 |
-
llm=AGENT_MODEL,
|
| 45 |
-
max_iter=3,
|
| 46 |
-
tools=[AITools.web_browser_tool],
|
| 47 |
-
verbose=True
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
@agent
|
| 51 |
-
def chess_analysis_agent(self) -> Agent:
|
| 52 |
-
return Agent(
|
| 53 |
-
config=self.agents_config["chess_analysis_agent"],
|
| 54 |
-
allow_delegation=False,
|
| 55 |
-
llm=AGENT_MODEL,
|
| 56 |
-
max_iter=2,
|
| 57 |
-
tools=[AITools.chess_analysis_tool],
|
| 58 |
-
verbose=True
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
@agent
|
| 62 |
-
def image_analysis_agent(self) -> Agent:
|
| 63 |
-
return Agent(
|
| 64 |
-
config=self.agents_config["image_analysis_agent"],
|
| 65 |
-
allow_delegation=False,
|
| 66 |
-
llm=AGENT_MODEL,
|
| 67 |
-
max_iter=2,
|
| 68 |
-
tools=[AITools.image_analysis_tool],
|
| 69 |
-
verbose=True
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
@agent
|
| 73 |
-
def audio_analysis_agent(self) -> Agent:
|
| 74 |
-
return Agent(
|
| 75 |
-
config=self.agents_config["audio_analysis_agent"],
|
| 76 |
-
allow_delegation=False,
|
| 77 |
-
llm=AGENT_MODEL,
|
| 78 |
-
max_iter=2,
|
| 79 |
-
tools=[AITools.audio_analysis_tool],
|
| 80 |
-
verbose=True
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
@agent
|
| 84 |
-
def video_analysis_agent(self) -> Agent:
|
| 85 |
-
return Agent(
|
| 86 |
-
config=self.agents_config["video_analysis_agent"],
|
| 87 |
-
allow_delegation=False,
|
| 88 |
-
llm=AGENT_MODEL,
|
| 89 |
-
max_iter=2,
|
| 90 |
-
tools=[AITools.video_analysis_tool],
|
| 91 |
-
verbose=True
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
@agent
|
| 95 |
-
def youtube_analysis_agent(self) -> Agent:
|
| 96 |
-
return Agent(
|
| 97 |
-
config=self.agents_config["youtube_analysis_agent"],
|
| 98 |
-
allow_delegation=False,
|
| 99 |
-
llm=AGENT_MODEL,
|
| 100 |
-
max_iter=2,
|
| 101 |
-
tools=[AITools.youtube_analysis_tool],
|
| 102 |
-
verbose=True
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
@agent
|
| 106 |
-
def document_analysis_agent(self) -> Agent:
|
| 107 |
-
return Agent(
|
| 108 |
-
config=self.agents_config["document_analysis_agent"],
|
| 109 |
-
allow_delegation=False,
|
| 110 |
-
llm=AGENT_MODEL,
|
| 111 |
-
max_iter=2,
|
| 112 |
-
tools=[AITools.document_analysis_tool],
|
| 113 |
-
verbose=True
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
@agent
|
| 117 |
-
def arithmetic_agent(self) -> Agent:
|
| 118 |
-
return Agent(
|
| 119 |
-
config=self.agents_config["document_analysis_agent"],
|
| 120 |
-
allow_delegation=False,
|
| 121 |
-
llm=AGENT_MODEL,
|
| 122 |
-
max_iter=2,
|
| 123 |
-
tools=[ArithmeticTools.add, ArithmeticTools.subtract, ArithmeticTools.multiply, ArithmeticTools.divide, ArithmeticTools.modulus],
|
| 124 |
-
verbose=True
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
@agent
|
| 128 |
-
def code_generation_agent(self) -> Agent:
|
| 129 |
-
return Agent(
|
| 130 |
-
config=self.agents_config["code_generation_agent"],
|
| 131 |
-
allow_delegation=False,
|
| 132 |
-
llm=AGENT_MODEL,
|
| 133 |
-
max_iter=3,
|
| 134 |
-
tools=[AITools.code_generation_tool],
|
| 135 |
-
verbose=True
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
@agent
|
| 139 |
-
def code_execution_agent(self) -> Agent:
|
| 140 |
-
return Agent(
|
| 141 |
-
config=self.agents_config["code_execution_agent"],
|
| 142 |
-
allow_delegation=False,
|
| 143 |
-
llm=AGENT_MODEL,
|
| 144 |
-
max_iter=3,
|
| 145 |
-
tools=[AITools.code_execution_tool],
|
| 146 |
-
verbose=True
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
@agent
|
| 150 |
-
def manager_agent(self) -> Agent:
|
| 151 |
-
return Agent(
|
| 152 |
-
config=self.agents_config["manager_agent"],
|
| 153 |
-
allow_delegation=True,
|
| 154 |
-
llm=MANAGER_MODEL,
|
| 155 |
-
max_iter=5,
|
| 156 |
-
verbose=True
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
@task
|
| 160 |
-
def manager_task(self) -> Task:
|
| 161 |
-
return Task(
|
| 162 |
-
config=self.tasks_config["manager_task"]
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
@crew
|
| 166 |
-
def crew(self) -> Crew:
|
| 167 |
-
return Crew(
|
| 168 |
-
agents=self.agents,
|
| 169 |
-
tasks=self.tasks,
|
| 170 |
-
verbose=True
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
def run_crew(question, file_path):
|
| 174 |
-
final_question = question
|
| 175 |
-
|
| 176 |
-
if file_path:
|
| 177 |
-
if is_ext(file_path, ".csv") or is_ext(file_path, ".xls") or is_ext(file_path, ".xlsx") or is_ext(file_path, ".json") or is_ext(file_path, ".jsonl"):
|
| 178 |
-
json_data = read_file_json(file_path)
|
| 179 |
-
final_question = f"{question} JSON data:\n{json_data}."
|
| 180 |
-
else:
|
| 181 |
-
final_question = f"{question} File path: {file_path}."
|
| 182 |
-
|
| 183 |
-
answer = GAIACrew().crew().kickoff(inputs={"question": final_question})
|
| 184 |
-
final_answer = get_final_answer(FINAL_ANSWER_MODEL, question, str(answer))
|
| 185 |
-
|
| 186 |
-
print(f"=> Initial question: {question}")
|
| 187 |
-
print(f"=> Final question: {final_question}")
|
| 188 |
-
print(f"=> Initial answer: {answer}")
|
| 189 |
-
print(f"=> Final answer: {final_answer}")
|
| 190 |
-
|
| 191 |
-
return final_answer
|
| 192 |
-
|
| 193 |
-
def get_final_answer(model, question, answer):
|
| 194 |
-
prompt_template = """
|
| 195 |
-
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|
| 196 |
-
Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
| 197 |
-
If you are asked for a number, don't use comma to write your number neither use units such as USD, $, percent, or % unless specified otherwise.
|
| 198 |
-
If you are asked for a string, don't use articles, neither abbreviations (for example cities), and write the digits in plain text unless specified otherwise.
|
| 199 |
-
If you are asked for a comma-separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 200 |
-
If the final answer is a number, use a number not a word.
|
| 201 |
-
If the final answer is a string, start with an uppercase character.
|
| 202 |
-
If the final answer is a comma-separated list of numbers, use a space character after each comma.
|
| 203 |
-
If the final answer is a comma-separated list of strings, use a space character after each comma and start with a lowercase character.
|
| 204 |
-
Do not add any content to the final answer that is not in the initial answer.
|
| 205 |
-
**Question:** """ + question + """
|
| 206 |
-
|
| 207 |
-
**Initial answer:** """ + answer + """
|
| 208 |
-
|
| 209 |
-
**Example 1:** What is the biggest city in California? Los Angeles
|
| 210 |
-
**Example 2:** How many 'r's are in strawberry? 3
|
| 211 |
-
**Example 3:** What is the opposite of black? White
|
| 212 |
-
**Example 4:** What are the first 5 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3
|
| 213 |
-
**Example 5:** What is the opposite of bad, worse, worst? good, better, best
|
| 214 |
-
|
| 215 |
-
**Final answer:**
|
| 216 |
-
"""
|
| 217 |
-
|
| 218 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 219 |
-
|
| 220 |
-
response = client.models.generate_content(
|
| 221 |
-
model=model,
|
| 222 |
-
contents=[prompt_template]
|
| 223 |
-
)
|
| 224 |
-
|
| 225 |
-
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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