Pamudu13 commited on
Commit
239b271
·
verified ·
1 Parent(s): a5b55e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -11,7 +11,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
11
  import os
12
  from pydantic import BaseModel
13
 
14
- SAMBA_NOVA_API_KEY = os.environ.get("SAMBA_NOVA_API_KEY", None)
15
 
16
  app = FastAPI()
17
 
@@ -34,7 +34,7 @@ scheduler.start()
34
  class StreamTextRequest(BaseModel):
35
  query: str
36
  history: str = "[]"
37
- model: str = "llama3-8b"
38
  api_key: str = None
39
 
40
  @app.post("/stream_text")
@@ -48,29 +48,25 @@ async def stream_text(request: StreamTextRequest):
48
  return StreamingResponse(iter([f"{cached_response}"]), media_type='text/event-stream')
49
 
50
  # Model selection logic
51
- if "405" in request.model:
52
- fmodel = "Meta-Llama-3.1-405B-Instruct"
53
- if "70" in request.model:
54
- fmodel = "Meta-Llama-3.1-70B-Instruct"
55
- else:
56
- fmodel = "Meta-Llama-3.1-8B-Instruct"
57
 
58
  system_message = """You are an AI language assistant. Your sole task is to correct the grammar, spelling, and structure of the sentences provided to you. You must not change the meaning of the sentences, and you should focus only on making them grammatically correct, concise, and clear. Do not add any additional information or provide explanations unless specifically asked. Your responses should be limited to the corrected version of the sentence."""
59
 
60
  messages = [{'role': 'system', 'content': system_message}]
61
-
62
  messages.extend(ast.literal_eval(request.history))
63
-
64
  messages.append({'role': 'user', 'content': request.query})
65
 
66
- data = {'messages': messages, 'stream': True, 'model': fmodel}
67
-
68
- api_key = request.api_key if request.api_key != 'none' else SAMBA_NOVA_API_KEY
69
 
 
70
 
71
  async def stream_response():
72
  async with aiohttp.ClientSession() as session:
73
- async with session.post('https://api.sambanova.ai/v1/chat/completions', headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' }, json=data) as response:
 
 
 
 
74
  if response.status != 200:
75
  raise HTTPException(status_code=response.status, detail="Error fetching AI response")
76
 
@@ -95,10 +91,8 @@ async def stream_text(request: StreamTextRequest):
95
 
96
  return StreamingResponse(stream_response(), media_type='text/event-stream')
97
 
98
-
99
-
100
- # Serve index.html from the same directory as your main.py file
101
- from starlette.responses import FileResponse
102
 
103
  @app.get("/script1.js")
104
  async def script1_js():
@@ -118,4 +112,4 @@ async def read_index():
118
 
119
  if __name__ == "__main__":
120
  import uvicorn
121
- uvicorn.run(app, host="0.0.0.0", port=7068, reload=True)
 
11
  import os
12
  from pydantic import BaseModel
13
 
14
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", None)
15
 
16
  app = FastAPI()
17
 
 
34
  class StreamTextRequest(BaseModel):
35
  query: str
36
  history: str = "[]"
37
+ model: str = "gpt-4"
38
  api_key: str = None
39
 
40
  @app.post("/stream_text")
 
48
  return StreamingResponse(iter([f"{cached_response}"]), media_type='text/event-stream')
49
 
50
  # Model selection logic
51
+ fmodel = "gpt-4" if "gpt-4" in request.model else "gpt-3.5-turbo"
 
 
 
 
 
52
 
53
  system_message = """You are an AI language assistant. Your sole task is to correct the grammar, spelling, and structure of the sentences provided to you. You must not change the meaning of the sentences, and you should focus only on making them grammatically correct, concise, and clear. Do not add any additional information or provide explanations unless specifically asked. Your responses should be limited to the corrected version of the sentence."""
54
 
55
  messages = [{'role': 'system', 'content': system_message}]
 
56
  messages.extend(ast.literal_eval(request.history))
 
57
  messages.append({'role': 'user', 'content': request.query})
58
 
59
+ data = {'messages': messages, 'model': fmodel, 'stream': True}
 
 
60
 
61
+ api_key = request.api_key if request.api_key != 'none' else OPENAI_API_KEY
62
 
63
  async def stream_response():
64
  async with aiohttp.ClientSession() as session:
65
+ async with session.post(
66
+ 'https://api.openai.com/v1/chat/completions',
67
+ headers={'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json'},
68
+ json=data
69
+ ) as response:
70
  if response.status != 200:
71
  raise HTTPException(status_code=response.status, detail="Error fetching AI response")
72
 
 
91
 
92
  return StreamingResponse(stream_response(), media_type='text/event-stream')
93
 
94
+ # Serve static files
95
+ from starlette.responses import FileResponse
 
 
96
 
97
  @app.get("/script1.js")
98
  async def script1_js():
 
112
 
113
  if __name__ == "__main__":
114
  import uvicorn
115
+ uvicorn.run(app, host="0.0.0.0", port=7068, reload=True)