Spaces:
Running
Running
File size: 12,610 Bytes
7f1b7ba 15ce3a3 7f1b7ba c16c2b3 7f1b7ba e6967c2 15ce3a3 e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba e6967c2 15ce3a3 7f1b7ba 15ce3a3 7f1b7ba 15ce3a3 e6967c2 15ce3a3 7f1b7ba 484e225 7f1b7ba 484e225 7f1b7ba 484e225 7f1b7ba c32764e 7f1b7ba c32764e 7f1b7ba |
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 |
# References:
# https://docs.crewai.com/introduction
# https://arize.com/docs/phoenix/integrations/python/crewai/crewai-tracing
import logging, os
os.environ["CREWAI_TESTING"] = "TRUE" # Arize Phoenix
os.environ["CREWAI_TRACING_ENABLED"] = "0" # Arize Phoenix
from agents.models.llms import (
LLM_CREW_PLANNING,
LLM_MANAGER,
LLM_AGENT
)
from agents.tools.ai_tools import AITools
from agents.tools.deterministic_tools import DeterministicTools
from agents.tools.mcp_tools import MCPTools
from crewai import Agent, Crew, Task, Process
from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.events.listeners.tracing.first_time_trace_handler import FirstTimeTraceHandler
from crewai.project import CrewBase, agent, crew, task
from google import genai
from google.genai import types
from phoenix.otel import register
from typing import List
from utils.utils import read_file_json, is_ext
def _noop(self, *args, **kwargs):
pass
FirstTimeTraceHandler._display_ephemeral_trace_link = _noop
FirstTimeTraceHandler._show_local_trace_message = _noop
FirstTimeTraceHandler._show_tracing_declined_message = _noop
# Configuration
PLANNING_CREW = True
MEMORY_CREW = False
VERBOSE_CREW = False
MAX_ITER_MANAGER = 15
REASONING_MANAGER = True
MAX_REASONING_ATTEMPTS_MANAGER = 3
VERBOSE_MANAGER = False
MAX_ITER_AGENT = 15
REASONING_AGENT = True
MAX_REASONING_ATTEMPTS_AGENT = 2
VERBOSE_AGENT = False
os.environ["CHROMA_OPENAI_API_KEY"] = os.getenv("CHROMA_OPENAI_API_KEY") # Memory
# Observability
os.environ["PHOENIX_API_KEY"] = os.getenv("PHOENIX_API_KEY")
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = "https://app.phoenix.arize.com/s/bstraehle/v1/traces"
os.environ["PHOENIX_CLIENT_HEADERS"] = f"api-key={os.environ['PHOENIX_API_KEY']}"
tracer_provider = register(
project_name="gaia",
protocol="http/protobuf",
auto_instrument=True,
batch=True,
set_global_tracer_provider=True
)
logging.getLogger("openinference").setLevel(logging.CRITICAL)
logging.getLogger("opentelemetry").setLevel(logging.CRITICAL)
def _print_agent_config(agent_name: str, reasoning: bool, max_reasoning_attempts: int, max_iter: int, verbose: bool, llm: str, tools: str = None):
print("")
print(f"π€ {agent_name} reasoning: {reasoning}")
print(f"π€ {agent_name} reasoning max attempts: {max_reasoning_attempts}")
print(f"π€ {agent_name} max iterations: {max_iter}")
print(f"π€ {agent_name} verbose: {verbose}")
print(f"π§ {agent_name} llm: {llm}")
if tools:
print(f"π οΈ {agent_name} tools: {tools}")
@CrewBase
class GAIACrew():
agents: List[BaseAgent]
tasks: List[Task]
@agent
def web_search_agent(self) -> Agent:
_print_agent_config("Web search agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.web_search_tool")
return Agent(
config=self.agents_config["web_search_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.web_search_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def web_browser_agent(self) -> Agent:
_print_agent_config("Web browser agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.web_browser_tool")
return Agent(
config=self.agents_config["web_browser_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.web_browser_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def chess_analysis_agent(self) -> Agent:
_print_agent_config("Chess analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.img_to_fen_tool, MCPTools.best_move_tool, AITools.algebraic_notation_tool")
return Agent(
config=self.agents_config["chess_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.img_to_fen_tool, MCPTools.best_move_tool, AITools.algebraic_notation_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def image_analysis_agent(self) -> Agent:
_print_agent_config("Image analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.image_analysis_tool")
return Agent(
config=self.agents_config["image_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.image_analysis_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def audio_analysis_agent(self) -> Agent:
_print_agent_config("Audio analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.audio_analysis_tool")
return Agent(
config=self.agents_config["audio_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.audio_analysis_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def video_analysis_agent(self) -> Agent:
_print_agent_config("Video analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.video_analysis_tool")
return Agent(
config=self.agents_config["video_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.video_analysis_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def youtube_analysis_agent(self) -> Agent:
_print_agent_config("Youtube analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.youtube_analysis_tool")
return Agent(
config=self.agents_config["youtube_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.youtube_analysis_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def document_analysis_agent(self) -> Agent:
_print_agent_config("Document analysis agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.document_analysis_tool")
return Agent(
config=self.agents_config["document_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.document_analysis_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def arithmetic_agent(self) -> Agent:
_print_agent_config("Arithmetic agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"DeterministicTools.add_tool, DeterministicTools.subtract_tool, DeterministicTools.multiply_tool, DeterministicTools.divide_tool, DeterministicTools.modulus_tool")
return Agent(
config=self.agents_config["document_analysis_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[DeterministicTools.add_tool,
DeterministicTools.subtract_tool,
DeterministicTools.multiply_tool,
DeterministicTools.divide_tool,
DeterministicTools.modulus_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def code_generation_and_execution_agent(self) -> Agent:
_print_agent_config("Code generation and execution agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.code_generation_and_execution_tool")
return Agent(
config=self.agents_config["code_generation_and_execution_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.code_generation_and_execution_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def code_execution_agent(self) -> Agent:
_print_agent_config("Code execution agent", REASONING_AGENT, MAX_REASONING_ATTEMPTS_AGENT, MAX_ITER_AGENT, VERBOSE_AGENT, LLM_AGENT,
"AITools.code_execution_tool")
return Agent(
config=self.agents_config["code_execution_agent"],
allow_delegation=False,
llm=LLM_AGENT,
max_iter=MAX_ITER_AGENT,
tools=[AITools.code_execution_tool],
reasoning=REASONING_AGENT,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_AGENT,
verbose=VERBOSE_AGENT
)
@agent
def manager_agent(self) -> Agent:
_print_agent_config("Manager", REASONING_MANAGER, MAX_REASONING_ATTEMPTS_MANAGER, MAX_ITER_MANAGER, VERBOSE_MANAGER, LLM_MANAGER)
return Agent(
config=self.agents_config["manager_agent"],
allow_delegation=True,
llm=LLM_MANAGER,
max_iter=MAX_ITER_MANAGER,
reasoning=REASONING_MANAGER,
max_reasoning_attempts=MAX_REASONING_ATTEMPTS_MANAGER,
verbose=VERBOSE_MANAGER
)
@task
def manager_task(self) -> Task:
return Task(
config=self.tasks_config["manager_task"]
)
@crew
def crew(self) -> Crew:
print("")
print(f"π€ Crew planning: {PLANNING_CREW}")
print(f"π€ Crew memory: {MEMORY_CREW}")
print(f"π€ Crew verbose: {VERBOSE_CREW}")
print(f"π§ Crew planning LLM: {LLM_CREW_PLANNING}")
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
planning_llm=LLM_CREW_PLANNING,
planning=PLANNING_CREW,
memory=MEMORY_CREW,
verbose=VERBOSE_CREW,
tracing=False # Arize Phoenix
)
def _get_final_question(question, file_path):
final_question = question
if file_path:
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"):
json_data = read_file_json(file_path)
final_question = f"{question} JSON data:\n{json_data}."
else:
final_question = f"{question} File path: {file_path}."
return final_question
def run_crew(question, file_path):
print("")
print(f"π€ Question: {question}")
final_question = _get_final_question(question, file_path)
print("")
print(f"π€ Crew execution started")
answer = GAIACrew().crew().kickoff(inputs={"question": final_question})
print("")
print(f"π€ Crew execution completed")
final_answer = AITools.final_answer_tool(question, answer)
print("")
print(f"π€ Answer: {final_answer}")
return final_answer |