-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
265 lines (211 loc) · 8.53 KB
/
Copy pathutils.py
File metadata and controls
265 lines (211 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import asyncio
import os
from typing import Any, Dict, List, Optional
import requests
from models import SearchResponse, SearchResult
# Try importing search libraries, provide graceful fallbacks
try:
from tavily import AsyncTavilyClient
except ImportError:
print("Tavily not installed. Use 'pip install tavily-python' to enable.")
AsyncTavilyClient = None
try:
from exa_py import Exa
except ImportError:
print("Exa not installed. Use 'pip install exa-py' to enable.")
Exa = None
def get_search_params(
search_api: str, search_api_config: Optional[Dict[str, Any]]
) -> Dict[str, Any]:
"""Extract valid parameters for a search API from a configuration dictionary."""
params_to_pass = {}
if not search_api_config:
return params_to_pass
if search_api == "exa":
# Exa supports: max_characters, num_results, include_domains, exclude_domains, subpages
valid_params = [
"max_characters",
"num_results",
"include_domains",
"exclude_domains",
"subpages",
]
for param in valid_params:
if param in search_api_config:
params_to_pass[param] = search_api_config[param]
elif search_api == "tavily":
# Tavily supports: max_results, search_depth, include_domains, exclude_domains, include_raw_content
valid_params = [
"max_results",
"search_depth",
"include_domains",
"exclude_domains",
"include_raw_content",
]
for param in valid_params:
if param in search_api_config:
params_to_pass[param] = search_api_config[param]
elif search_api == "perplexity":
# Perplexity supports: max_results
if "max_results" in search_api_config:
params_to_pass["max_results"] = search_api_config["max_results"]
return params_to_pass
def normalize_search_results(
search_responses: List[Dict[str, Any]],
) -> List[SearchResponse]:
"""Normalize search responses from different APIs into a common format."""
normalized_responses = []
for response in search_responses:
# Extract the query and results
query = response.get("query", "")
raw_results = response.get("results", [])
# Normalize each result
results = []
for result in raw_results:
search_result = SearchResult(
title=result.get("title", "No title"),
url=result.get("url", ""),
content=result.get("content", ""),
score=result.get("score", 1.0),
raw_content=result.get("raw_content"),
)
results.append(search_result)
# Create normalized response
normalized_response = SearchResponse(query=query, results=results)
normalized_responses.append(normalized_response)
return normalized_responses
async def tavily_search_async(
search_queries: List[str], **kwargs
) -> List[Dict[str, Any]]:
"""Perform concurrent web searches using the Tavily API."""
if not AsyncTavilyClient:
raise ImportError(
"Tavily client not installed. Install with 'pip install tavily-python'"
)
tavily_async_client = AsyncTavilyClient()
search_tasks = []
# Set default parameters
max_results = kwargs.get("max_results", 5)
include_raw_content = kwargs.get("include_raw_content", True)
search_depth = kwargs.get("search_depth", "basic")
include_domains = kwargs.get("include_domains", None)
exclude_domains = kwargs.get("exclude_domains", None)
for query in search_queries:
search_tasks.append(
tavily_async_client.search(
query,
max_results=max_results,
include_raw_content=include_raw_content,
search_depth=search_depth,
include_domains=include_domains,
exclude_domains=exclude_domains,
topic="general",
)
)
# Execute all searches concurrently
search_docs = await asyncio.gather(*search_tasks)
return search_docs
def perplexity_search(search_queries: List[str], **kwargs) -> List[Dict[str, Any]]:
"""Search the web using the Perplexity API."""
api_key = os.environ.get("PERPLEXITY_API_KEY")
if not api_key:
raise ValueError("PERPLEXITY_API_KEY environment variable not set")
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {api_key}",
}
# Set default parameters
max_results = kwargs.get("max_results", 5)
search_results = []
for query in search_queries:
url = "https://api.perplexity.ai/search"
payload = {"query": query, "max_results": max_results}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
results = []
for item in data.get("results", []):
result = {
"title": item.get("title", ""),
"url": item.get("url", ""),
"content": (
item.get("snippets", [""])[0] if item.get("snippets") else ""
),
"score": 1.0,
"raw_content": None,
}
results.append(result)
search_results.append({"query": query, "results": results})
except Exception as e:
print(f"Error in Perplexity search for query '{query}': {str(e)}")
search_results.append({"query": query, "results": []})
return search_results
async def exa_search(search_queries: List[str], **kwargs) -> List[Dict[str, Any]]:
"""Search the web using the Exa API."""
if not Exa:
raise ImportError("Exa not installed. Install with 'pip install exa-py'")
api_key = os.environ.get("EXA_API_KEY")
if not api_key:
raise ValueError("EXA_API_KEY environment variable not set")
exa_client = Exa(api_key=api_key)
# Set default parameters
num_results = kwargs.get("num_results", 5)
include_domains = kwargs.get("include_domains")
exclude_domains = kwargs.get("exclude_domains")
max_characters = kwargs.get("max_characters")
search_results = []
for query in search_queries:
try:
# Configure parameters
search_params = {"num_results": num_results}
if include_domains:
search_params["include_domains"] = include_domains
if exclude_domains:
search_params["exclude_domains"] = exclude_domains
if max_characters:
search_params["text"] = {"max_characters": max_characters}
# Execute search
response = exa_client.search(query, **search_params)
results = []
for result in response.results:
results.append(
{
"title": result.title,
"url": result.url,
"content": result.text,
"score": 1.0,
"raw_content": result.text,
}
)
search_results.append({"query": query, "results": results})
# Respect rate limits
await asyncio.sleep(0.25)
except Exception as e:
print(f"Error in Exa search for query '{query}': {str(e)}")
search_results.append({"query": query, "results": []})
return search_results
async def select_and_execute_search(
search_api: str, query_list: List[str], params: Dict[str, Any]
) -> List[SearchResponse]:
"""Select and execute the appropriate search API based on configuration.
Args:
search_api: Name of the search API to use
query_list: List of search queries to execute
params: Parameters to pass to the search API
Returns:
List of normalized search responses
Raises:
ValueError: If an unsupported search API is specified
"""
if search_api == "tavily":
search_results = await tavily_search_async(query_list, **params)
elif search_api == "perplexity":
search_results = perplexity_search(query_list, **params)
elif search_api == "exa":
search_results = await exa_search(query_list, **params)
else:
raise ValueError(f"Unsupported search API: {search_api}")
# Normalize the results into our standard format
return normalize_search_results(search_results)