Spaces:
Runtime error
Runtime error
Create agent_tools.py
Browse files- agent_tools.py +175 -0
agent_tools.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
Perform a search using DuckDuckGo and return the results.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
query: The search query string
|
12 |
+
count: Maximum number of results to return (default: 3)
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
List of search results
|
16 |
+
"""
|
17 |
+
print(f"Performing DuckDuckGo search for: {query}")
|
18 |
+
|
19 |
+
try:
|
20 |
+
headers = {
|
21 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
22 |
+
}
|
23 |
+
|
24 |
+
# Format the query for the URL
|
25 |
+
formatted_query = query.replace(' ', '+')
|
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 |
+
# Send the request
|
30 |
+
response = requests.get(url, headers=headers, timeout=10)
|
31 |
+
response.raise_for_status()
|
32 |
+
|
33 |
+
# Parse the HTML response
|
34 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
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 |
+
# results.append({
|
49 |
+
# "title": title,
|
50 |
+
# "url": url,
|
51 |
+
# "snippet": snippet
|
52 |
+
# })
|
53 |
+
|
54 |
+
results.append(snippet)
|
55 |
+
|
56 |
+
if len(results) >= count:
|
57 |
+
break
|
58 |
+
|
59 |
+
print(f"DuckDuckGo results: {results}")
|
60 |
+
return results
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error during DuckDuckGo search: {e}")
|
63 |
+
return []
|
64 |
+
|
65 |
+
|
66 |
+
def langsearch_search(query: str, count: int = 5) -> list:
|
67 |
+
"""
|
68 |
+
Perform a search using LangSearch API and return the results.
|
69 |
+
|
70 |
+
Args:
|
71 |
+
query: The search query string
|
72 |
+
count: Maximum number of results to return (default: 5)
|
73 |
+
api_key: LangSearch API key (default: None, will look for env variable)
|
74 |
+
|
75 |
+
Returns:
|
76 |
+
List of search results
|
77 |
+
"""
|
78 |
+
print(f"Performing LangSearch search for: {query}")
|
79 |
+
|
80 |
+
try:
|
81 |
+
import os
|
82 |
+
# Use API key from parameters or environment variable
|
83 |
+
api_key = os.environ.get("LS_TOKEN")
|
84 |
+
|
85 |
+
if not api_key:
|
86 |
+
print("Warning: No LangSearch API key provided. Set LS_TOKEN environment variable.")
|
87 |
+
return []
|
88 |
+
|
89 |
+
headers = {
|
90 |
+
"Content-Type": "application/json",
|
91 |
+
"Authorization": f"Bearer {api_key}"
|
92 |
+
}
|
93 |
+
|
94 |
+
payload = json.dumps({
|
95 |
+
"query": query,
|
96 |
+
"freshness": "noLimit",
|
97 |
+
"summary": True,
|
98 |
+
"count": count
|
99 |
+
})
|
100 |
+
|
101 |
+
url = "https://api.langsearch.com/v1/web-search"
|
102 |
+
|
103 |
+
response = requests.post(url, headers=headers, data=payload, timeout=30)
|
104 |
+
response.raise_for_status()
|
105 |
+
if response.status_code != 200:
|
106 |
+
print(f"LangSearch API error: {response.text}")
|
107 |
+
return []
|
108 |
+
response = response.json()
|
109 |
+
results = []
|
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 |
+
# Dictionary mapping tool names to their functions
|
120 |
+
TOOLS_MAPPING = {
|
121 |
+
"duckduckgo_search": duckduckgo_search,
|
122 |
+
"langsearch_search": langsearch_search
|
123 |
+
}
|
124 |
+
|
125 |
+
# Tool definitions for LLM API
|
126 |
+
TOOLS_DEFINITION = [
|
127 |
+
{
|
128 |
+
"type": "function",
|
129 |
+
"function": {
|
130 |
+
"name": "duckduckgo_search",
|
131 |
+
"description": "Search the web using DuckDuckGo and return relevant results",
|
132 |
+
"parameters": {
|
133 |
+
"type": "object",
|
134 |
+
"properties": {
|
135 |
+
"query": {
|
136 |
+
"type": "string",
|
137 |
+
"description": "The search query string"
|
138 |
+
},
|
139 |
+
"num_results": {
|
140 |
+
"type": "integer",
|
141 |
+
"description": "Maximum number of results to return",
|
142 |
+
"default": 5
|
143 |
+
}
|
144 |
+
},
|
145 |
+
"required": ["query"]
|
146 |
+
}
|
147 |
+
}
|
148 |
+
},
|
149 |
+
{
|
150 |
+
"type": "function",
|
151 |
+
"function": {
|
152 |
+
"name": "langsearch_search",
|
153 |
+
"description": "Search the web using LangSearch API for more relevant results with deeper context",
|
154 |
+
"parameters": {
|
155 |
+
"type": "object",
|
156 |
+
"properties": {
|
157 |
+
"query": {
|
158 |
+
"type": "string",
|
159 |
+
"description": "The search query string"
|
160 |
+
},
|
161 |
+
"top_k": {
|
162 |
+
"type": "integer",
|
163 |
+
"description": "Maximum number of results to return",
|
164 |
+
"default": 5
|
165 |
+
},
|
166 |
+
"api_key": {
|
167 |
+
"type": "string",
|
168 |
+
"description": "LangSearch API key (optional, will use LANGSEARCH_API_KEY env var if not provided)"
|
169 |
+
}
|
170 |
+
},
|
171 |
+
"required": ["query"]
|
172 |
+
}
|
173 |
+
}
|
174 |
+
}
|
175 |
+
]
|