This repository has been archived by the owner on Jul 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (60 loc) · 1.95 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
64
65
66
67
68
69
70
71
72
73
import datetime
import uuid
from fastapi import FastAPI, Body
from fastapi.middleware.cors import CORSMiddleware
from func.chat import chat_with_history
from func.format_dataset import format_dataset
from type import RAW_DATASET, CHAT_DATA
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
datasets = {}
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
@app.post("/dataset/{dataset_id}")
async def create_dataset(dataset_id: str, dataset: RAW_DATASET = Body(...)):
try:
data = format_dataset(dataset)
datasets[dataset_id] = data
return {"data": {"message": f"Dataset {dataset_id} created", "statue": 1}}
except Exception as e:
return {
"data": {"message": f"Fail to create Dataset {dataset_id}", "statue": 0}
}
@app.post("/chat/{chat_id}")
async def get_chat(chat_id: str, chat_data: CHAT_DATA = Body(...)):
try:
if chat_data["dataset_id"] not in datasets.keys():
return {
"data": {
"message": f"Dataset {chat_data['dataset_id']} not found",
"statue": -1,
}
}
res = chat_with_history(
chat_data["content"],
datasets[chat_data["dataset_id"]]["content"],
chat_data["history"],
chat_data["sys_name"],
)
res_id = str(uuid.uuid4())
return {
"data": {
"message": f"Chat {chat_id} answered",
"statue": 1,
"id": res_id,
"content": res,
"date": str(int(datetime.datetime.now().timestamp())),
}
}
except Exception as e:
return {"data": {"message": f"Fail to answer Chat {chat_id}", "statue": 0}}