Spaces:
Sleeping
Sleeping
Luigi D'Addona
commited on
Commit
·
0cb983c
1
Parent(s):
9280a27
aggiunto tool analyze_excel_file per lavorare dui file Excel
Browse files
agent.py
CHANGED
|
@@ -13,7 +13,8 @@ from langgraph.graph import StateGraph, END
|
|
| 13 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 14 |
|
| 15 |
# Local imports
|
| 16 |
-
from tools import get_search_tool, get_tavily_search_tool, get_wikipedia_tool, wikipedia_search, wikipedia_search_3
|
|
|
|
| 17 |
|
| 18 |
# Nota: per i test in locale si usa il .env
|
| 19 |
# su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
|
|
@@ -58,7 +59,7 @@ chat = ChatGoogleGenerativeAI(
|
|
| 58 |
search_tool = get_tavily_search_tool()
|
| 59 |
#wikipedia_tool = get_wikipedia_tool()
|
| 60 |
|
| 61 |
-
tools = [search_tool, wikipedia_search_3, execute_python_code_from_file, download_taskid_file]
|
| 62 |
|
| 63 |
# Bind tools to the model
|
| 64 |
chat_with_tools = chat.bind_tools(tools)
|
|
|
|
| 13 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 14 |
|
| 15 |
# Local imports
|
| 16 |
+
from tools import get_search_tool, get_tavily_search_tool, get_wikipedia_tool, wikipedia_search, wikipedia_search_3,\
|
| 17 |
+
execute_python_code_from_file, download_taskid_file, analyze_excel_file
|
| 18 |
|
| 19 |
# Nota: per i test in locale si usa il .env
|
| 20 |
# su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
|
|
|
|
| 59 |
search_tool = get_tavily_search_tool()
|
| 60 |
#wikipedia_tool = get_wikipedia_tool()
|
| 61 |
|
| 62 |
+
tools = [search_tool, wikipedia_search_3, execute_python_code_from_file, download_taskid_file, analyze_excel_file]
|
| 63 |
|
| 64 |
# Bind tools to the model
|
| 65 |
chat_with_tools = chat.bind_tools(tools)
|
tools.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os, sys
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
from langchain_community.utilities import WikipediaAPIWrapper
|
|
@@ -167,4 +168,34 @@ def download_taskid_file(task_id: str, file_name: str) -> str:
|
|
| 167 |
file.write(response.content)
|
| 168 |
return os.path.abspath(file_name)
|
| 169 |
except Exception as e:
|
| 170 |
-
return "Error occurred: {}".format(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, sys
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
from langchain_community.utilities import WikipediaAPIWrapper
|
|
|
|
| 168 |
file.write(response.content)
|
| 169 |
return os.path.abspath(file_name)
|
| 170 |
except Exception as e:
|
| 171 |
+
return "Error occurred: {}".format(e)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
@tool
|
| 175 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 176 |
+
"""
|
| 177 |
+
Analyzes an Excel (.xlsx) file using pandas.
|
| 178 |
+
Loads the specified Excel file into a pandas DataFrame and executes a Python query against it.
|
| 179 |
+
The query should be a valid pandas DataFrame operation (e.g., df.head(), df.describe(),
|
| 180 |
+
df[df['column_name'] > 10], df.groupby('category')['value'].mean()).
|
| 181 |
+
Returns the result of the query as a string (JSON or string representation).
|
| 182 |
+
"""
|
| 183 |
+
if not os.path.exists(file_path):
|
| 184 |
+
return f"Error: File not found at {file_path}"
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
df = pd.read_excel(file_path)
|
| 188 |
+
|
| 189 |
+
# Make the DataFrame accessible for the query
|
| 190 |
+
local_vars = {"df": df}
|
| 191 |
+
|
| 192 |
+
# Execute the query
|
| 193 |
+
# IMPORTANT: Be extremely cautious with eval/exec for user-provided input in a production system.
|
| 194 |
+
# For a ReAct agent, the LLM generates this query, so it's generally safer
|
| 195 |
+
# if the LLM is well-constrained and reliable.
|
| 196 |
+
# For sensitive applications, consider a safer parsing mechanism or a restricted set of operations.
|
| 197 |
+
result = eval(query, {}, local_vars)
|
| 198 |
+
|
| 199 |
+
return str(result) # Convert result to string for the LLM
|
| 200 |
+
except Exception as e:
|
| 201 |
+
return f"Error analyzing Excel file: {e}"
|