pvanand commited on
Commit
b79d37a
1 Parent(s): 267ecdf

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -18
main.py CHANGED
@@ -13,7 +13,7 @@ import time
13
  from datetime import datetime, timedelta
14
  import asyncio
15
  import requests
16
- from prompts import CODING_ASSISTANT_PROMPT, NEWS_ASSISTANT_PROMPT, generate_news_prompt
17
  from fastapi_cache import FastAPICache
18
  from fastapi_cache.backends.inmemory import InMemoryBackend
19
  from fastapi_cache.decorator import cache
@@ -205,8 +205,12 @@ async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks,
205
 
206
  # New functions for news assistant
207
 
208
- def fetch_news(query, num_results=20):
209
- url = "https://api.search.brave.com/res/v1/news/search"
 
 
 
 
210
  headers = {
211
  "Accept": "application/json",
212
  "Accept-Encoding": "gzip",
@@ -216,27 +220,35 @@ def fetch_news(query, num_results=20):
216
 
217
  response = requests.get(url, headers=headers, params=params)
218
 
219
- if response.status_code == 200:
220
- news_data = response.json()
221
- return [
222
- {
223
- "title": item["title"],
224
- "snippet": item["extra_snippets"][0] if "extra_snippets" in item and item["extra_snippets"] else "",
225
- "last_updated": item.get("age", ""),
226
- }
227
- for item in news_data['results']
228
- if "extra_snippets" in item and item["extra_snippets"]
229
- ][:num_results]
230
- else:
231
  return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  @lru_cache(maxsize=100)
234
- def cached_fetch_news(query: str):
235
- return fetch_news(query)
236
 
237
 
238
  def analyze_news(query):
239
- news_data = cached_fetch_news(query)
240
 
241
  if not news_data:
242
  return "Failed to fetch news data.", []
 
13
  from datetime import datetime, timedelta
14
  import asyncio
15
  import requests
16
+ from prompts import CODING_ASSISTANT_PROMPT, NEWS_ASSISTANT_PROMPT, generate_news_prompt, SEARCH_ASSISTANT_PROMPT, generate_search_prompt
17
  from fastapi_cache import FastAPICache
18
  from fastapi_cache.backends.inmemory import InMemoryBackend
19
  from fastapi_cache.decorator import cache
 
205
 
206
  # New functions for news assistant
207
 
208
+ def internet_search(query, type = "web", num_results=20):
209
+ if type == "web":
210
+ url = "https://api.search.brave.com/res/v1/web/search"
211
+ else:
212
+ url = "https://api.search.brave.com/res/v1/news/search"
213
+
214
  headers = {
215
  "Accept": "application/json",
216
  "Accept-Encoding": "gzip",
 
220
 
221
  response = requests.get(url, headers=headers, params=params)
222
 
223
+ if response.status_code != 200:
 
 
 
 
 
 
 
 
 
 
 
224
  return []
225
+
226
+ if type == "web":
227
+ search_data = response.json()["web"]["results"]
228
+ else:
229
+ search_data = response.json()["results"]
230
+ processed_results = []
231
+
232
+ for item in search_data:
233
+ if not item.get("extra_snippets"):
234
+ continue
235
+
236
+ result = {
237
+ "title": item["title"],
238
+ "snippet": item["extra_snippets"][0],
239
+ "last_updated": item.get("age", "")
240
+ }
241
+ processed_results.append(result)
242
+
243
+ return processed_results[:num_results]
244
 
245
  @lru_cache(maxsize=100)
246
+ def cached_internet_search(query: str):
247
+ return internet_search(query, type = "news")
248
 
249
 
250
  def analyze_news(query):
251
+ news_data = cached_internet_search(query)
252
 
253
  if not news_data:
254
  return "Failed to fetch news data.", []