bstraehle commited on
Commit
e080246
·
verified ·
1 Parent(s): 0ff052c

Update crew.py

Browse files
Files changed (1) hide show
  1. crew.py +262 -262
crew.py CHANGED
@@ -48,268 +48,6 @@ tracer_provider = register(
48
 
49
  #CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
50
 
51
- @tool("Web Search Tool")
52
- def web_search_tool(question: str) -> str:
53
- """Given a question only, search the web to answer the question.
54
-
55
- Args:
56
- question (str): Question to answer
57
-
58
- Returns:
59
- str: Answer to the question
60
-
61
- Raises:
62
- RuntimeError: If processing fails"""
63
- try:
64
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
65
-
66
- response = client.models.generate_content(
67
- model=WEB_SEARCH_MODEL,
68
- contents=question,
69
- config=types.GenerateContentConfig(
70
- tools=[types.Tool(google_search=types.GoogleSearchRetrieval())]
71
- )
72
- )
73
-
74
- return response.text
75
- except Exception as e:
76
- raise RuntimeError(f"Processing failed: {str(e)}")
77
-
78
- @tool("Image Analysis Tool")
79
- def image_analysis_tool(question: str, file_path: str) -> str:
80
- """Given a question and image file, analyze the image to answer the question.
81
-
82
- Args:
83
- question (str): Question about an image file
84
- file_path (str): The image file path
85
-
86
- Returns:
87
- str: Answer to the question about the image file
88
-
89
- Raises:
90
- RuntimeError: If processing fails"""
91
- try:
92
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
93
-
94
- file = client.files.upload(file=file_path)
95
-
96
- response = client.models.generate_content(
97
- model=IMAGE_ANALYSIS_MODEL,
98
- contents=[file, question]
99
- )
100
-
101
- return response.text
102
- except Exception as e:
103
- raise RuntimeError(f"Processing failed: {str(e)}")
104
-
105
- @tool("Audio Analysis Tool")
106
- def audio_analysis_tool(question: str, file_path: str) -> str:
107
- """Given a question and audio file, analyze the audio to answer the question.
108
-
109
- Args:
110
- question (str): Question about an audio file
111
- file_path (str): The audio file path
112
-
113
- Returns:
114
- str: Answer to the question about the audio file
115
-
116
- Raises:
117
- RuntimeError: If processing fails"""
118
- try:
119
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
120
-
121
- file = client.files.upload(file=file_path)
122
-
123
- response = client.models.generate_content(
124
- model=AUDIO_ANALYSIS_MODEL,
125
- contents=[file, question]
126
- )
127
-
128
- return response.text
129
- except Exception as e:
130
- raise RuntimeError(f"Processing failed: {str(e)}")
131
-
132
- @tool("Video Analysis Tool")
133
- def video_analysis_tool(question: str, file_path: str) -> str:
134
- """Given a question and video file, analyze the video to answer the question.
135
-
136
- Args:
137
- question (str): Question about a video file
138
- file_path (str): The video file path
139
-
140
- Returns:
141
- str: Answer to the question about the video file
142
-
143
- Raises:
144
- RuntimeError: If processing fails"""
145
- try:
146
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
147
-
148
- file = client.files.upload(file=file_path)
149
-
150
- response = client.models.generate_content(
151
- model=VIDEO_ANALYSIS_MODEL,
152
- contents=[file, question]
153
- )
154
-
155
- return response.text
156
- except Exception as e:
157
- raise RuntimeError(f"Processing failed: {str(e)}")
158
-
159
- @tool("YouTube Analysis Tool")
160
- def youtube_analysis_tool(question: str, url: str) -> str:
161
- """Given a question and YouTube URL, analyze the video to answer the question.
162
-
163
- Args:
164
- question (str): Question about a YouTube video
165
- url (str): The YouTube URL
166
-
167
- Returns:
168
- str: Answer to the question about the YouTube video
169
-
170
- Raises:
171
- RuntimeError: If processing fails"""
172
- try:
173
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
174
-
175
- return client.models.generate_content(
176
- model=YOUTUBE_ANALYSIS_MODEL,
177
- contents=types.Content(
178
- parts=[types.Part(file_data=types.FileData(file_uri=url)),
179
- types.Part(text=question)]
180
- )
181
- )
182
- except Exception as e:
183
- raise RuntimeError(f"Processing failed: {str(e)}")
184
-
185
- @tool("Document Analysis Tool")
186
- def document_analysis_tool(question: str, file_path: str) -> str:
187
- """Given a question and document file, analyze the document to answer the question.
188
-
189
- Args:
190
- question (str): Question about a document file
191
- file_path (str): The document file path
192
-
193
- Returns:
194
- str: Answer to the question about the document file
195
-
196
- Raises:
197
- RuntimeError: If processing fails"""
198
- try:
199
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
200
-
201
- contents = []
202
-
203
- if is_ext(file_path, ".docx"):
204
- text_data = read_docx_text(file_path)
205
- contents = [f"{question}\n{text_data}"]
206
- print(f"=> Text data:\n{text_data}")
207
- elif is_ext(file_path, ".pptx"):
208
- text_data = read_pptx_text(file_path)
209
- contents = [f"{question}\n{text_data}"]
210
- print(f"=> Text data:\n{text_data}")
211
- else:
212
- file = client.files.upload(file=file_path)
213
- contents = [file, question]
214
-
215
- response = client.models.generate_content(
216
- model=DOCUMENT_ANALYSIS_MODEL,
217
- contents=contents
218
- )
219
-
220
- return response.text
221
- except Exception as e:
222
- raise RuntimeError(f"Processing failed: {str(e)}")
223
-
224
- @tool("Arithmetic Tool")
225
- def arithmetic_tool(question: str, a: float, b: float) -> float:
226
- """Given a question and two numbers, perform the calculation to answer the question.
227
-
228
- Args:
229
- question (str): Question to answer
230
- a (float): First number
231
- b (float): Second number
232
-
233
- Returns:
234
- float: Result number
235
-
236
- Raises:
237
- RuntimeError: If processing fails"""
238
- try:
239
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
240
-
241
- response = client.models.generate_content(
242
- model=ARITHMETIC_MODEL,
243
- contents=question,
244
- config=types.GenerateContentConfig(
245
- tools=[add, subtract, multiply, divide, modulus]
246
- )
247
- )
248
-
249
- return response.text
250
- except Exception as e:
251
- raise RuntimeError(f"Processing failed: {str(e)}")
252
-
253
- @tool("Code Execution Tool")
254
- def code_execution_tool(question: str, file_path: str) -> str:
255
- """Given a question and Python file, execute the file to answer the question.
256
-
257
- Args:
258
- question (str): Question to answer
259
- file_path (str): The Python file path
260
-
261
- Returns:
262
- str: Answer to the question
263
-
264
- Raises:
265
- RuntimeError: If processing fails"""
266
- try:
267
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
268
-
269
- file = client.files.upload(file=file_path)
270
-
271
- response = client.models.generate_content(
272
- model=CODE_EXECUTION_MODEL,
273
- contents=[file, question],
274
- config=types.GenerateContentConfig(
275
- tools=[types.Tool(code_execution=types.ToolCodeExecution)]
276
- ),
277
- )
278
-
279
- for part in response.candidates[0].content.parts:
280
- if part.code_execution_result is not None:
281
- return part.code_execution_result.output
282
- except Exception as e:
283
- raise RuntimeError(f"Processing failed: {str(e)}")
284
-
285
- @tool("Code Generation Tool")
286
- def code_generation_tool(question: str, json_data: str) -> str:
287
- """Given a question and JSON data, generate and execute code to answer the question.
288
- Args:
289
- question (str): Question to answer
290
- file_path (str): The JSON data
291
- Returns:
292
- str: Answer to the question
293
-
294
- Raises:
295
- RuntimeError: If processing fails"""
296
- try:
297
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
298
-
299
- response = client.models.generate_content(
300
- model=CODE_GENERATION_MODEL,
301
- contents=[f"{question}\n{json_data}"],
302
- config=types.GenerateContentConfig(
303
- tools=[types.Tool(code_execution=types.ToolCodeExecution)]
304
- ),
305
- )
306
-
307
- for part in response.candidates[0].content.parts:
308
- if part.code_execution_result is not None:
309
- return part.code_execution_result.output
310
- except Exception as e:
311
- raise RuntimeError(f"Processing failed: {str(e)}")
312
-
313
  @CrewBase
314
  class GAIACrew():
315
  agents: List[BaseAgent]
@@ -438,6 +176,268 @@ class GAIACrew():
438
  verbose=True
439
  )
440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
  def run_crew(question, file_path):
442
  final_question = question
443
 
 
48
 
49
  #CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  @CrewBase
52
  class GAIACrew():
53
  agents: List[BaseAgent]
 
176
  verbose=True
177
  )
178
 
179
+ @tool("Web Search Tool")
180
+ def web_search_tool(question: str) -> str:
181
+ """Given a question only, search the web to answer the question.
182
+
183
+ Args:
184
+ question (str): Question to answer
185
+
186
+ Returns:
187
+ str: Answer to the question
188
+
189
+ Raises:
190
+ RuntimeError: If processing fails"""
191
+ try:
192
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
193
+
194
+ response = client.models.generate_content(
195
+ model=WEB_SEARCH_MODEL,
196
+ contents=question,
197
+ config=types.GenerateContentConfig(
198
+ tools=[types.Tool(google_search=types.GoogleSearchRetrieval())]
199
+ )
200
+ )
201
+
202
+ return response.text
203
+ except Exception as e:
204
+ raise RuntimeError(f"Processing failed: {str(e)}")
205
+
206
+ @tool("Image Analysis Tool")
207
+ def image_analysis_tool(question: str, file_path: str) -> str:
208
+ """Given a question and image file, analyze the image to answer the question.
209
+
210
+ Args:
211
+ question (str): Question about an image file
212
+ file_path (str): The image file path
213
+
214
+ Returns:
215
+ str: Answer to the question about the image file
216
+
217
+ Raises:
218
+ RuntimeError: If processing fails"""
219
+ try:
220
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
221
+
222
+ file = client.files.upload(file=file_path)
223
+
224
+ response = client.models.generate_content(
225
+ model=IMAGE_ANALYSIS_MODEL,
226
+ contents=[file, question]
227
+ )
228
+
229
+ return response.text
230
+ except Exception as e:
231
+ raise RuntimeError(f"Processing failed: {str(e)}")
232
+
233
+ @tool("Audio Analysis Tool")
234
+ def audio_analysis_tool(question: str, file_path: str) -> str:
235
+ """Given a question and audio file, analyze the audio to answer the question.
236
+
237
+ Args:
238
+ question (str): Question about an audio file
239
+ file_path (str): The audio file path
240
+
241
+ Returns:
242
+ str: Answer to the question about the audio file
243
+
244
+ Raises:
245
+ RuntimeError: If processing fails"""
246
+ try:
247
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
248
+
249
+ file = client.files.upload(file=file_path)
250
+
251
+ response = client.models.generate_content(
252
+ model=AUDIO_ANALYSIS_MODEL,
253
+ contents=[file, question]
254
+ )
255
+
256
+ return response.text
257
+ except Exception as e:
258
+ raise RuntimeError(f"Processing failed: {str(e)}")
259
+
260
+ @tool("Video Analysis Tool")
261
+ def video_analysis_tool(question: str, file_path: str) -> str:
262
+ """Given a question and video file, analyze the video to answer the question.
263
+
264
+ Args:
265
+ question (str): Question about a video file
266
+ file_path (str): The video file path
267
+
268
+ Returns:
269
+ str: Answer to the question about the video file
270
+
271
+ Raises:
272
+ RuntimeError: If processing fails"""
273
+ try:
274
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
275
+
276
+ file = client.files.upload(file=file_path)
277
+
278
+ response = client.models.generate_content(
279
+ model=VIDEO_ANALYSIS_MODEL,
280
+ contents=[file, question]
281
+ )
282
+
283
+ return response.text
284
+ except Exception as e:
285
+ raise RuntimeError(f"Processing failed: {str(e)}")
286
+
287
+ @tool("YouTube Analysis Tool")
288
+ def youtube_analysis_tool(question: str, url: str) -> str:
289
+ """Given a question and YouTube URL, analyze the video to answer the question.
290
+
291
+ Args:
292
+ question (str): Question about a YouTube video
293
+ url (str): The YouTube URL
294
+
295
+ Returns:
296
+ str: Answer to the question about the YouTube video
297
+
298
+ Raises:
299
+ RuntimeError: If processing fails"""
300
+ try:
301
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
302
+
303
+ return client.models.generate_content(
304
+ model=YOUTUBE_ANALYSIS_MODEL,
305
+ contents=types.Content(
306
+ parts=[types.Part(file_data=types.FileData(file_uri=url)),
307
+ types.Part(text=question)]
308
+ )
309
+ )
310
+ except Exception as e:
311
+ raise RuntimeError(f"Processing failed: {str(e)}")
312
+
313
+ @tool("Document Analysis Tool")
314
+ def document_analysis_tool(question: str, file_path: str) -> str:
315
+ """Given a question and document file, analyze the document to answer the question.
316
+
317
+ Args:
318
+ question (str): Question about a document file
319
+ file_path (str): The document file path
320
+
321
+ Returns:
322
+ str: Answer to the question about the document file
323
+
324
+ Raises:
325
+ RuntimeError: If processing fails"""
326
+ try:
327
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
328
+
329
+ contents = []
330
+
331
+ if is_ext(file_path, ".docx"):
332
+ text_data = read_docx_text(file_path)
333
+ contents = [f"{question}\n{text_data}"]
334
+ print(f"=> Text data:\n{text_data}")
335
+ elif is_ext(file_path, ".pptx"):
336
+ text_data = read_pptx_text(file_path)
337
+ contents = [f"{question}\n{text_data}"]
338
+ print(f"=> Text data:\n{text_data}")
339
+ else:
340
+ file = client.files.upload(file=file_path)
341
+ contents = [file, question]
342
+
343
+ response = client.models.generate_content(
344
+ model=DOCUMENT_ANALYSIS_MODEL,
345
+ contents=contents
346
+ )
347
+
348
+ return response.text
349
+ except Exception as e:
350
+ raise RuntimeError(f"Processing failed: {str(e)}")
351
+
352
+ @tool("Arithmetic Tool")
353
+ def arithmetic_tool(question: str, a: float, b: float) -> float:
354
+ """Given a question and two numbers, perform the calculation to answer the question.
355
+
356
+ Args:
357
+ question (str): Question to answer
358
+ a (float): First number
359
+ b (float): Second number
360
+
361
+ Returns:
362
+ float: Result number
363
+
364
+ Raises:
365
+ RuntimeError: If processing fails"""
366
+ try:
367
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
368
+
369
+ response = client.models.generate_content(
370
+ model=ARITHMETIC_MODEL,
371
+ contents=question,
372
+ config=types.GenerateContentConfig(
373
+ tools=[add, subtract, multiply, divide, modulus]
374
+ )
375
+ )
376
+
377
+ return response.text
378
+ except Exception as e:
379
+ raise RuntimeError(f"Processing failed: {str(e)}")
380
+
381
+ @tool("Code Execution Tool")
382
+ def code_execution_tool(question: str, file_path: str) -> str:
383
+ """Given a question and Python file, execute the file to answer the question.
384
+
385
+ Args:
386
+ question (str): Question to answer
387
+ file_path (str): The Python file path
388
+
389
+ Returns:
390
+ str: Answer to the question
391
+
392
+ Raises:
393
+ RuntimeError: If processing fails"""
394
+ try:
395
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
396
+
397
+ file = client.files.upload(file=file_path)
398
+
399
+ response = client.models.generate_content(
400
+ model=CODE_EXECUTION_MODEL,
401
+ contents=[file, question],
402
+ config=types.GenerateContentConfig(
403
+ tools=[types.Tool(code_execution=types.ToolCodeExecution)]
404
+ ),
405
+ )
406
+
407
+ for part in response.candidates[0].content.parts:
408
+ if part.code_execution_result is not None:
409
+ return part.code_execution_result.output
410
+ except Exception as e:
411
+ raise RuntimeError(f"Processing failed: {str(e)}")
412
+
413
+ @tool("Code Generation Tool")
414
+ def code_generation_tool(question: str, json_data: str) -> str:
415
+ """Given a question and JSON data, generate and execute code to answer the question.
416
+ Args:
417
+ question (str): Question to answer
418
+ file_path (str): The JSON data
419
+ Returns:
420
+ str: Answer to the question
421
+
422
+ Raises:
423
+ RuntimeError: If processing fails"""
424
+ try:
425
+ client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
426
+
427
+ response = client.models.generate_content(
428
+ model=CODE_GENERATION_MODEL,
429
+ contents=[f"{question}\n{json_data}"],
430
+ config=types.GenerateContentConfig(
431
+ tools=[types.Tool(code_execution=types.ToolCodeExecution)]
432
+ ),
433
+ )
434
+
435
+ for part in response.candidates[0].content.parts:
436
+ if part.code_execution_result is not None:
437
+ return part.code_execution_result.output
438
+ except Exception as e:
439
+ raise RuntimeError(f"Processing failed: {str(e)}")
440
+
441
  def run_crew(question, file_path):
442
  final_question = question
443