Spaces:
Running
Running
Update helper_functions_api.py
Browse files- helper_functions_api.py +27 -5
helper_functions_api.py
CHANGED
@@ -237,12 +237,34 @@ def fetch_and_extract_content(data_format, urls, query):
|
|
237 |
|
238 |
return all_text_with_urls
|
239 |
|
240 |
-
|
241 |
-
#@retry(tries=3, delay=0.25)
|
242 |
def search_brave(query, num_results=5):
|
|
|
|
|
243 |
cleaned_query = query #re.sub(r'[^a-zA-Z0-9]+', '', query)
|
244 |
search_query = together_response(cleaned_query, model=llm_default_small, SysPrompt=SysPromptSearch, max_tokens = 25).strip()
|
245 |
cleaned_search_query = re.sub(r'[^\w\s]', '', search_query).strip() #re.sub(r'[^a-zA-Z0-9*]+', '', search_query)
|
246 |
-
|
247 |
-
|
248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
|
238 |
return all_text_with_urls
|
239 |
|
|
|
|
|
240 |
def search_brave(query, num_results=5):
|
241 |
+
"""Fetch search results from Brave's API."""
|
242 |
+
|
243 |
cleaned_query = query #re.sub(r'[^a-zA-Z0-9]+', '', query)
|
244 |
search_query = together_response(cleaned_query, model=llm_default_small, SysPrompt=SysPromptSearch, max_tokens = 25).strip()
|
245 |
cleaned_search_query = re.sub(r'[^\w\s]', '', search_query).strip() #re.sub(r'[^a-zA-Z0-9*]+', '', search_query)
|
246 |
+
|
247 |
+
url = "https://api.search.brave.com/res/v1/web/search"
|
248 |
+
headers = {
|
249 |
+
"Accept": "application/json",
|
250 |
+
"Accept-Encoding": "gzip",
|
251 |
+
"X-Subscription-Token": BRAVE_API_KEY
|
252 |
+
}
|
253 |
+
params = {"q": cleaned_search_query}
|
254 |
+
|
255 |
+
response = requests.get(url, headers=headers, params=params)
|
256 |
+
|
257 |
+
if response.status_code == 200:
|
258 |
+
result = response.json() # Return the JSON response if successful
|
259 |
+
return [item["url"] for item in result["web"]["results"]][:num_results]
|
260 |
+
else:
|
261 |
+
return f"Error: {response.status_code}" # Return error code if not successful
|
262 |
+
|
263 |
+
# #@retry(tries=3, delay=0.25)
|
264 |
+
# def search_brave(query, num_results=5):
|
265 |
+
# cleaned_query = query #re.sub(r'[^a-zA-Z0-9]+', '', query)
|
266 |
+
# search_query = together_response(cleaned_query, model=llm_default_small, SysPrompt=SysPromptSearch, max_tokens = 25).strip()
|
267 |
+
# cleaned_search_query = re.sub(r'[^\w\s]', '', search_query).strip() #re.sub(r'[^a-zA-Z0-9*]+', '', search_query)
|
268 |
+
# brave = Brave(BRAVE_API_KEY)
|
269 |
+
# search_results = brave.search(q=cleaned_search_query, count=num_results)
|
270 |
+
# return [url.__str__() for url in search_results.urls],cleaned_search_query
|