File size: 10,611 Bytes
f09fcc7 00aced7 adf9d94 1c7a0a7 15b8627 1c7a0a7 617431a 1c7a0a7 2f4d8f8 257d222 f06be5d 230d96d a09b432 2c6f8d9 ce7387d 27f2401 dcfeceb a09b432 27f2401 a09b432 9e82ac9 4c6d89e 27f2401 86f2a58 230d96d df58f18 230d96d 3bd69bd 5028b6b 7ddb52b f407c48 7bc23ab 948a6f4 a09b432 ed46760 f605354 a09b432 b95f33c a09b432 b95f33c a09b432 b95f33c a09b432 b95f33c a09b432 b95f33c a09b432 b95f33c a09b432 b95f33c a09b432 617431a a09b432 617431a a09b432 617431a a09b432 617431a a09b432 617431a a09b432 617431a a09b432 617431a a09b432 845c4a7 a09b432 845c4a7 a09b432 845c4a7 617431a a09b432 cafe2aa a09b432 617431a a09b432 845c4a7 148f2b3 a09b432 148f2b3 a09b432 148f2b3 a09b432 148f2b3 ad1b760 a09b432 148f2b3 a09b432 9e82ac9 845c4a7 7da0809 eea8c7f 617431a 8439f94 4175faa 617431a 8439f94 617431a 9e82ac9 eea8c7f 69b460d 4175faa 9e82ac9 7349562 eea8c7f 8439f94 9e82ac9 6bf14de 3981c3e 4175faa 9e82ac9 f855987 1c7a0a7 7f78cfc 9e82ac9 35828ac 3981c3e 4175faa 9e82ac9 f855987 35828ac 9e82ac9 a09b432 148f2b3 4175faa 9e82ac9 148f2b3 b95f33c f605354 617431a f605354 617431a f605354 617431a a412583 b40cc33 148f2b3 fd399dc fa21952 ea40888 4453360 bec1a98 f605354 c70f203 4453360 ed46760 b05f917 a412583 148f2b3 ea40888 b05f917 4453360 7da0809 35828ac 0b498b7 617431a b95f33c a09b432 9e82ac9 f605354 a412583 f155629 c70f203 ab1c2b7 ed46760 27f2401 750bbf8 27f2401 1e57e78 fe96564 fa21952 948a6f4 750bbf8 1312508 |
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 |
import os
from crewai import Agent, Crew, Process, Task
from crewai.tools import tool
from crewai_tools import (
CodeInterpreterTool,
SerperDevTool,
WebsiteSearchTool
)
from google import genai
from openai import OpenAI
from openinference.instrumentation.crewai import CrewAIInstrumentor
from phoenix.otel import register
from util import get_final_answer
## LLMs
MANAGER_MODEL = "gpt-4.1"
AGENT_MODEL = "gpt-4.1"
FINAL_ANSWER_MODEL = "gpt-4.5-preview"
IMAGE_MODEL = "gemini-2.0-flash"
AUDIO_MODEL = "gemini-2.0-flash"
VIDEO_MODEL = "gemini-2.0-flash"
YOUTUBE_MODEL = "gemini-2.0-flash"
DOCUMENT_MODEL = "gemini-2.0-flash"
# LLM evaluation
PHOENIX_API_KEY = os.environ["PHOENIX_API_KEY"]
os.environ["PHOENIX_CLIENT_HEADERS"] = f"api_key={PHOENIX_API_KEY}"
os.environ["PHOENIX_COLLECTOR_ENDPOINT"] = "https://app.phoenix.arize.com"
tracer_provider = register(
auto_instrument=True,
project_name="gaia"
)
CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
def run_crew(question, file_path):
# Tools
web_search_tool = SerperDevTool()
web_rag_tool = WebsiteSearchTool()
code_execution_tool = CodeInterpreterTool()
@tool("Image Analysis Tool")
def image_analysis_tool(question: str, file_path: str) -> str:
"""Answer a question about an image file.
Args:
question (str): Question about an image file
file_path (str): The image file path
Returns:
str: Answer to the question about the image file
Raises:
RuntimeError: If processing fails"""
try:
client = genai.Client(api_key="GOOGLE_API_KEY")
file = client.files.upload(file=file_path)
response = client.models.generate_content(
model=IMAGE_MODEL,
contents=[file, question]
)
return response.text
except Exception as e:
raise RuntimeError(f"Processing failed: {str(e)}")
@tool("Audio Analysis Tool")
def audio_analysis_tool(question: str, file_path: str) -> str:
"""Answer a question about an audio file.
Args:
question (str): Question about an audio file
file_path (str): The audio file path
Returns:
str: Answer to the question about the audio file
Raises:
RuntimeError: If processing fails"""
try:
client = genai.Client(api_key="GOOGLE_API_KEY")
file = client.files.upload(file=file_path)
response = client.models.generate_content(
model=AUDIO_MODEL,
contents=[file, question]
)
return response.text
except Exception as e:
raise RuntimeError(f"Processing failed: {str(e)}")
@tool("Video Analysis Tool")
def video_analysis_tool(question: str, file_path: str) -> str:
"""Answer a question about a video file.
Args:
question (str): Question about a video file
file_path (str): The video file path
Returns:
str: Answer to the question about the video file
Raises:
RuntimeError: If processing fails"""
try:
client = genai.Client(api_key="GOOGLE_API_KEY")
file = client.files.upload(file=file_path)
response = client.models.generate_content(
model=VIDEO_MODEL,
contents=[file, question]
)
return response.text
except Exception as e:
raise RuntimeError(f"Processing failed: {str(e)}")
@tool("YouTube Analysis Tool")
def youtube_analysis_tool(question: str, url: str) -> str:
"""Answer a question about a YouTube video.
Args:
question (str): Question about a YouTube video
url (str): The YouTube video URL
Returns:
str: Answer to the question about the YouTube video
Raises:
RuntimeError: If processing fails"""
try:
client = genai.Client(api_key="GOOGLE_API_KEY")
return client.models.generate_content(
model=YOUTUBE_MODEL,
contents=types.Content(
parts=[types.Part(file_data=types.FileData(file_uri=url)),
types.Part(text=question)]
)
)
except Exception as e:
raise RuntimeError(f"Processing failed: {str(e)}")
@tool("Document Analysis Tool")
def document_analysis_tool(question: str, file_path: str) -> str:
"""Answer a question about a document file. Supported document types include:
.txt, .csv, .xml, .rtf, .pdf, .md, .html, .css, .js, .py
Args:
question (str): Question about a document file
file_path (str): The document file path
Returns:
str: Answer to the question about the document file
Raises:
RuntimeError: If processing fails"""
try:
client = genai.Client(api_key="GOOGLE_API_KEY")
file = client.files.upload(file=file_path)
response = client.models.generate_content(
model=DOCUMENT_MODEL,
contents=[file, question]
)
return response.text
except Exception as e:
raise RuntimeError(f"Processing failed: {str(e)}")
# Agents
web_search_agent = Agent(
role="Web Search Agent",
goal="Search the web to help answer question \"{question}\", then scrape the most relevant web page.",
backstory="As an expert web search assistant, you search the web to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[web_search_tool, web_rag_tool],
verbose=False
)
image_analysis_agent = Agent(
role="Image Analysis Agent",
goal="Analyze image file to help answer question \"{question}\"",
backstory="As an expert image analysis assistant, you analyze the image file to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[image_analysis_tool],
verbose=False
)
audio_analysis_agent = Agent(
role="Audio Analysis Agent",
goal="Analyze audio file to help answer question \"{question}\"",
backstory="As an expert audio analysis assistant, you analyze the audio file to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[audio_analysis_tool],
verbose=False
)
video_analysis_agent = Agent(
role="Video Analysis Agent",
goal="Analyze video file to help answer question \"{question}\"",
backstory="As an expert video analysis assistant, you analyze the video file to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[video_analysis_tool],
verbose=False
)
youtube_analysis_agent = Agent(
role="YouTube Analysis Agent",
goal="Analyze YouTube video to help answer question \"{question}\"",
backstory="As an expert YouTube analysis assistant, you analyze the video to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[youtube_analysis_tool],
verbose=False
)
document_analysis_agent = Agent(
role="Document Analysis Agent",
goal="Analyze document of type .txt, .csv, .xml, .rtf, .pdf, .md, .html, .css, .js, .py to help answer question \"{question}\"",
backstory="As an expert document analysis assistant, you analyze the document to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=2,
tools=[document_analysis_tool],
verbose=False
)
code_execution_agent = Agent(
role="Code Execution Agent",
goal="Write and/or execute Python code to help answer question \"{question}\"",
backstory="As an expert code execution assistant, you write and/or execute Python code to help answer the question.",
allow_delegation=False,
llm=AGENT_MODEL,
max_iter=3,
tools=[code_execution_tool],
verbose=False
)
manager_agent = Agent(
role="Manager Agent",
goal="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. "
"If there is no good coworker, delegate to the Python Coding Agent to implement a tool for the task. "
"Question: \"{question}\"",
backstory="As an expert manager assistant, you answer the question.",
allow_delegation=True,
llm=MANAGER_MODEL,
max_iter=5,
verbose=True
)
# Task
manager_task = Task(
agent=manager_agent,
description="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. Question: \"{question}\"",
expected_output="The answer to the question."
)
# Crew
crew = Crew(
agents=[web_search_agent,
image_analysis_agent,
audio_analysis_agent,
video_analysis_agent,
youtube_analysis_agent,
document_analysis_agent,
code_execution_agent],
manager_agent=manager_agent,
tasks=[manager_task],
verbose=True
)
# Process
if file_path:
question = f"{question} File path: {file_path}."
if file_path.endswith(".py"):
with open(f"{file_path}", "r") as file:
question = f"{question} File data:\n{file.read()}"
initial_answer = crew.kickoff(inputs={"question": question})
final_answer = get_final_answer(FINAL_ANSWER_MODEL, question, str(initial_answer))
print(f"Question: {question}")
print(f"Initial answer: {initial_answer}")
print(f"Final answer: {final_answer}")
return final_answer |