Update crew.py
Browse files
crew.py
CHANGED
@@ -47,276 +47,276 @@ tracer_provider = register(
|
|
47 |
|
48 |
#CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
Args:
|
62 |
-
question (str): Question to answer
|
63 |
-
|
64 |
-
Returns:
|
65 |
-
str: Answer to the question
|
66 |
-
|
67 |
-
Raises:
|
68 |
-
RuntimeError: If processing fails"""
|
69 |
-
try:
|
70 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
)
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
@tool("Image Analysis Tool")
|
85 |
-
def image_analysis_tool(question: str, file_path: str) -> str:
|
86 |
-
"""Given a question and image file, analyze the image to answer the question.
|
87 |
-
|
88 |
-
Args:
|
89 |
-
question (str): Question about an image file
|
90 |
-
file_path (str): The image file path
|
91 |
-
|
92 |
-
Returns:
|
93 |
-
str: Answer to the question about the image file
|
94 |
-
|
95 |
-
Raises:
|
96 |
-
RuntimeError: If processing fails"""
|
97 |
-
try:
|
98 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
99 |
-
|
100 |
-
file = client.files.upload(file=file_path)
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
)
|
106 |
-
|
107 |
-
return response.text
|
108 |
-
except Exception as e:
|
109 |
-
raise RuntimeError(f"Processing failed: {str(e)}")
|
110 |
-
|
111 |
-
@tool("Audio Analysis Tool")
|
112 |
-
def audio_analysis_tool(question: str, file_path: str) -> str:
|
113 |
-
"""Given a question and audio file, analyze the audio to answer the question.
|
114 |
-
|
115 |
-
Args:
|
116 |
-
question (str): Question about an audio file
|
117 |
-
file_path (str): The audio file path
|
118 |
-
|
119 |
-
Returns:
|
120 |
-
str: Answer to the question about the audio file
|
121 |
-
|
122 |
-
Raises:
|
123 |
-
RuntimeError: If processing fails"""
|
124 |
-
try:
|
125 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
126 |
|
127 |
-
|
|
|
|
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
@tool("Video Analysis Tool")
|
139 |
-
def video_analysis_tool(question: str, file_path: str) -> str:
|
140 |
-
"""Given a question and video file, analyze the video to answer the question.
|
141 |
-
|
142 |
-
Args:
|
143 |
-
question (str): Question about a video file
|
144 |
-
file_path (str): The video file path
|
145 |
-
|
146 |
-
Returns:
|
147 |
-
str: Answer to the question about the video file
|
148 |
-
|
149 |
-
Raises:
|
150 |
-
RuntimeError: If processing fails"""
|
151 |
-
try:
|
152 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
153 |
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
Returns:
|
174 |
-
str: Answer to the question about the YouTube video
|
175 |
-
|
176 |
-
Raises:
|
177 |
-
RuntimeError: If processing fails"""
|
178 |
-
try:
|
179 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
180 |
-
|
181 |
-
return client.models.generate_content(
|
182 |
-
model=YOUTUBE_ANALYSIS_MODEL,
|
183 |
-
contents=types.Content(
|
184 |
-
parts=[types.Part(file_data=types.FileData(file_uri=url)),
|
185 |
-
types.Part(text=question)]
|
186 |
-
)
|
187 |
-
)
|
188 |
-
except Exception as e:
|
189 |
-
raise RuntimeError(f"Processing failed: {str(e)}")
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
file_path (str): The document file path
|
198 |
-
|
199 |
-
Returns:
|
200 |
-
str: Answer to the question about the document file
|
201 |
-
|
202 |
-
Raises:
|
203 |
-
RuntimeError: If processing fails"""
|
204 |
-
try:
|
205 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
206 |
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
-
|
210 |
-
|
211 |
-
contents = [f"{question}\n{text_data}"]
|
212 |
-
print(f"=> Text data:\n{text_data}")
|
213 |
-
elif is_ext(file_path, ".pptx"):
|
214 |
-
text_data = read_pptx_text(file_path)
|
215 |
-
contents = [f"{question}\n{text_data}"]
|
216 |
-
print(f"=> Text data:\n{text_data}")
|
217 |
-
else:
|
218 |
-
file = client.files.upload(file=file_path)
|
219 |
-
contents = [file, question]
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
def arithmetic_tool(question: str, a: float, b: float) -> float:
|
232 |
-
"""Given a question and two numbers, perform the calculation to answer the question.
|
233 |
-
|
234 |
-
Args:
|
235 |
-
question (str): Question to answer
|
236 |
-
a (float): First number
|
237 |
-
b (float): Second number
|
238 |
-
|
239 |
-
Returns:
|
240 |
-
float: Result number
|
241 |
-
|
242 |
-
Raises:
|
243 |
-
RuntimeError: If processing fails"""
|
244 |
-
try:
|
245 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
246 |
-
|
247 |
-
response = client.models.generate_content(
|
248 |
-
model=ARITHMETIC_MODEL,
|
249 |
-
contents=question,
|
250 |
-
config=types.GenerateContentConfig(
|
251 |
-
tools=[add, subtract, multiply, divide, modulus]
|
252 |
-
)
|
253 |
)
|
|
|
|
|
|
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
Raises:
|
271 |
-
RuntimeError: If processing fails"""
|
272 |
-
try:
|
273 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
file = client.files.upload(file=file_path)
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
Returns:
|
300 |
-
str: Answer to the question
|
301 |
-
|
302 |
-
Raises:
|
303 |
-
RuntimeError: If processing fails"""
|
304 |
-
try:
|
305 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
306 |
-
|
307 |
-
response = client.models.generate_content(
|
308 |
-
model=CODE_GENERATION_MODEL,
|
309 |
-
contents=[f"{question}\n{json_data}"],
|
310 |
-
config=types.GenerateContentConfig(
|
311 |
-
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
|
312 |
-
),
|
313 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
|
321 |
# Agents
|
322 |
|
|
|
47 |
|
48 |
#CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
|
49 |
|
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.
|
55 |
+
|
56 |
+
Args:
|
57 |
+
question (str): Question to answer
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
str: Answer to the question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
Raises:
|
63 |
+
RuntimeError: If processing fails"""
|
64 |
+
try:
|
65 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
66 |
+
|
67 |
+
response = client.models.generate_content(
|
68 |
+
model=WEB_SEARCH_MODEL,
|
69 |
+
contents=question,
|
70 |
+
config=types.GenerateContentConfig(
|
71 |
+
tools=[types.Tool(google_search=types.GoogleSearchRetrieval())]
|
72 |
)
|
73 |
+
)
|
74 |
|
75 |
+
return response.text
|
76 |
+
except Exception as e:
|
77 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
@tool("Image Analysis Tool")
|
80 |
+
def image_analysis_tool(question: str, file_path: str) -> str:
|
81 |
+
"""Given a question and image file, analyze the image to answer the question.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
Args:
|
84 |
+
question (str): Question about an image file
|
85 |
+
file_path (str): The image file path
|
86 |
|
87 |
+
Returns:
|
88 |
+
str: Answer to the question about the image file
|
89 |
+
|
90 |
+
Raises:
|
91 |
+
RuntimeError: If processing fails"""
|
92 |
+
try:
|
93 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
94 |
+
|
95 |
+
file = client.files.upload(file=file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
response = client.models.generate_content(
|
98 |
+
model=IMAGE_ANALYSIS_MODEL,
|
99 |
+
contents=[file, question]
|
100 |
+
)
|
101 |
+
|
102 |
+
return response.text
|
103 |
+
except Exception as e:
|
104 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
105 |
+
|
106 |
+
@tool("Audio Analysis Tool")
|
107 |
+
def audio_analysis_tool(question: str, file_path: str) -> str:
|
108 |
+
"""Given a question and audio file, analyze the audio to answer the question.
|
109 |
+
|
110 |
+
Args:
|
111 |
+
question (str): Question about an audio file
|
112 |
+
file_path (str): The audio file path
|
113 |
|
114 |
+
Returns:
|
115 |
+
str: Answer to the question about the audio file
|
116 |
+
|
117 |
+
Raises:
|
118 |
+
RuntimeError: If processing fails"""
|
119 |
+
try:
|
120 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
121 |
|
122 |
+
file = client.files.upload(file=file_path)
|
123 |
+
|
124 |
+
response = client.models.generate_content(
|
125 |
+
model=AUDIO_ANALYSIS_MODEL,
|
126 |
+
contents=[file, question]
|
127 |
+
)
|
128 |
+
|
129 |
+
return response.text
|
130 |
+
except Exception as e:
|
131 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
132 |
+
|
133 |
+
@tool("Video Analysis Tool")
|
134 |
+
def video_analysis_tool(question: str, file_path: str) -> str:
|
135 |
+
"""Given a question and video file, analyze the video to answer the question.
|
136 |
+
|
137 |
+
Args:
|
138 |
+
question (str): Question about a video file
|
139 |
+
file_path (str): The video file path
|
140 |
|
141 |
+
Returns:
|
142 |
+
str: Answer to the question about the video file
|
143 |
+
|
144 |
+
Raises:
|
145 |
+
RuntimeError: If processing fails"""
|
146 |
+
try:
|
147 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
+
file = client.files.upload(file=file_path)
|
150 |
+
|
151 |
+
response = client.models.generate_content(
|
152 |
+
model=VIDEO_ANALYSIS_MODEL,
|
153 |
+
contents=[file, question]
|
154 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
+
return response.text
|
157 |
+
except Exception as e:
|
158 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
159 |
+
|
160 |
+
@tool("YouTube Analysis Tool")
|
161 |
+
def youtube_analysis_tool(question: str, url: str) -> str:
|
162 |
+
"""Given a question and YouTube URL, analyze the video to answer the question.
|
163 |
+
|
164 |
+
Args:
|
165 |
+
question (str): Question about a YouTube video
|
166 |
+
url (str): The YouTube URL
|
167 |
|
168 |
+
Returns:
|
169 |
+
str: Answer to the question about the YouTube video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
+
Raises:
|
172 |
+
RuntimeError: If processing fails"""
|
173 |
+
try:
|
174 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
175 |
+
|
176 |
+
return client.models.generate_content(
|
177 |
+
model=YOUTUBE_ANALYSIS_MODEL,
|
178 |
+
contents=types.Content(
|
179 |
+
parts=[types.Part(file_data=types.FileData(file_uri=url)),
|
180 |
+
types.Part(text=question)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
)
|
182 |
+
)
|
183 |
+
except Exception as e:
|
184 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
185 |
|
186 |
+
@tool("Document Analysis Tool")
|
187 |
+
def document_analysis_tool(question: str, file_path: str) -> str:
|
188 |
+
"""Given a question and document file, analyze the document to answer the question.
|
189 |
|
190 |
+
Args:
|
191 |
+
question (str): Question about a document file
|
192 |
+
file_path (str): The document file path
|
193 |
+
|
194 |
+
Returns:
|
195 |
+
str: Answer to the question about the document file
|
196 |
+
|
197 |
+
Raises:
|
198 |
+
RuntimeError: If processing fails"""
|
199 |
+
try:
|
200 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
|
|
|
|
|
|
|
|
201 |
|
202 |
+
contents = []
|
203 |
+
|
204 |
+
if is_ext(file_path, ".docx"):
|
205 |
+
text_data = read_docx_text(file_path)
|
206 |
+
contents = [f"{question}\n{text_data}"]
|
207 |
+
print(f"=> Text data:\n{text_data}")
|
208 |
+
elif is_ext(file_path, ".pptx"):
|
209 |
+
text_data = read_pptx_text(file_path)
|
210 |
+
contents = [f"{question}\n{text_data}"]
|
211 |
+
print(f"=> Text data:\n{text_data}")
|
212 |
+
else:
|
213 |
file = client.files.upload(file=file_path)
|
214 |
+
contents = [file, question]
|
215 |
+
|
216 |
+
response = client.models.generate_content(
|
217 |
+
model=DOCUMENT_ANALYSIS_MODEL,
|
218 |
+
contents=contents
|
219 |
+
)
|
220 |
+
|
221 |
+
return response.text
|
222 |
+
except Exception as e:
|
223 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
224 |
+
|
225 |
+
@tool("Arithmetic Tool")
|
226 |
+
def arithmetic_tool(question: str, a: float, b: float) -> float:
|
227 |
+
"""Given a question and two numbers, perform the calculation to answer the question.
|
228 |
+
|
229 |
+
Args:
|
230 |
+
question (str): Question to answer
|
231 |
+
a (float): First number
|
232 |
+
b (float): Second number
|
233 |
|
234 |
+
Returns:
|
235 |
+
float: Result number
|
236 |
+
|
237 |
+
Raises:
|
238 |
+
RuntimeError: If processing fails"""
|
239 |
+
try:
|
240 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
241 |
+
|
242 |
+
response = client.models.generate_content(
|
243 |
+
model=ARITHMETIC_MODEL,
|
244 |
+
contents=question,
|
245 |
+
config=types.GenerateContentConfig(
|
246 |
+
tools=[add, subtract, multiply, divide, modulus]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
)
|
248 |
+
)
|
249 |
+
|
250 |
+
return response.text
|
251 |
+
except Exception as e:
|
252 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
253 |
+
|
254 |
+
@tool("Code Execution Tool")
|
255 |
+
def code_execution_tool(question: str, file_path: str) -> str:
|
256 |
+
"""Given a question and Python file, execute the file to answer the question.
|
257 |
+
|
258 |
+
Args:
|
259 |
+
question (str): Question to answer
|
260 |
+
file_path (str): The Python file path
|
261 |
+
|
262 |
+
Returns:
|
263 |
+
str: Answer to the question
|
264 |
|
265 |
+
Raises:
|
266 |
+
RuntimeError: If processing fails"""
|
267 |
+
try:
|
268 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
269 |
+
|
270 |
+
file = client.files.upload(file=file_path)
|
271 |
+
|
272 |
+
response = client.models.generate_content(
|
273 |
+
model=CODE_EXECUTION_MODEL,
|
274 |
+
contents=[file, question],
|
275 |
+
config=types.GenerateContentConfig(
|
276 |
+
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
|
277 |
+
),
|
278 |
+
)
|
279 |
+
|
280 |
+
for part in response.candidates[0].content.parts:
|
281 |
+
if part.code_execution_result is not None:
|
282 |
+
return part.code_execution_result.output
|
283 |
+
except Exception as e:
|
284 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
285 |
+
|
286 |
+
@tool("Code Generation Tool")
|
287 |
+
def code_generation_tool(question: str, json_data: str) -> str:
|
288 |
+
"""Given a question and JSON data, generate and execute code to answer the question.
|
289 |
+
|
290 |
+
Args:
|
291 |
+
question (str): Question to answer
|
292 |
+
file_path (str): The JSON data
|
293 |
+
|
294 |
+
Returns:
|
295 |
+
str: Answer to the question
|
296 |
+
|
297 |
+
Raises:
|
298 |
+
RuntimeError: If processing fails"""
|
299 |
+
try:
|
300 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
301 |
+
|
302 |
+
response = client.models.generate_content(
|
303 |
+
model=CODE_GENERATION_MODEL,
|
304 |
+
contents=[f"{question}\n{json_data}"],
|
305 |
+
config=types.GenerateContentConfig(
|
306 |
+
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
|
307 |
+
),
|
308 |
+
)
|
309 |
+
|
310 |
+
for part in response.candidates[0].content.parts:
|
311 |
+
if part.code_execution_result is not None:
|
312 |
+
return part.code_execution_result.output
|
313 |
+
except Exception as e:
|
314 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
315 |
+
|
316 |
+
@CrewBase
|
317 |
+
class GAIACrew():
|
318 |
+
agents: List[BaseAgent]
|
319 |
+
tasks: List[Task]
|
320 |
|
321 |
# Agents
|
322 |
|