Updated tools
Browse files- tools_doc.py +88 -39
tools_doc.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import tempfile
|
|
@@ -6,9 +7,11 @@ import uuid
|
|
| 6 |
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# from smolagents import tool
|
| 10 |
from typing import Optional, Dict, Union
|
| 11 |
-
from urllib.parse import urlparse
|
| 12 |
|
| 13 |
|
| 14 |
@tool
|
|
@@ -142,68 +145,114 @@ def extract_text_from_image(image_path: str) -> str:
|
|
| 142 |
return f"Error extracting text from image: {str(e)}"
|
| 143 |
|
| 144 |
|
|
|
|
| 145 |
@tool
|
| 146 |
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 147 |
-
"""
|
| 148 |
-
Analyze a CSV file using pandas and answer a question about it.
|
| 149 |
-
|
| 150 |
Args:
|
| 151 |
-
file_path:
|
| 152 |
-
query:
|
| 153 |
-
|
| 154 |
Returns:
|
| 155 |
-
|
| 156 |
"""
|
| 157 |
try:
|
| 158 |
-
import pandas as pd
|
| 159 |
-
|
| 160 |
# Read the CSV file
|
| 161 |
df = pd.read_csv(file_path)
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
result +=
|
| 169 |
-
result += str(df.describe())
|
| 170 |
|
| 171 |
return result
|
| 172 |
-
except ImportError:
|
| 173 |
-
return "Error: pandas is not installed. Please install it with 'pip install pandas'."
|
| 174 |
except Exception as e:
|
| 175 |
return f"Error analyzing CSV file: {str(e)}"
|
| 176 |
|
| 177 |
|
|
|
|
| 178 |
@tool
|
| 179 |
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 180 |
-
"""
|
| 181 |
-
Analyze an Excel file using pandas and answer a question about it.
|
| 182 |
-
|
| 183 |
Args:
|
| 184 |
-
file_path:
|
| 185 |
-
query:
|
| 186 |
-
|
| 187 |
Returns:
|
| 188 |
-
|
| 189 |
"""
|
| 190 |
try:
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
return result
|
| 205 |
-
except ImportError:
|
| 206 |
-
return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
|
| 207 |
except Exception as e:
|
| 208 |
return f"Error analyzing Excel file: {str(e)}"
|
| 209 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
import re
|
| 4 |
import requests
|
| 5 |
import tempfile
|
|
|
|
| 7 |
|
| 8 |
from langchain_core.tools import tool
|
| 9 |
|
| 10 |
+
from google import genai
|
| 11 |
+
from google.genai import types
|
| 12 |
+
|
| 13 |
# from smolagents import tool
|
| 14 |
from typing import Optional, Dict, Union
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
|
|
|
| 145 |
return f"Error extracting text from image: {str(e)}"
|
| 146 |
|
| 147 |
|
| 148 |
+
# CSV Analysis Tool
|
| 149 |
@tool
|
| 150 |
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 151 |
+
"""Analyzes a CSV file and answers questions about its contents using Gemini.
|
|
|
|
|
|
|
| 152 |
Args:
|
| 153 |
+
file_path (str): the path to the CSV file to analyze.
|
| 154 |
+
query (str): the question to answer about the CSV file.
|
|
|
|
| 155 |
Returns:
|
| 156 |
+
str: The result of the analysis.
|
| 157 |
"""
|
| 158 |
try:
|
|
|
|
|
|
|
| 159 |
# Read the CSV file
|
| 160 |
df = pd.read_csv(file_path)
|
| 161 |
|
| 162 |
+
# Initialize Gemini
|
| 163 |
+
client = genai.Client(api_key=os.getenv("GEMINI_KEY"))
|
| 164 |
+
model = "models/gemini-1.5-flash-8b"
|
| 165 |
+
|
| 166 |
+
# Convert DataFrame to a string representation
|
| 167 |
+
df_str = df.to_string()
|
| 168 |
+
|
| 169 |
+
# Create a prompt for Gemini
|
| 170 |
+
prompt = f"""Analyze this CSV data and provide insights:
|
| 171 |
+
Dimensions: {len(df)} rows × {len(df.columns)} columns
|
| 172 |
+
Data:
|
| 173 |
+
{df_str}
|
| 174 |
+
Please provide:
|
| 175 |
+
1. A summary of the data structure and content
|
| 176 |
+
2. Key patterns and insights
|
| 177 |
+
3. Potential data quality issues
|
| 178 |
+
4. Suggestions for analysis
|
| 179 |
+
User Query: {query}
|
| 180 |
+
Please format your response in a clear, structured way with sections and bullet points."""
|
| 181 |
+
|
| 182 |
+
# Get analysis from Gemini
|
| 183 |
+
response = client.models.generate_content(
|
| 184 |
+
model=model,
|
| 185 |
+
contents=types.Content(
|
| 186 |
+
parts=[
|
| 187 |
+
types.Part(text=df_str),
|
| 188 |
+
types.Part(text=prompt),
|
| 189 |
+
]
|
| 190 |
+
),
|
| 191 |
+
)
|
| 192 |
|
| 193 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n\n"
|
| 194 |
+
result += response.text
|
|
|
|
| 195 |
|
| 196 |
return result
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
return f"Error analyzing CSV file: {str(e)}"
|
| 199 |
|
| 200 |
|
| 201 |
+
# Excel Analysis Tool
|
| 202 |
@tool
|
| 203 |
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 204 |
+
"""Analyzes an Excel file and answers questions about its contents using Gemini.
|
|
|
|
|
|
|
| 205 |
Args:
|
| 206 |
+
file_path (str): the path to the Excel file to analyze.
|
| 207 |
+
query (str): the question to answer about the Excel file.
|
|
|
|
| 208 |
Returns:
|
| 209 |
+
str: The result of the analysis.
|
| 210 |
"""
|
| 211 |
try:
|
| 212 |
+
# Read all sheets from the Excel file
|
| 213 |
+
excel_file = pd.ExcelFile(file_path)
|
| 214 |
+
sheet_names = excel_file.sheet_names
|
| 215 |
+
|
| 216 |
+
# Initialize Gemini
|
| 217 |
+
client = genai.Client(api_key=os.getenv("GEMINI_KEY"))
|
| 218 |
+
model = "models/gemini-1.5-flash-8b"
|
| 219 |
+
|
| 220 |
+
result = f"Excel file loaded with {len(sheet_names)} sheets: {', '.join(sheet_names)}\n\n"
|
| 221 |
+
|
| 222 |
+
# Analyze each sheet
|
| 223 |
+
for sheet_name in sheet_names:
|
| 224 |
+
df = pd.read_excel(file_path, sheet_name=sheet_name)
|
| 225 |
+
|
| 226 |
+
# Convert DataFrame to a string representation
|
| 227 |
+
df_str = df.to_string()
|
| 228 |
+
|
| 229 |
+
# Create a prompt for Gemini
|
| 230 |
+
prompt = f"""Analyze this Excel sheet data and provide insights:
|
| 231 |
+
Sheet Name: {sheet_name}
|
| 232 |
+
Dimensions: {len(df)} rows × {len(df.columns)} columns
|
| 233 |
+
Data:
|
| 234 |
+
{df_str}
|
| 235 |
+
Please provide:
|
| 236 |
+
1. A summary of the data structure and content
|
| 237 |
+
2. Key patterns and insights
|
| 238 |
+
3. Potential data quality issues
|
| 239 |
+
4. Suggestions for analysis
|
| 240 |
+
User Query: {query}
|
| 241 |
+
Please format your response in a clear, structured way with sections and bullet points."""
|
| 242 |
+
|
| 243 |
+
# Get analysis from Gemini
|
| 244 |
+
response = client.models.generate_content(
|
| 245 |
+
model=model,
|
| 246 |
+
contents=types.Content(
|
| 247 |
+
parts=[types.Part(text=df_str), types.Part(text=prompt)]
|
| 248 |
+
),
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
result += f"=== Sheet: {sheet_name} ===\n"
|
| 252 |
+
result += response.text + "\n"
|
| 253 |
+
result += "=" * 50 + "\n\n"
|
| 254 |
|
| 255 |
return result
|
|
|
|
|
|
|
| 256 |
except Exception as e:
|
| 257 |
return f"Error analyzing Excel file: {str(e)}"
|
| 258 |
|