pvanand commited on
Commit
2bb5179
·
verified ·
1 Parent(s): 8b7c87d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +76 -3
main.py CHANGED
@@ -12,6 +12,7 @@ import sqlite3
12
  import time
13
  from datetime import datetime, timedelta
14
  import asyncio
 
15
 
16
  app = FastAPI()
17
 
@@ -48,10 +49,21 @@ class QueryModel(BaseModel):
48
  }
49
  }
50
 
 
 
 
 
 
 
 
 
 
 
51
  @lru_cache()
52
  def get_api_keys():
53
  return {
54
- "OPENROUTER_API_KEY": f"sk-or-v1-{os.environ['OPENROUTER_API_KEY']}"
 
55
  }
56
 
57
  api_keys = get_api_keys()
@@ -152,7 +164,6 @@ async def startup_event():
152
  async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
153
  """
154
  Coding assistant endpoint that provides programming help based on user queries.
155
-
156
  Available models:
157
  - meta-llama/llama-3-70b-instruct (default)
158
  - anthropic/claude-3.5-sonnet
@@ -161,7 +172,6 @@ async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks,
161
  - openai/gpt-3.5-turbo-instruct
162
  - qwen/qwen-72b-chat
163
  - google/gemma-2-27b-it
164
-
165
  Requires API Key authentication via X-API-Key header.
166
  """
167
  if query.conversation_id not in conversations:
@@ -184,6 +194,69 @@ async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks,
184
 
185
  return StreamingResponse(process_response(), media_type="text/event-stream")
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  if __name__ == "__main__":
188
  import uvicorn
189
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
12
  import time
13
  from datetime import datetime, timedelta
14
  import asyncio
15
+ import requests
16
 
17
  app = FastAPI()
18
 
 
49
  }
50
  }
51
 
52
+ class NewsQueryModel(BaseModel):
53
+ query: str = Field(..., description="News topic to search for")
54
+
55
+ class Config:
56
+ schema_extra = {
57
+ "example": {
58
+ "query": "Latest developments in AI"
59
+ }
60
+ }
61
+
62
  @lru_cache()
63
  def get_api_keys():
64
  return {
65
+ "OPENROUTER_API_KEY": f"sk-or-v1-{os.environ['OPENROUTER_API_KEY']}",
66
+ "BRAVE_API_KEY": os.environ['BRAVE_API_KEY']
67
  }
68
 
69
  api_keys = get_api_keys()
 
164
  async def coding_assistant(query: QueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
165
  """
166
  Coding assistant endpoint that provides programming help based on user queries.
 
167
  Available models:
168
  - meta-llama/llama-3-70b-instruct (default)
169
  - anthropic/claude-3.5-sonnet
 
172
  - openai/gpt-3.5-turbo-instruct
173
  - qwen/qwen-72b-chat
174
  - google/gemma-2-27b-it
 
175
  Requires API Key authentication via X-API-Key header.
176
  """
177
  if query.conversation_id not in conversations:
 
194
 
195
  return StreamingResponse(process_response(), media_type="text/event-stream")
196
 
197
+ # New functions for news assistant
198
+ def fetch_news(query, num_results=20):
199
+ url = "https://api.search.brave.com/res/v1/news/search"
200
+ headers = {
201
+ "Accept": "application/json",
202
+ "Accept-Encoding": "gzip",
203
+ "X-Subscription-Token": api_keys["BRAVE_API_KEY"]
204
+ }
205
+ params = {"q": query}
206
+
207
+ response = requests.get(url, headers=headers, params=params)
208
+
209
+ if response.status_code == 200:
210
+ news_data = response.json()
211
+ return [
212
+ {
213
+ "title": item["title"],
214
+ "snippet": item["extra_snippets"][0] if "extra_snippets" in item and item["extra_snippets"] else "",
215
+ "last_updated": item.get("age", ""),
216
+ }
217
+ for item in news_data['results']
218
+ if "extra_snippets" in item and item["extra_snippets"]
219
+ ][:num_results]
220
+ else:
221
+ return []
222
+
223
+ def analyze_news(query):
224
+ news_data = fetch_news(query)
225
+
226
+ if not news_data:
227
+ return "Failed to fetch news data.", []
228
+
229
+ # Prepare the prompt for the AI
230
+ prompt = f"Based on the following recent news about '{query}', Provide a well summarized and formatted answer using markdown, give importance to the latest news:\n\n"
231
+ for item in news_data:
232
+ prompt += f"Title: {item['title']}\n"
233
+ prompt += f"Snippet: {item['snippet']}\n"
234
+ prompt += f"Last Updated: {item['last_updated']}\n\n"
235
+
236
+ messages = [
237
+ {"role": "system", "content": "You are a knowledgeable Assistant capable of providing insightful answers on various topics."},
238
+ {"role": "user", "content": prompt}
239
+ ]
240
+
241
+ return messages
242
+
243
+ @app.post("/news-assistant")
244
+ async def news_assistant(query: NewsQueryModel, api_key: str = Depends(verify_api_key)):
245
+ """
246
+ News assistant endpoint that provides summaries and analysis of recent news based on user queries.
247
+ Requires API Key authentication via X-API-Key header.
248
+ """
249
+ messages = analyze_news(query.query)
250
+
251
+ if not messages:
252
+ raise HTTPException(status_code=500, detail="Failed to fetch news data")
253
+
254
+ def process_response():
255
+ for content in chat_with_llama_stream(messages, model="meta-llama/llama-3-70b-instruct"):
256
+ yield content
257
+
258
+ return StreamingResponse(process_response(), media_type="text/event-stream")
259
+
260
  if __name__ == "__main__":
261
  import uvicorn
262
  uvicorn.run(app, host="0.0.0.0", port=7860)