-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathwebsocket4modelcache.py
More file actions
58 lines (50 loc) · 2.01 KB
/
websocket4modelcache.py
File metadata and controls
58 lines (50 loc) · 2.01 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
# -*- coding: utf-8 -*-
from contextlib import asynccontextmanager
import uvicorn
import json
import asyncio
from fastapi import FastAPI, WebSocket
from starlette.websockets import WebSocketDisconnect
from modelcache.cache import Cache
from modelcache.embedding import EmbeddingModel
@asynccontextmanager
async def lifespan(app: FastAPI):
global cache
cache, _ = await Cache.init(
sql_storage="mysql",
vector_storage="milvus",
embedding_model=EmbeddingModel.HUGGINGFACE_ALL_MPNET_BASE_V2,
embedding_workers_num=8
)
yield
app = FastAPI(lifespan=lifespan)
cache: Cache = None
@app.websocket("/modelcache")
async def user_backend(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
asyncio.create_task(handle_message(websocket, data))
except WebSocketDisconnect as e:
print(e)
async def handle_message(websocket,message):
try:
param_dict = json.loads(message)
except Exception:
await websocket.send_json({"errorCode": 400, "errorDesc": "bad request", "cacheHit": False, "delta_time": 0, "hit_query": '', "answer": ''})
return
request_id = param_dict.get("requestId")
request_payload = param_dict.get("payload")
if not request_id or not request_payload:
await websocket.send_json({"errorCode": 400, "errorDesc": "bad request", "cacheHit": False, "delta_time": 0, "hit_query": '', "answer": ''})
return
try:
result = await cache.handle_request(request_payload)
await websocket.send_json({"requestId": request_id,"result": result})
except Exception as e:
error_result = {"errorCode": 500, "errorDesc": str(e), "cacheHit": False, "delta_time": 0, "hit_query": '', "answer": ''}
cache.save_query_resp(error_result, model='', query='', delta_time=0)
await websocket.send_json(error_result)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=5000, loop="asyncio", http="httptools")