Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1102,44 +1102,48 @@ def search_knowledge_base(query):
|
|
| 1102 |
|
| 1103 |
|
| 1104 |
# Function to perform an advanced Google search using SERP API
|
| 1105 |
-
def
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
search_client = Client(api_key=serper_api_key)
|
| 1109 |
-
|
| 1110 |
-
# Build advanced search parameters
|
| 1111 |
-
search_params = {
|
| 1112 |
-
"engine": "google",
|
| 1113 |
-
"q": query,
|
| 1114 |
-
"num": 10, # Number of results to fetch
|
| 1115 |
-
}
|
| 1116 |
-
if site:
|
| 1117 |
-
search_params["q"] += f" site:{site}" # Restrict results to a specific domain
|
| 1118 |
-
if exclude_terms:
|
| 1119 |
-
search_params["exclude_terms"] = exclude_terms
|
| 1120 |
-
if freshness:
|
| 1121 |
-
search_params["freshness"] = freshness # Options: last_day, last_week, last_month
|
| 1122 |
-
if gl:
|
| 1123 |
-
search_params["gl"] = gl # Country code (e.g., "us" for United States)
|
| 1124 |
-
if hl:
|
| 1125 |
-
search_params["hl"] = hl # Language code (e.g., "en" for English)
|
| 1126 |
-
|
| 1127 |
-
# Perform the search
|
| 1128 |
-
results = search_client.search(search_params)
|
| 1129 |
-
|
| 1130 |
-
# Extract snippets and URLs
|
| 1131 |
-
organic_results = results.get("organic_results", [])
|
| 1132 |
-
valid_results = [
|
| 1133 |
-
{"snippet": result["snippet"], "link": result["link"]}
|
| 1134 |
-
for result in organic_results
|
| 1135 |
-
if "snippet" in result and "link" in result
|
| 1136 |
-
]
|
| 1137 |
-
|
| 1138 |
-
return valid_results
|
| 1139 |
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1143 |
|
| 1144 |
def rag_response(query, selected_doc_ids=None):
|
| 1145 |
"""
|
|
@@ -1230,22 +1234,35 @@ def cache_response(query, response, ttl=3600):
|
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
-
tavily_tool = TavilySearchResults(
|
| 1234 |
-
|
| 1235 |
-
|
| 1236 |
-
|
| 1237 |
-
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
-
|
| 1243 |
-
|
| 1244 |
-
)#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1245 |
#Compile all tool functions into a list
|
| 1246 |
tools = [
|
| 1247 |
knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
|
| 1248 |
-
|
| 1249 |
#google_search_tool, # Tool for performing a Google search and retrieving search result snippets
|
| 1250 |
]
|
| 1251 |
|
|
|
|
| 1102 |
|
| 1103 |
|
| 1104 |
# Function to perform an advanced Google search using SERP API
|
| 1105 |
+
def brave_search(query, api_key, max_results=12, search_depth="advanced", days=3, include_answer=True, include_raw_content=True, exclude_domains=None):
|
| 1106 |
+
"""
|
| 1107 |
+
Perform a search using the Brave Search API.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1108 |
|
| 1109 |
+
Args:
|
| 1110 |
+
query (str): The search query string.
|
| 1111 |
+
api_key (str): Your Brave Search API key.
|
| 1112 |
+
max_results (int): Maximum number of results to retrieve.
|
| 1113 |
+
search_depth (str): Search depth ("basic" or "advanced").
|
| 1114 |
+
days (int): Limit results to the past specified days.
|
| 1115 |
+
include_answer (bool): Include direct answers in the response.
|
| 1116 |
+
include_raw_content (bool): Include raw content in the response.
|
| 1117 |
+
exclude_domains (list): List of domains to exclude from the results.
|
| 1118 |
+
|
| 1119 |
+
Returns:
|
| 1120 |
+
dict: Parsed search results from the Brave Search API.
|
| 1121 |
+
"""
|
| 1122 |
+
url = "https://api.brave.com/search"
|
| 1123 |
+
headers = {
|
| 1124 |
+
"Authorization": f"Bearer {api_key}",
|
| 1125 |
+
"Content-Type": "application/json",
|
| 1126 |
+
}
|
| 1127 |
+
|
| 1128 |
+
# Payload to customize the search
|
| 1129 |
+
payload = {
|
| 1130 |
+
"q": query,
|
| 1131 |
+
"count": max_results,
|
| 1132 |
+
"search_depth": search_depth,
|
| 1133 |
+
"days": days,
|
| 1134 |
+
"include_answer": include_answer,
|
| 1135 |
+
"include_raw_content": include_raw_content,
|
| 1136 |
+
}
|
| 1137 |
+
|
| 1138 |
+
if exclude_domains:
|
| 1139 |
+
payload["exclude_domains"] = exclude_domains
|
| 1140 |
+
|
| 1141 |
+
# Make the API request
|
| 1142 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 1143 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 1144 |
+
return response.json()
|
| 1145 |
+
|
| 1146 |
+
|
| 1147 |
|
| 1148 |
def rag_response(query, selected_doc_ids=None):
|
| 1149 |
"""
|
|
|
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
+
# tavily_tool = TavilySearchResults(
|
| 1238 |
+
# max_results=12,
|
| 1239 |
+
# search_depth="advanced",
|
| 1240 |
+
# days=3,
|
| 1241 |
+
# include_answer=True,
|
| 1242 |
+
# include_raw_content=True,
|
| 1243 |
+
# # include_domains=[...],
|
| 1244 |
+
# exclude_domains=['example.com'],
|
| 1245 |
+
# # name="...", # overwrite default tool name
|
| 1246 |
+
# # description="...", # overwrite default tool description
|
| 1247 |
+
# # args_schema=..., # overwrite default args_schema: BaseModel
|
| 1248 |
+
# )#
|
| 1249 |
+
api_key = "BSA8wB9NbA2fM76yt0lnU7OEpTQhAm6" # Replace with your Brave API key
|
| 1250 |
+
|
| 1251 |
+
results = brave_search(
|
| 1252 |
+
api_key=api_key,
|
| 1253 |
+
max_results=12,
|
| 1254 |
+
search_depth="advanced",
|
| 1255 |
+
days=1,
|
| 1256 |
+
include_answer=True,
|
| 1257 |
+
include_raw_content=True,
|
| 1258 |
+
exclude_domains=["example.com"]
|
| 1259 |
+
)
|
| 1260 |
+
|
| 1261 |
+
|
| 1262 |
#Compile all tool functions into a list
|
| 1263 |
tools = [
|
| 1264 |
knowledge_base_tool, # Tool for querying the knowledge base and retrieving responses
|
| 1265 |
+
results,
|
| 1266 |
#google_search_tool, # Tool for performing a Google search and retrieving search result snippets
|
| 1267 |
]
|
| 1268 |
|