dawalina commited on
Commit
cb8e5e3
·
1 Parent(s): 68bf2fc

Add new tool

Browse files
Files changed (4) hide show
  1. agent.py +2 -1
  2. requirements.txt +1 -0
  3. tools/__init__.py +2 -1
  4. tools/webpage_fetcher.py +40 -0
agent.py CHANGED
@@ -6,8 +6,9 @@ from tools import all_tools
6
 
7
  SYSTEM_PROMPT = (
8
  "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
9
- "You have access to a web search tool, an image identification tool, and a YouTube identifier tool. "
10
  "Use the search tool when you need to look up factual information. "
 
11
  "Use the image identification tool when the question involves an image URL. "
12
  "When the question includes a YouTube link (youtube.com or youtu.be), use the YouTube identifier tool with that URL first; it returns title, channel, and transcript text so you can describe what is said or shown in the video before answering."
13
  "IMPORTANT: When calling any tool, pass ONLY the required arguments as plain values. "
 
6
 
7
  SYSTEM_PROMPT = (
8
  "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
9
+ "You have access to a web search tool, a webpage fetcher tool, an image identification tool, and a YouTube identifier tool. "
10
  "Use the search tool when you need to look up factual information. "
11
+ "Use the webpage fetcher tool when you need to read the actual contents of a specific URL (e.g. a Wikipedia article, a research paper, or any link found via search). First search, then fetch the relevant URL to get detailed information. "
12
  "Use the image identification tool when the question involves an image URL. "
13
  "When the question includes a YouTube link (youtube.com or youtu.be), use the YouTube identifier tool with that URL first; it returns title, channel, and transcript text so you can describe what is said or shown in the video before answering."
14
  "IMPORTANT: When calling any tool, pass ONLY the required arguments as plain values. "
requirements.txt CHANGED
@@ -10,6 +10,7 @@ langgraph-sdk==0.3.12
10
  langchain-huggingface==1.2.1
11
  langchain-community==0.4.1
12
  langchain-groq
 
13
  huggingface_hub
14
  truststore
15
  youtube-transcript-api
 
10
  langchain-huggingface==1.2.1
11
  langchain-community==0.4.1
12
  langchain-groq
13
+ beautifulsoup4
14
  huggingface_hub
15
  truststore
16
  youtube-transcript-api
tools/__init__.py CHANGED
@@ -1,5 +1,6 @@
1
  from tools.search import search_tool
2
  from tools.image_identifier import image_identifier_tool
3
  from tools.youtube_identifier import youtube_identifier_tool
 
4
 
5
- all_tools = [search_tool, image_identifier_tool, youtube_identifier_tool]
 
1
  from tools.search import search_tool
2
  from tools.image_identifier import image_identifier_tool
3
  from tools.youtube_identifier import youtube_identifier_tool
4
+ from tools.webpage_fetcher import webpage_fetcher_tool
5
 
6
+ all_tools = [search_tool, image_identifier_tool, youtube_identifier_tool, webpage_fetcher_tool]
tools/webpage_fetcher.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ from langchain_core.tools import tool
4
+
5
+ _MAX_CHARS = 10_000
6
+ _HEADERS = {
7
+ "User-Agent": (
8
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
9
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
10
+ "Chrome/124.0.0.0 Safari/537.36"
11
+ )
12
+ }
13
+ _STRIP_TAGS = {"script", "style", "nav", "footer", "header", "noscript", "svg", "img"}
14
+
15
+
16
+ @tool
17
+ def webpage_fetcher_tool(url: str) -> str:
18
+ """Fetch a webpage URL and return its readable text content.
19
+
20
+ Use this tool when you need to read the actual contents of a specific
21
+ web page, for example a Wikipedia article, a research paper, or any
22
+ URL discovered via search. Pass the full URL as a string.
23
+ """
24
+ try:
25
+ resp = requests.get(url.strip(), headers=_HEADERS, timeout=30)
26
+ resp.raise_for_status()
27
+ except requests.RequestException as exc:
28
+ return f"Error fetching URL: {exc}"
29
+
30
+ soup = BeautifulSoup(resp.text, "html.parser")
31
+
32
+ for tag in soup.find_all(_STRIP_TAGS):
33
+ tag.decompose()
34
+
35
+ text = soup.get_text(separator="\n", strip=True)
36
+
37
+ if len(text) > _MAX_CHARS:
38
+ text = text[:_MAX_CHARS] + "\n[... truncated ...]"
39
+
40
+ return text if text else "No readable text found on this page."