-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
executable file
·65 lines (53 loc) · 2.05 KB
/
Copy pathserve.py
File metadata and controls
executable file
·65 lines (53 loc) · 2.05 KB
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
# -*- coding: utf-8 -*-
"""
api server base on fastapi
/qa_poems
"""
import logging
import json
import traceback
import fastapi
import uvicorn
from fastapi.responses import Response, JSONResponse, StreamingResponse
from pydantic import BaseModel, Field
from rag_tang_poems import create_instance_by_qianfan_cloud, RagTangPoems
class QARequest(BaseModel):
question: str = Field(description='question to ask')
stream: bool = Field(default=False, description='streaming response')
def load_config(file='config.json'):
"""
load config from file
"""
with open(file, 'r', encoding='utf8') as f:
return json.load(f)
config = load_config()
rag: RagTangPoems = create_instance_by_qianfan_cloud(config)
app = fastapi.FastAPI()
@app.post("/qa_poems", response_class=Response)
async def qa_poems(question: QARequest):
"""
qa poems
"""
if not question.stream:
try:
answer = await rag.ainvoke(question.question)
except:
logging.error('process question failed: %s, %s' % (question.question, traceback.format_exc()))
answer = '我现在无法回答这个问题'
return JSONResponse(content={'content': answer}, media_type='application/json')
else:
async def stream():
try:
async for chunk in rag.astream(question.question):
if not chunk:
yield 'data: {"content": "", "finished": true, "finish_reason": "stop"}\n\n'
return
data = json.dumps({'content': chunk}, ensure_ascii=False)
yield 'data: %s\n\n' % data
except:
logging.error('process question failed: %s, %s' % (question.question, traceback.format_exc()))
yield 'data: {"content": "\n异常中断\n", "finish": true, "finish_reason": "exception"}\n\n'
return
return StreamingResponse(stream(), media_type='text/event-stream')
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8888, limit_concurrency=10)