Spaces:
Sleeping
Sleeping
Luigi D'Addona
commited on
Commit
·
ba25c91
1
Parent(s):
15b7f56
definito wikipedia tool alternativo per accedere al contenuto delle pagine wiki
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, 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,9 +35,9 @@ chat = ChatGoogleGenerativeAI(
|
|
| 35 |
|
| 36 |
# Imposta i tool
|
| 37 |
search_tool = get_search_tool()
|
| 38 |
-
wikipedia_tool = get_wikipedia_tool()
|
| 39 |
|
| 40 |
-
tools = [search_tool,
|
| 41 |
|
| 42 |
# Bind tools to the model
|
| 43 |
chat_with_tools = chat.bind_tools(tools)
|
|
@@ -62,6 +62,11 @@ def call_tool(state: AgentState):
|
|
| 62 |
for tool_call in state["messages"][-1].tool_calls:
|
| 63 |
# Get the tool by name
|
| 64 |
tool_result = tools_by_name[tool_call["name"]].invoke(tool_call["args"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
outputs.append(
|
| 66 |
ToolMessage(
|
| 67 |
content=tool_result,
|
|
|
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
|
| 14 |
# Local imports
|
| 15 |
+
from tools import get_search_tool, get_wikipedia_tool, wikipedia_search, 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 |
|
| 36 |
# Imposta i tool
|
| 37 |
search_tool = get_search_tool()
|
| 38 |
+
#wikipedia_tool = get_wikipedia_tool()
|
| 39 |
|
| 40 |
+
tools = [search_tool, wikipedia_search, execute_python_code_from_file]
|
| 41 |
|
| 42 |
# Bind tools to the model
|
| 43 |
chat_with_tools = chat.bind_tools(tools)
|
|
|
|
| 62 |
for tool_call in state["messages"][-1].tool_calls:
|
| 63 |
# Get the tool by name
|
| 64 |
tool_result = tools_by_name[tool_call["name"]].invoke(tool_call["args"])
|
| 65 |
+
|
| 66 |
+
# print(f"\n--- DEBUG: Tool Raw Output (Length: {len(tool_result)} chars) ---")
|
| 67 |
+
# print(tool_result)
|
| 68 |
+
# print("------------------------------------------------------------------\n")
|
| 69 |
+
|
| 70 |
outputs.append(
|
| 71 |
ToolMessage(
|
| 72 |
content=tool_result,
|
tools.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
import os, sys
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
|
| 4 |
-
# DuckDuckGo
|
| 5 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
-
# Wikipedia tool components
|
| 7 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 8 |
from langchain_community.tools import WikipediaQueryRun
|
| 9 |
-
|
| 10 |
from langchain_core.tools import tool
|
| 11 |
from langchain.tools import Tool
|
| 12 |
|
|
@@ -39,6 +37,35 @@ def get_wikipedia_tool():
|
|
| 39 |
return wikipedia_tool
|
| 40 |
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@tool
|
| 43 |
def execute_python_code_from_file(file_path: str) -> str:
|
| 44 |
"""
|
|
|
|
| 1 |
import os, sys
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
|
|
|
|
| 4 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 5 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 6 |
from langchain_community.tools import WikipediaQueryRun
|
| 7 |
+
import wikipedia
|
| 8 |
from langchain_core.tools import tool
|
| 9 |
from langchain.tools import Tool
|
| 10 |
|
|
|
|
| 37 |
return wikipedia_tool
|
| 38 |
|
| 39 |
|
| 40 |
+
@tool
|
| 41 |
+
def wikipedia_search(query: str) -> str:
|
| 42 |
+
"""
|
| 43 |
+
Search Wikipedia and return the full content of the most relevant article.
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
results = wikipedia.search(query)
|
| 47 |
+
if not results:
|
| 48 |
+
return f"No results found for '{query}'."
|
| 49 |
+
|
| 50 |
+
page = wikipedia.page(results[0])
|
| 51 |
+
content = page.content
|
| 52 |
+
|
| 53 |
+
# Truncate content if it's too long
|
| 54 |
+
if len(content) > WIKIPEDIA_DOC_CONTENT_CHARS_MAX:
|
| 55 |
+
content = content[:WIKIPEDIA_DOC_CONTENT_CHARS_MAX] + "..."
|
| 56 |
+
|
| 57 |
+
return content
|
| 58 |
+
|
| 59 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 60 |
+
return f"Ambiguous query. Possible options: {', '.join(e.options[:5])}..."
|
| 61 |
+
except wikipedia.exceptions.PageError:
|
| 62 |
+
return f"Page not found for '{query}'."
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error occurred: {str(e)}"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
@tool
|
| 70 |
def execute_python_code_from_file(file_path: str) -> str:
|
| 71 |
"""
|