forked from zilliztech/akcio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (48 loc) · 2.24 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
import argparse
import uvicorn
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
# Specify mode
parser = argparse.ArgumentParser(description='Start service with different modes.')
parser.add_argument('--langchain', action='store_true')
parser.add_argument('--towhee', action='store_true')
args = parser.parse_args()
USE_LANGCHAIN = args.langchain
USE_TOWHEE = args.towhee
assert (USE_LANGCHAIN and not USE_TOWHEE ) or (USE_TOWHEE and not USE_LANGCHAIN), \
'The service should start with either "--langchain" or "--towhee".'
if USE_LANGCHAIN:
from src_langchain.operations import chat, insert, drop # pylint: disable=C0413
if USE_TOWHEE:
from src_towhee.operations import chat, insert, drop # pylint: disable=C0413
app = FastAPI()
origins = ['*']
@app.get('/')
def check_api():
return jsonable_encoder({'status': True, 'msg': 'ok'}), 200
@app.get('/answer')
def do_answer_api(session_id: str, project: str, question: str):
try:
final_answer = chat(session_id=session_id,
project=project, question=question)
assert isinstance(final_answer, str)
return jsonable_encoder({'status': True, 'msg': final_answer}), 200
except Exception as e: # pylint: disable=W0703
return jsonable_encoder({'status': False, 'msg': f'Failed to answer question:\n{e}', 'code': 400}), 400
@app.post('/project/add')
def do_project_add_api(data_src: str, project: str, source_type: str = 'file'):
try:
num = insert(data_src=data_src, project=project, source_type=source_type)
return jsonable_encoder({'status': True, 'msg': f'Successfully inserted doc chunks: {num}'}), 200
except Exception as e: # pylint: disable=W0703
return jsonable_encoder({'status': False, 'msg': f'Failed to load data:\n{e}'}), 400
@app.post('/project/drop')
def do_project_drop_api(project: str):
# Drop data in vector db
try:
drop(project=project)
return jsonable_encoder({'status': True, 'msg': f'Dropped project: {project}'}), 200
except Exception as e: # pylint: disable=W0703
return jsonable_encoder({'status': False, 'msg': f'Failed to drop project:\n{e}'}), 400
if __name__ == '__main__':
uvicorn.run(app=app, host='0.0.0.0', port=8900)