-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (52 loc) · 1.62 KB
/
main.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.data import Prompt, GenericResponse, Tables, AskResponse, ErrorResponse
from app.schema import Schema
from app.interaction import query_model
from app.parsers.parser import Parser
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
@app.get("/", summary="Health check")
def health():
return {
"message": "OK",
}
@app.get(
"/schema",
response_model=GenericResponse[Tables],
summary="Get the schema (deprecated)",
deprecated=True,
)
def schema():
return GenericResponse[Tables](message="Success", data=Schema.table_data)
@app.post(
"/ask",
response_model=AskResponse,
responses={
400: {
"model": ErrorResponse,
"description": "Bad request. Your request contains invalid group_by or the model response return invalid data. See `context` for detail.",
},
404: {
"model": ErrorResponse,
"description": "Data not found. Please review your request. See `context` for detail.",
},
},
summary="Ask the model",
description="Dear FE, 'mode', 'group_by', and 'page' is optional. Use 'page' if 'mode' is 'list'. Usually, 'mode' are inferred by the Model so you don't need to specify it.",
)
def ask(prompt: Prompt):
query = query_model(prompt.question)
parser = Parser(query)
result = parser.parse(
mode=prompt.mode,
group_by=prompt.group_by,
page=prompt.page,
)
return result