philincloud commited on
Commit
d222e19
·
verified ·
1 Parent(s): 42b8aa6

Update langgraph_agent.py

Browse files
Files changed (1) hide show
  1. langgraph_agent.py +18 -10
langgraph_agent.py CHANGED
@@ -92,34 +92,42 @@ if HF_API_TOKEN:
92
  else:
93
  print("WARNING: HF_API_TOKEN not set. If any other HF tools are used, they might not function.")
94
 
95
- @tool
96
  def read_file_content(file_path: str) -> Dict[str, str]:
97
- """Reads the content of a file and returns its primary information. For text/code/excel, returns content. For media, returns a prompt to use specific tools."""
98
  try:
99
  _, file_extension = os.path.splitext(file_path)
100
  file_extension = file_extension.lower()
101
 
102
- if file_extension in (".txt", ".py"):
 
 
 
 
 
 
 
 
 
103
  with open(file_path, "r", encoding="utf-8") as f:
104
  content = f.read()
105
  return {"file_type": "text/code", "file_name": file_path, "file_content": content}
 
 
106
  elif file_extension == ".xlsx":
107
  df = pd.read_excel(file_path)
108
  content = df.to_string()
109
  return {"file_type": "excel", "file_name": file_path, "file_content": content}
110
- elif file_extension in (".jpeg", ".jpg", ".png"):
111
- # For images, we indicate it's an image file and expect the LLM to handle the blob directly.
112
- return {"file_type": "image", "file_name": file_path, "file_content": f"Image file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this image content directly."}
113
- elif file_extension == ".mp3":
114
- # For MP3, we indicate it's an audio file and expect the LLM to handle the blob directly.
115
- return {"file_type": "audio", "file_name": file_path, "file_content": f"Audio file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this audio content directly."}
116
  else:
117
- return {"file_type": "unsupported", "file_name": file_path, "file_content": f"Unsupported file type: {file_extension}. Only .txt, .py, .xlsx, .jpeg, .jpg, .png, .mp3 files are recognized."}
 
118
  except FileNotFoundError:
119
  return {"file_error": f"File not found: {file_path}. Please ensure the file exists in the environment."}
120
  except Exception as e:
121
  return {"file_error": f"Error reading file {file_path}: {e}"}
122
 
 
123
  @tool
124
  def python_interpreter(code: str) -> Dict[str, str]:
125
  """Executes Python code and returns its standard output. If there's an error during execution, it returns the error message."""
 
92
  else:
93
  print("WARNING: HF_API_TOKEN not set. If any other HF tools are used, they might not function.")
94
 
95
+ @tooldef
96
  def read_file_content(file_path: str) -> Dict[str, str]:
97
+ """Reads the content of a file and returns its primary information. For text/code/excel, returns content. For media, indicates it's a blob for LLM processing."""
98
  try:
99
  _, file_extension = os.path.splitext(file_path)
100
  file_extension = file_extension.lower()
101
 
102
+ # Prioritize handling of video, audio, and image files for direct LLM processing
103
+ if file_extension in (".mp4", ".avi", ".mov", ".mkv", ".webm"):
104
+ return {"file_type": "video", "file_name": file_path, "file_content": f"Video file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this video content directly as a blob."}
105
+ elif file_extension == ".mp3":
106
+ return {"file_type": "audio", "file_name": file_path, "file_content": f"Audio file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this audio content directly as a blob."}
107
+ elif file_extension in (".jpeg", ".jpg", ".png"):
108
+ return {"file_type": "image", "file_name": file_path, "file_content": f"Image file '{file_path}' detected. The LLM (Gemini 2.5 Pro) can process this image content directly as a blob."}
109
+
110
+ # Handle text and code files
111
+ elif file_extension in (".txt", ".py"):
112
  with open(file_path, "r", encoding="utf-8") as f:
113
  content = f.read()
114
  return {"file_type": "text/code", "file_name": file_path, "file_content": content}
115
+
116
+ # Handle Excel files
117
  elif file_extension == ".xlsx":
118
  df = pd.read_excel(file_path)
119
  content = df.to_string()
120
  return {"file_type": "excel", "file_name": file_path, "file_content": content}
121
+
 
 
 
 
 
122
  else:
123
+ return {"file_type": "unsupported", "file_name": file_path, "file_content": f"Unsupported file type: {file_extension}. Only .txt, .py, .xlsx, .jpeg, .jpg, .png, .mp3, .mp4, .avi, .mov, .mkv, .webm files are recognized."}
124
+
125
  except FileNotFoundError:
126
  return {"file_error": f"File not found: {file_path}. Please ensure the file exists in the environment."}
127
  except Exception as e:
128
  return {"file_error": f"Error reading file {file_path}: {e}"}
129
 
130
+
131
  @tool
132
  def python_interpreter(code: str) -> Dict[str, str]:
133
  """Executes Python code and returns its standard output. If there's an error during execution, it returns the error message."""