Spaces:
Sleeping
Sleeping
Luigi D'Addona
commited on
Commit
·
398bbe5
1
Parent(s):
948148d
aggiunto tool per eseguire codice python
Browse files
agent.py
CHANGED
|
@@ -12,7 +12,7 @@ from langgraph.graph import StateGraph, END
|
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
|
| 14 |
# Local imports
|
| 15 |
-
from tools import get_search_tool, get_wikipedia_tool
|
| 16 |
|
| 17 |
# Nota: per i test in locale si usa il .env
|
| 18 |
# su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
|
|
@@ -35,7 +35,7 @@ chat = ChatGoogleGenerativeAI(
|
|
| 35 |
search_tool = get_search_tool()
|
| 36 |
wikipedia_tool = get_wikipedia_tool()
|
| 37 |
|
| 38 |
-
tools = [search_tool, wikipedia_tool]
|
| 39 |
|
| 40 |
# Bind tools to the model
|
| 41 |
chat_with_tools = chat.bind_tools(tools)
|
|
|
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
|
| 14 |
# Local imports
|
| 15 |
+
from tools import get_search_tool, get_wikipedia_tool, execute_python_code_from_file
|
| 16 |
|
| 17 |
# Nota: per i test in locale si usa il .env
|
| 18 |
# su HuggingFace invece si usano le variabili definite in Settings/"Variables and secrets"
|
|
|
|
| 35 |
search_tool = get_search_tool()
|
| 36 |
wikipedia_tool = get_wikipedia_tool()
|
| 37 |
|
| 38 |
+
tools = [search_tool, wikipedia_tool, execute_python_code_from_file]
|
| 39 |
|
| 40 |
# Bind tools to the model
|
| 41 |
chat_with_tools = chat.bind_tools(tools)
|
tools.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
|
|
|
|
|
| 1 |
# DuckDuckGo
|
| 2 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 3 |
# Wikipedia tool components
|
| 4 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 5 |
from langchain_community.tools import WikipediaQueryRun
|
| 6 |
|
|
|
|
| 7 |
from langchain.tools import Tool
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_search_tool():
|
| 11 |
|
|
@@ -13,6 +18,7 @@ def get_search_tool():
|
|
| 13 |
|
| 14 |
return search_tool
|
| 15 |
|
|
|
|
| 16 |
def get_wikipedia_tool():
|
| 17 |
|
| 18 |
# creates an instance of the Wikipedia API wrapper. top_k_results=1 means it will only fetch the top result from Wikipedia
|
|
@@ -21,4 +27,48 @@ def get_wikipedia_tool():
|
|
| 21 |
# converts the WikipediaAPIWrapper into a LangChain tool.
|
| 22 |
wikipedia_tool = WikipediaQueryRun(api_wrapper=wikipedia_api_wrapper)
|
| 23 |
|
| 24 |
-
return wikipedia_tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, sys
|
| 2 |
+
|
| 3 |
# DuckDuckGo
|
| 4 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 5 |
# Wikipedia tool components
|
| 6 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 7 |
from langchain_community.tools import WikipediaQueryRun
|
| 8 |
|
| 9 |
+
from langchain_core.tools import tool
|
| 10 |
from langchain.tools import Tool
|
| 11 |
|
| 12 |
+
# per gestire esecuzione di codice python
|
| 13 |
+
import subprocess
|
| 14 |
|
| 15 |
def get_search_tool():
|
| 16 |
|
|
|
|
| 18 |
|
| 19 |
return search_tool
|
| 20 |
|
| 21 |
+
|
| 22 |
def get_wikipedia_tool():
|
| 23 |
|
| 24 |
# creates an instance of the Wikipedia API wrapper. top_k_results=1 means it will only fetch the top result from Wikipedia
|
|
|
|
| 27 |
# converts the WikipediaAPIWrapper into a LangChain tool.
|
| 28 |
wikipedia_tool = WikipediaQueryRun(api_wrapper=wikipedia_api_wrapper)
|
| 29 |
|
| 30 |
+
return wikipedia_tool
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def execute_python_code_from_file(file_path: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Reads a Python file from the given path, executes its code, and returns the combined stdout and stderr.
|
| 37 |
+
WARNING: Executing arbitrary code from files is a significant security risk.
|
| 38 |
+
Only use this tool with trusted code in a controlled environment.
|
| 39 |
+
"""
|
| 40 |
+
if not os.path.exists(file_path):
|
| 41 |
+
return f"Error: File not found at '{file_path}'."
|
| 42 |
+
|
| 43 |
+
if not file_path.endswith(".py"):
|
| 44 |
+
return f"Error: Provided file '{file_path}' is not a Python (.py) file."
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Use subprocess to run the Python file in a new process.
|
| 48 |
+
# This provides some isolation compared to 'exec()' but is still dangerous for untrusted code.
|
| 49 |
+
result = subprocess.run(
|
| 50 |
+
[sys.executable, file_path], # sys.executable ensures it uses the current Python interpreter
|
| 51 |
+
capture_output=True, # Capture stdout and stderr
|
| 52 |
+
text=True, # Capture output as text (strings)
|
| 53 |
+
check=False # Do not raise an exception for non-zero exit codes (handle errors manually)
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
stdout_output = result.stdout.strip()
|
| 57 |
+
stderr_output = result.stderr.strip()
|
| 58 |
+
|
| 59 |
+
output_lines = []
|
| 60 |
+
if stdout_output:
|
| 61 |
+
output_lines.append(f"STDOUT:\n{stdout_output}")
|
| 62 |
+
if stderr_output:
|
| 63 |
+
output_lines.append(f"STDERR:\n{stderr_output}")
|
| 64 |
+
|
| 65 |
+
if result.returncode != 0:
|
| 66 |
+
output_lines.append(f"Process exited with code {result.returncode}. This usually indicates an error.")
|
| 67 |
+
|
| 68 |
+
if not output_lines:
|
| 69 |
+
return "Execution completed with no output."
|
| 70 |
+
|
| 71 |
+
return "\n".join(output_lines)
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"An unexpected error occurred during code execution: {e}"
|