-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
360 lines (328 loc) · 11.6 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import json
from typing import Optional, Any, List, Annotated, Dict
import uvicorn
from fastapi import FastAPI, Header, Request
from starlette.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
import atexit
import logging
import os
from pymilvus import MilvusException
import pymilvus
from src.constants import (
ARGS_CONNECTION_ARGS,
ERROR_MILVUS_CONNECT_FAILED,
ERROR_MILVUS_UNKNOWN,
ERROR_OK,
ERROR_UNKNOW,
ERRSTR_MILVUS_CONNECT_FAILED,
)
from src.conversation_manager import chat, has_conversation, initialize_conversation
from src.datatypes import (
ChatCompletionsPostModel,
KgConnectionStatusPostModel,
RagAllDocumentsPostModel,
RagConnectionStatusPostModel,
RagDocumentDeleteModel,
RagNewDocumentPostModel,
)
from src.document_embedder import (
get_all_documents,
get_connection_status as get_vectorstore_connection_status,
new_embedder_document,
remove_document,
)
from src.kg_agent import get_connection_status as get_kg_connection_status
from src.utils import get_auth
from src.job_recycle_conversations import run_scheduled_job_continuously
# prepare logger
logging.basicConfig(level=logging.INFO)
file_handler = logging.FileHandler("./logs/app.log")
file_handler.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.addHandler(file_handler)
root_logger.addHandler(stream_handler)
logger = logging.getLogger(__name__)
# run scheduled job: recycle unused session
cease_event = run_scheduled_job_continuously()
def onExit():
cease_event.set()
atexit.register(onExit)
load_dotenv()
app = FastAPI(
# Initialize FastAPI cache with in-memory backend
title="Biochatter server API",
version="0.3.1",
description="API to interact with biochatter server",
debug=True,
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
DEFAULT_RAGCONFIG = {
"splitByChar": True,
"chunkSize": 1000,
"overlapSize": 0,
"resultNum": 3,
}
RAG_KG = "KG"
RAG_VECTORSTORE = "VS"
def process_connection_args(rag: str, connection_args: dict) -> dict:
if rag == RAG_VECTORSTORE:
if connection_args.get("host", "").lower() == "local":
connection_args["host"] = (
"127.0.0.1" if "HOST" not in os.environ else os.environ["HOST"]
)
port = connection_args.get("port", "19530")
connection_args["port"] = f"{port}"
elif rag == RAG_KG:
if connection_args.get("host", "").lower() == "local":
connection_args["host"] = (
"127.0.0.1" if "KGHOST" not in os.environ else os.environ["KGHOST"]
)
port = connection_args.get("port", "7687")
connection_args["port"] = f"{port}"
return connection_args
def extract_and_process_params_from_json_body(
json: Optional[Dict], name: str, defaultVal: Optional[Any]=None,
) -> Optional[Any]:
if not json:
return defaultVal
val = json.get(name, defaultVal)
return val
@app.post("/v1/chat/completions", description="chat completions")
async def handle(
authorization: Annotated[str | None, Header()],
# item: ChatCompletionsPostModel,
request: Request, # ChatCompletionsPostModel,
):
auth = get_auth(authorization)
jsonBody = await request.json()
sessionId = extract_and_process_params_from_json_body(
jsonBody, "session_id", defaultVal=""
)
messages = extract_and_process_params_from_json_body(
jsonBody, "messages", defaultVal=[]
)
model = extract_and_process_params_from_json_body(
jsonBody, "model", defaultVal="gpt-3.5-turbo"
)
temperature = extract_and_process_params_from_json_body(
jsonBody, "temperature", defaultVal=0.7
)
presence_penalty = extract_and_process_params_from_json_body(
jsonBody, "presence_penalty", defaultVal=0
)
frequency_penalty = extract_and_process_params_from_json_body(
jsonBody, "frequency_penalty", defaultVal=0
)
top_p = extract_and_process_params_from_json_body(jsonBody, "top_p", defaultVal=1)
ragConfig = extract_and_process_params_from_json_body(
jsonBody, "ragConfig", defaultVal=None
)
if ragConfig is not None:
ragConfig[ARGS_CONNECTION_ARGS] = process_connection_args(
RAG_VECTORSTORE, ragConfig[ARGS_CONNECTION_ARGS]
)
useRAG = extract_and_process_params_from_json_body(
jsonBody, "useRAG", defaultVal=False
)
kgConfig = extract_and_process_params_from_json_body(
jsonBody, "kgConfig", defaultVal=None
)
if kgConfig is not None:
kgConfig[ARGS_CONNECTION_ARGS] = process_connection_args(
RAG_KG, kgConfig[ARGS_CONNECTION_ARGS]
)
useKG = extract_and_process_params_from_json_body(
jsonBody, "useKG", defaultVal=False
)
oncokbConfig = extract_and_process_params_from_json_body(
jsonBody, "oncokbConfig", defaultVal=None
)
useAutoAgent = extract_and_process_params_from_json_body(
jsonBody, "useAutoAgent", defaultVal=False
)
if not has_conversation(sessionId):
initialize_conversation(
sessionId=sessionId,
modelConfig={
"temperature": temperature,
"presence_penalty": presence_penalty,
"frequency_penalty": frequency_penalty,
"top_p": top_p,
"model": model,
"auth": auth,
},
)
try:
(msg, usage, contexts) = chat(
sessionId=sessionId,
messages=messages,
authKey=auth,
ragConfig=ragConfig,
useRAG=useRAG,
kgConfig=kgConfig,
useKG=useKG,
oncokbConfig=oncokbConfig,
useAutoAgent=useAutoAgent,
)
return {
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": msg},
"finish_reason": "stop",
}
],
"usage": usage,
"contexts": contexts,
"code": ERROR_OK,
}
except MilvusException as e:
if e.code == pymilvus.Status.CONNECT_FAILED:
return {
"error": ERRSTR_MILVUS_CONNECT_FAILED,
"code": ERROR_MILVUS_CONNECT_FAILED,
}
else:
return {"error": e.message, "code": ERROR_MILVUS_UNKNOWN}
except Exception as e:
return {"error": str(e)}
@app.post("/v1/rag/newdocument", description="creates new document")
def newDocument(
authorization: Annotated[str | None, Header()],
item: RagNewDocumentPostModel
):
tmpFile = item.tmpFile
filename = item.filename
ragConfig = item.ragConfig
if type(ragConfig) is str:
ragConfig = json.loads(ragConfig)
ragConfig[ARGS_CONNECTION_ARGS] = process_connection_args(
RAG_VECTORSTORE, ragConfig[ARGS_CONNECTION_ARGS]
)
auth = get_auth(authorization)
# TODO: consider to be compatible with XinferenceDocumentEmbedder
try:
doc_id = new_embedder_document(
authKey=auth, tmpFile=tmpFile, filename=filename, rag_config=ragConfig
)
return {"id": doc_id, "code": ERROR_OK}
except MilvusException as e:
if e.code == pymilvus.Status.CONNECT_FAILED:
return {
"error": ERRSTR_MILVUS_CONNECT_FAILED,
"code": ERROR_MILVUS_CONNECT_FAILED,
}
else:
return {"error": e.message, "code": ERROR_MILVUS_UNKNOWN}
except Exception as e:
return {"error": str(e), "code": ERROR_UNKNOW}
@app.post("/v1/rag/alldocuments", description="retrieves all documents")
def getAllDocuments(
authorization: Annotated[str | None, Header()],
item: RagAllDocumentsPostModel,
):
def post_process(docs: List[Any]):
for doc in docs:
doc["id"] = str(doc["id"])
return docs
auth = get_auth(authorization)
connection_args = item.connectionArgs
connection_args = vars(connection_args)
connection_args = process_connection_args(RAG_VECTORSTORE, connection_args)
doc_ids = item.docIds
try:
docs = get_all_documents(auth, connection_args, doc_ids=doc_ids)
docs = post_process(docs)
return {"documents": docs, "code": ERROR_OK}
except MilvusException as e:
if e.code == pymilvus.Status.CONNECT_FAILED:
return {
"error": ERRSTR_MILVUS_CONNECT_FAILED,
"code": ERROR_MILVUS_CONNECT_FAILED,
}
else:
return {"error": e.message, "code": ERROR_MILVUS_UNKNOWN}
except Exception as e:
return {"error": str(e), "code": ERROR_UNKNOW}
@app.delete("/v1/rag/document", description="removes a document")
def removeDocument(
authorization: Annotated[str | None, Header()],
item: RagDocumentDeleteModel,
):
auth = get_auth(authorization)
docId = item.docId
connection_args = item.connectionArgs
connection_args = vars(connection_args)
connection_args = process_connection_args(RAG_VECTORSTORE, connection_args)
doc_ids = item.docIds
if len(docId) == 0:
return {"error": "Failed to find document"}
try:
remove_document(
docId, authKey=auth, connection_args=connection_args, doc_ids=doc_ids
)
return {"id": docId, "code": ERROR_OK}
except MilvusException as e:
if e.code == pymilvus.Status.CONNECT_FAILED:
return {
"error": ERRSTR_MILVUS_CONNECT_FAILED,
"code": ERROR_MILVUS_CONNECT_FAILED,
}
else:
return {"error": e.message, "code": ERROR_MILVUS_UNKNOWN}
except Exception as e:
return {"error": str(e), "code": ERROR_UNKNOW}
@app.post("/v1/rag/connectionstatus", description="returns connection status")
def getConnectionStatus(
authorization: Annotated[str | None, Header()],
item: RagConnectionStatusPostModel,
):
try:
auth = get_auth(authorization)
connection_args = item.connectionArgs
connection_args = vars(connection_args)
connection_args = process_connection_args(RAG_VECTORSTORE, connection_args)
connected = get_vectorstore_connection_status(connection_args, auth)
return {
"status": "connected" if connected else "disconnected",
"code": ERROR_OK,
}
except MilvusException as e:
return {"error": e.message, "code": ERROR_MILVUS_UNKNOWN}
except Exception as e:
return {"error": str(e), "code": ERROR_UNKNOW}
@app.post(
"/v1/kg/connectionstatus", description="returns knowledge graph connection status"
)
def getKGConnectionStatus(
item: KgConnectionStatusPostModel,
):
try:
connection_args = item.connectionArgs
connection_args = vars(connection_args)
connection_args = process_connection_args(RAG_KG, connection_args)
connected = get_kg_connection_status(connection_args)
return {
"status": "connected" if connected else "disconnected",
"code": ERROR_OK,
}
except Exception as e:
return {"error": str(e), "code": ERROR_UNKNOW}
if __name__ == "__main__":
port: int = 5001
uvicorn.run(app, host="0.0.0.0", port=port)