-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (28 loc) · 968 Bytes
/
server.py
File metadata and controls
32 lines (28 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import FastAPI, Response, status
from pydantic import BaseModel
from config import settings
from utils import ModelName, selectModel, send_rest_api_req
class Prompt(BaseModel):
question: str
context: str
model: ModelName = ModelName.distilbert
app = FastAPI()
@app.get("/")
def home_route():
return {"Message": "welcome to bons.ai"}
@app.post("/prompt")
async def prompt_llm(prompt: Prompt, response: Response):
context = prompt.context
question = prompt.question
headers = {"Authorization": f"Bearer {settings.api_token}"}
payload = {
"inputs": {
"question": f"{question}",
"context": f"{context}",
}
}
api_url = selectModel(prompt.model)
data = await send_rest_api_req(payload= payload, headers= headers, api_url= api_url)
if data.status_code == 503:
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return data.content