Spaces:
Runtime error
Runtime error
Update agent_tools.py
Browse files- agent_tools.py +70 -86
agent_tools.py
CHANGED
@@ -1,145 +1,129 @@
|
|
1 |
import requests
|
2 |
import json
|
3 |
from bs4 import BeautifulSoup
|
|
|
4 |
|
5 |
-
|
6 |
-
def duckduckgo_search(query: str, count: int = 3) -> list:
|
7 |
"""
|
8 |
-
|
9 |
|
10 |
Args:
|
11 |
-
query: The search
|
12 |
-
|
13 |
|
14 |
Returns:
|
15 |
-
|
16 |
"""
|
17 |
-
print(f"
|
18 |
|
19 |
try:
|
20 |
headers = {
|
21 |
-
"User-Agent":
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
# Format the URL with query and parameter to increase snippet size
|
27 |
-
url = f"https://html.duckduckgo.com/html/?q={formatted_query}&kl=wt-wt"
|
28 |
|
29 |
-
|
30 |
-
response = requests.get(url, headers=headers, timeout=10)
|
31 |
response.raise_for_status()
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
# Extract search results
|
37 |
-
results = []
|
38 |
-
for result in soup.select('.result'):
|
39 |
-
title_elem = result.select_one('.result__title')
|
40 |
-
link_elem = result.select_one('.result__url')
|
41 |
-
snippet_elem = result.select_one('.result__snippet')
|
42 |
-
|
43 |
-
if title_elem and link_elem:
|
44 |
-
title = title_elem.get_text(strip=True)
|
45 |
-
url = link_elem.get('href') if link_elem.get('href') else link_elem.get_text(strip=True)
|
46 |
-
snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# })
|
53 |
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
if len(
|
57 |
break
|
58 |
|
59 |
-
print(f"DuckDuckGo
|
60 |
-
return
|
61 |
-
|
62 |
-
|
|
|
63 |
return []
|
64 |
|
65 |
|
66 |
-
def
|
67 |
"""
|
68 |
-
|
69 |
|
70 |
Args:
|
71 |
-
query: The search
|
72 |
-
|
73 |
-
api_key: LangSearch API key (default: None, will look for env variable)
|
74 |
|
75 |
Returns:
|
76 |
-
|
77 |
"""
|
78 |
-
print(f"
|
79 |
|
80 |
try:
|
81 |
-
|
82 |
-
# Use API key from parameters or environment variable
|
83 |
-
api_key = os.environ.get("LS_TOKEN")
|
84 |
|
85 |
-
if not
|
86 |
-
print("
|
87 |
return []
|
88 |
|
89 |
headers = {
|
90 |
"Content-Type": "application/json",
|
91 |
-
"Authorization": f"Bearer {
|
92 |
}
|
93 |
|
94 |
-
|
95 |
"query": query,
|
96 |
"freshness": "noLimit",
|
97 |
"summary": True,
|
98 |
-
"count":
|
99 |
})
|
100 |
|
101 |
-
|
|
|
|
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
for result in response["data"]["webPages"]["value"]:
|
111 |
-
results.append(result["summary"])
|
112 |
-
#print(f"LangSearch results: {results}")
|
113 |
-
return results
|
114 |
-
except Exception as e:
|
115 |
-
print(f"Error during LangSearch search: {e}")
|
116 |
return []
|
117 |
|
118 |
|
119 |
-
#
|
120 |
-
|
121 |
-
"
|
122 |
-
"
|
123 |
}
|
124 |
|
125 |
-
#
|
126 |
-
|
127 |
{
|
128 |
"type": "function",
|
129 |
"function": {
|
130 |
-
"name": "
|
131 |
-
"description": "
|
132 |
"parameters": {
|
133 |
"type": "object",
|
134 |
"properties": {
|
135 |
"query": {
|
136 |
"type": "string",
|
137 |
-
"description": "
|
138 |
},
|
139 |
-
"
|
140 |
"type": "integer",
|
141 |
-
"description": "
|
142 |
-
"default":
|
143 |
}
|
144 |
},
|
145 |
"required": ["query"]
|
@@ -149,23 +133,23 @@ TOOLS_DEFINITION = [
|
|
149 |
{
|
150 |
"type": "function",
|
151 |
"function": {
|
152 |
-
"name": "
|
153 |
-
"description": "
|
154 |
"parameters": {
|
155 |
"type": "object",
|
156 |
"properties": {
|
157 |
"query": {
|
158 |
"type": "string",
|
159 |
-
"description": "
|
160 |
},
|
161 |
-
"
|
162 |
"type": "integer",
|
163 |
-
"description": "
|
164 |
"default": 5
|
165 |
},
|
166 |
"api_key": {
|
167 |
"type": "string",
|
168 |
-
"description": "LangSearch API key (
|
169 |
}
|
170 |
},
|
171 |
"required": ["query"]
|
|
|
1 |
import requests
|
2 |
import json
|
3 |
from bs4 import BeautifulSoup
|
4 |
+
import os
|
5 |
|
6 |
+
def search_duckduckgo(query: str, max_results: int = 3) -> list:
|
|
|
7 |
"""
|
8 |
+
Execute a DuckDuckGo search and retrieve snippets from results.
|
9 |
|
10 |
Args:
|
11 |
+
query (str): The search term to look up.
|
12 |
+
max_results (int): Maximum number of result snippets to return. Default is 3.
|
13 |
|
14 |
Returns:
|
15 |
+
list: A list of result snippets (strings).
|
16 |
"""
|
17 |
+
print(f"Searching DuckDuckGo for: '{query}'")
|
18 |
|
19 |
try:
|
20 |
headers = {
|
21 |
+
"User-Agent": (
|
22 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
23 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
24 |
+
"Chrome/91.0.4472.124 Safari/537.36"
|
25 |
+
)
|
26 |
}
|
27 |
|
28 |
+
search_term = query.replace(" ", "+")
|
29 |
+
search_url = f"https://html.duckduckgo.com/html/?q={search_term}&kl=wt-wt"
|
|
|
|
|
30 |
|
31 |
+
response = requests.get(search_url, headers=headers, timeout=10)
|
|
|
32 |
response.raise_for_status()
|
33 |
|
34 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
35 |
+
snippets = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
for result in soup.select(".result"):
|
38 |
+
title = result.select_one(".result__title")
|
39 |
+
url = result.select_one(".result__url")
|
40 |
+
summary = result.select_one(".result__snippet")
|
|
|
41 |
|
42 |
+
if title and url:
|
43 |
+
snippet_text = summary.get_text(strip=True) if summary else ""
|
44 |
+
snippets.append(snippet_text)
|
45 |
|
46 |
+
if len(snippets) >= max_results:
|
47 |
break
|
48 |
|
49 |
+
print(f"DuckDuckGo snippets retrieved: {snippets}")
|
50 |
+
return snippets
|
51 |
+
|
52 |
+
except Exception as error:
|
53 |
+
print(f"Failed to fetch DuckDuckGo results: {error}")
|
54 |
return []
|
55 |
|
56 |
|
57 |
+
def search_langsearch(query: str, max_items: int = 5) -> list:
|
58 |
"""
|
59 |
+
Use the LangSearch API to perform a web search and return summaries.
|
60 |
|
61 |
Args:
|
62 |
+
query (str): The search phrase.
|
63 |
+
max_items (int): Number of results to return. Defaults to 5.
|
|
|
64 |
|
65 |
Returns:
|
66 |
+
list: A list of summarized search results.
|
67 |
"""
|
68 |
+
print(f"Using LangSearch API for: '{query}'")
|
69 |
|
70 |
try:
|
71 |
+
token = os.getenv("LS_TOKEN")
|
|
|
|
|
72 |
|
73 |
+
if not token:
|
74 |
+
print("LangSearch token not found. Set LS_TOKEN environment variable.")
|
75 |
return []
|
76 |
|
77 |
headers = {
|
78 |
"Content-Type": "application/json",
|
79 |
+
"Authorization": f"Bearer {token}"
|
80 |
}
|
81 |
|
82 |
+
body = json.dumps({
|
83 |
"query": query,
|
84 |
"freshness": "noLimit",
|
85 |
"summary": True,
|
86 |
+
"count": max_items
|
87 |
})
|
88 |
|
89 |
+
endpoint = "https://api.langsearch.com/v1/web-search"
|
90 |
+
res = requests.post(endpoint, headers=headers, data=body, timeout=30)
|
91 |
+
res.raise_for_status()
|
92 |
|
93 |
+
data = res.json()
|
94 |
+
summaries = [entry["summary"] for entry in data.get("data", {}).get("webPages", {}).get("value", [])]
|
95 |
+
|
96 |
+
return summaries
|
97 |
+
|
98 |
+
except Exception as error:
|
99 |
+
print(f"Error during LangSearch API call: {error}")
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
return []
|
101 |
|
102 |
|
103 |
+
# Map available tools to their corresponding function handlers
|
104 |
+
SEARCH_TOOLS = {
|
105 |
+
"search_duckduckgo": search_duckduckgo,
|
106 |
+
"search_langsearch": search_langsearch
|
107 |
}
|
108 |
|
109 |
+
# Definitions for LLM-compatible function tools
|
110 |
+
LLM_TOOL_DEFINITIONS = [
|
111 |
{
|
112 |
"type": "function",
|
113 |
"function": {
|
114 |
+
"name": "search_duckduckgo",
|
115 |
+
"description": "Query DuckDuckGo to retrieve short summaries of search results.",
|
116 |
"parameters": {
|
117 |
"type": "object",
|
118 |
"properties": {
|
119 |
"query": {
|
120 |
"type": "string",
|
121 |
+
"description": "Search term for the web query"
|
122 |
},
|
123 |
+
"max_results": {
|
124 |
"type": "integer",
|
125 |
+
"description": "Limit of results to return",
|
126 |
+
"default": 3
|
127 |
}
|
128 |
},
|
129 |
"required": ["query"]
|
|
|
133 |
{
|
134 |
"type": "function",
|
135 |
"function": {
|
136 |
+
"name": "search_langsearch",
|
137 |
+
"description": "Use the LangSearch API to find and summarize relevant web content.",
|
138 |
"parameters": {
|
139 |
"type": "object",
|
140 |
"properties": {
|
141 |
"query": {
|
142 |
"type": "string",
|
143 |
+
"description": "Text to search using LangSearch"
|
144 |
},
|
145 |
+
"max_items": {
|
146 |
"type": "integer",
|
147 |
+
"description": "Number of search results to return",
|
148 |
"default": 5
|
149 |
},
|
150 |
"api_key": {
|
151 |
"type": "string",
|
152 |
+
"description": "Optional LangSearch API key (uses LS_TOKEN from environment if not given)"
|
153 |
}
|
154 |
},
|
155 |
"required": ["query"]
|