bstraehle commited on
Commit
6a7364f
·
verified ·
1 Parent(s): 5a8d469

Update crew.py

Browse files
Files changed (1) hide show
  1. crew.py +6 -20
crew.py CHANGED
@@ -2,13 +2,11 @@ import os
2
  from crewai import Agent, Crew, Process, Task
3
  from crewai.tools import tool
4
  from crewai_tools import (
5
- CodeInterpreterTool,
6
  SerperDevTool,
7
  WebsiteSearchTool
8
  )
9
  from google import genai
10
  from google.genai import types
11
- #from openai import OpenAI
12
  from openinference.instrumentation.crewai import CrewAIInstrumentor
13
  from phoenix.otel import register
14
  from util import get_final_answer
@@ -46,7 +44,6 @@ def run_crew(question, file_path):
46
 
47
  web_search_tool = SerperDevTool()
48
  web_rag_tool = WebsiteSearchTool()
49
- #code_execution_tool = CodeInterpreterTool()
50
 
51
  @tool("Image Analysis Tool")
52
  def image_analysis_tool(question: str, file_path: str) -> str:
@@ -184,44 +181,33 @@ def run_crew(question, file_path):
184
  raise RuntimeError(f"Processing failed: {str(e)}")
185
 
186
  @tool("Code Execution Tool")
187
- def code_execution_tool(question: str, file_path: str) -> str:
188
- """Answer a question about a Python code file.
189
 
190
  Args:
191
- question (str): Question about a Pyton code file
192
  file_path (str): The Python code file path
193
 
194
  Returns:
195
- str: Answer to the question about the Python code file
196
 
197
  Raises:
198
  RuntimeError: If processing fails"""
199
  try:
200
  client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
201
 
202
- ###
203
  file = client.files.upload(file=file_path)
204
 
205
  response = client.models.generate_content(
206
  model=CODE_MODEL,
207
- contents=[file, question],
208
  config=types.GenerateContentConfig(
209
  tools=[types.Tool(code_execution=types.ToolCodeExecution)]
210
  ),
211
  )
212
 
213
  for part in response.candidates[0].content.parts:
214
- if part.text is not None:
215
- print("###")
216
- print(part.text)
217
- if part.executable_code is not None:
218
- print("###")
219
- print(part.executable_code.code)
220
  if part.code_execution_result is not None:
221
- print("###")
222
- print(part.code_execution_result.output)
223
  return part.code_execution_result.output
224
- ###
225
  except Exception as e:
226
  raise RuntimeError(f"Processing failed: {str(e)}")
227
 
@@ -295,8 +281,8 @@ def run_crew(question, file_path):
295
 
296
  code_execution_agent = Agent(
297
  role="Code Execution Agent",
298
- goal="Write and/or execute Python code to help answer question \"{question}\"",
299
- backstory="As an expert code execution assistant, you write and/or execute Python code to help answer the question.",
300
  allow_delegation=False,
301
  llm=AGENT_MODEL,
302
  max_iter=3,
 
2
  from crewai import Agent, Crew, Process, Task
3
  from crewai.tools import tool
4
  from crewai_tools import (
 
5
  SerperDevTool,
6
  WebsiteSearchTool
7
  )
8
  from google import genai
9
  from google.genai import types
 
10
  from openinference.instrumentation.crewai import CrewAIInstrumentor
11
  from phoenix.otel import register
12
  from util import get_final_answer
 
44
 
45
  web_search_tool = SerperDevTool()
46
  web_rag_tool = WebsiteSearchTool()
 
47
 
48
  @tool("Image Analysis Tool")
49
  def image_analysis_tool(question: str, file_path: str) -> str:
 
181
  raise RuntimeError(f"Processing failed: {str(e)}")
182
 
183
  @tool("Code Execution Tool")
184
+ def code_execution_tool(file_path: str) -> str:
185
+ """Execute a Python code file and return the result.
186
 
187
  Args:
 
188
  file_path (str): The Python code file path
189
 
190
  Returns:
191
+ str: The code execution result
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=[file],
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
 
 
281
 
282
  code_execution_agent = Agent(
283
  role="Code Execution Agent",
284
+ goal="Execute Python code file to help answer question \"{question}\"",
285
+ backstory="As an expert Python code execution assistant, you execute the code file to help answer the question.",
286
  allow_delegation=False,
287
  llm=AGENT_MODEL,
288
  max_iter=3,