Update crew.py
Browse files
crew.py
CHANGED
@@ -180,16 +180,47 @@ def run_crew(question, file_path):
|
|
180 |
except Exception as e:
|
181 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
@tool("Code Execution Tool")
|
184 |
def code_execution_tool(question: str, file_path: str) -> str:
|
185 |
-
"""Execute a Python code file
|
186 |
|
187 |
Args:
|
188 |
-
question (str): Question
|
189 |
file_path (str): The Python code file path
|
190 |
|
191 |
Returns:
|
192 |
-
str: Answer to the question
|
193 |
|
194 |
Raises:
|
195 |
RuntimeError: If processing fails"""
|
@@ -279,6 +310,17 @@ def run_crew(question, file_path):
|
|
279 |
tools=[document_analysis_tool],
|
280 |
verbose=False
|
281 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
283 |
code_execution_agent = Agent(
|
284 |
role="Code Execution Agent",
|
@@ -319,7 +361,8 @@ def run_crew(question, file_path):
|
|
319 |
audio_analysis_agent,
|
320 |
video_analysis_agent,
|
321 |
youtube_analysis_agent,
|
322 |
-
|
|
|
323 |
code_execution_agent],
|
324 |
manager_agent=manager_agent,
|
325 |
tasks=[manager_task],
|
|
|
180 |
except Exception as e:
|
181 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
182 |
|
183 |
+
@tool("Code Generation Tool")
|
184 |
+
def code_generation_tool(question: str) -> str:
|
185 |
+
"""Generate and execute Python code to answer a question.
|
186 |
+
|
187 |
+
Args:
|
188 |
+
question (str): Question to answer
|
189 |
+
|
190 |
+
Returns:
|
191 |
+
str: Answer to the question
|
192 |
+
|
193 |
+
Raises:
|
194 |
+
RuntimeError: If processing fails"""
|
195 |
+
try:
|
196 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
197 |
+
|
198 |
+
file = client.files.upload(file=file_path)
|
199 |
+
|
200 |
+
response = client.models.generate_content(
|
201 |
+
model=CODE_MODEL,
|
202 |
+
contents=[question],
|
203 |
+
config=types.GenerateContentConfig(
|
204 |
+
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
|
205 |
+
),
|
206 |
+
)
|
207 |
+
|
208 |
+
for part in response.candidates[0].content.parts:
|
209 |
+
if part.code_execution_result is not None:
|
210 |
+
return part.code_execution_result.output
|
211 |
+
except Exception as e:
|
212 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
213 |
+
|
214 |
@tool("Code Execution Tool")
|
215 |
def code_execution_tool(question: str, file_path: str) -> str:
|
216 |
+
"""Execute a Python code file to answer a question.
|
217 |
|
218 |
Args:
|
219 |
+
question (str): Question to answer
|
220 |
file_path (str): The Python code file path
|
221 |
|
222 |
Returns:
|
223 |
+
str: Answer to the question
|
224 |
|
225 |
Raises:
|
226 |
RuntimeError: If processing fails"""
|
|
|
310 |
tools=[document_analysis_tool],
|
311 |
verbose=False
|
312 |
)
|
313 |
+
|
314 |
+
code_generation_agent = Agent(
|
315 |
+
role="Code Generation Agent",
|
316 |
+
goal="Generate Python code and execute it to help answer question \"{question}\"",
|
317 |
+
backstory="As an expert Python code generation assistant, you generate and execute code to help answer the question.",
|
318 |
+
allow_delegation=False,
|
319 |
+
llm=AGENT_MODEL,
|
320 |
+
max_iter=3,
|
321 |
+
tools=[code_execution_tool],
|
322 |
+
verbose=False
|
323 |
+
)
|
324 |
|
325 |
code_execution_agent = Agent(
|
326 |
role="Code Execution Agent",
|
|
|
361 |
audio_analysis_agent,
|
362 |
video_analysis_agent,
|
363 |
youtube_analysis_agent,
|
364 |
+
document_analysis_agent,
|
365 |
+
code_generation_agent,
|
366 |
code_execution_agent],
|
367 |
manager_agent=manager_agent,
|
368 |
tasks=[manager_task],
|