pvanand commited on
Commit
62ab01b
1 Parent(s): cbcfc90

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +7 -0
main.py CHANGED
@@ -14,6 +14,9 @@ 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
 
18
  app = FastAPI()
19
 
@@ -159,6 +162,7 @@ async def clear_inactive_conversations():
159
 
160
  @app.on_event("startup")
161
  async def startup_event():
 
162
  asyncio.create_task(clear_inactive_conversations())
163
 
164
  @app.post("/coding-assistant")
@@ -196,6 +200,7 @@ async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks,
196
  return StreamingResponse(process_response(), media_type="text/event-stream")
197
 
198
  # New functions for news assistant
 
199
  def fetch_news(query, num_results=20):
200
  url = "https://api.search.brave.com/res/v1/news/search"
201
  headers = {
@@ -221,6 +226,7 @@ def fetch_news(query, num_results=20):
221
  else:
222
  return []
223
 
 
224
  def analyze_news(query):
225
  news_data = fetch_news(query)
226
 
@@ -239,6 +245,7 @@ def analyze_news(query):
239
  return messages
240
 
241
  @app.post("/news-assistant")
 
242
  async def news_assistant(query: NewsQueryModel, api_key: str = Depends(verify_api_key)):
243
  """
244
  News assistant endpoint that provides summaries and analysis of recent news based on user queries.
 
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
20
 
21
  app = FastAPI()
22
 
 
162
 
163
  @app.on_event("startup")
164
  async def startup_event():
165
+ FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
166
  asyncio.create_task(clear_inactive_conversations())
167
 
168
  @app.post("/coding-assistant")
 
200
  return StreamingResponse(process_response(), media_type="text/event-stream")
201
 
202
  # New functions for news assistant
203
+
204
  def fetch_news(query, num_results=20):
205
  url = "https://api.search.brave.com/res/v1/news/search"
206
  headers = {
 
226
  else:
227
  return []
228
 
229
+
230
  def analyze_news(query):
231
  news_data = fetch_news(query)
232
 
 
245
  return messages
246
 
247
  @app.post("/news-assistant")
248
+ @cache(expire=3600)
249
  async def news_assistant(query: NewsQueryModel, api_key: str = Depends(verify_api_key)):
250
  """
251
  News assistant endpoint that provides summaries and analysis of recent news based on user queries.