Commit
·
274a893
1
Parent(s):
8e24bf0
Fix bugs (#4241)
Browse files### What problem does this PR solve?
1. Refactor error message
2. Fix knowledges are created on ES and can't be found in Infinity. The
document chunk fetch error.
### Type of change
- [x] Fix bug
- [x] Refactoring
---------
Signed-off-by: jinhai <[email protected]>
- api/apps/api_app.py +12 -12
- api/apps/canvas_app.py +1 -1
- api/apps/conversation_app.py +1 -1
- api/apps/sdk/session.py +2 -2
- api/utils/api_utils.py +1 -1
- rag/utils/infinity_conn.py +14 -6
api/apps/api_app.py
CHANGED
|
@@ -141,7 +141,7 @@ def set_conversation():
|
|
| 141 |
objs = APIToken.query(token=token)
|
| 142 |
if not objs:
|
| 143 |
return get_json_result(
|
| 144 |
-
data=False, message='
|
| 145 |
try:
|
| 146 |
if objs[0].source == "agent":
|
| 147 |
e, cvs = UserCanvasService.get_by_id(objs[0].dialog_id)
|
|
@@ -182,7 +182,7 @@ def completion():
|
|
| 182 |
objs = APIToken.query(token=token)
|
| 183 |
if not objs:
|
| 184 |
return get_json_result(
|
| 185 |
-
data=False, message='
|
| 186 |
req = request.json
|
| 187 |
e, conv = API4ConversationService.get_by_id(req["conversation_id"])
|
| 188 |
if not e:
|
|
@@ -348,7 +348,7 @@ def get(conversation_id):
|
|
| 348 |
objs = APIToken.query(token=token)
|
| 349 |
if not objs:
|
| 350 |
return get_json_result(
|
| 351 |
-
data=False, message='
|
| 352 |
|
| 353 |
try:
|
| 354 |
e, conv = API4ConversationService.get_by_id(conversation_id)
|
|
@@ -357,7 +357,7 @@ def get(conversation_id):
|
|
| 357 |
|
| 358 |
conv = conv.to_dict()
|
| 359 |
if token != APIToken.query(dialog_id=conv['dialog_id'])[0].token:
|
| 360 |
-
return get_json_result(data=False, message='
|
| 361 |
code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 362 |
|
| 363 |
for referenct_i in conv['reference']:
|
|
@@ -379,7 +379,7 @@ def upload():
|
|
| 379 |
objs = APIToken.query(token=token)
|
| 380 |
if not objs:
|
| 381 |
return get_json_result(
|
| 382 |
-
data=False, message='
|
| 383 |
|
| 384 |
kb_name = request.form.get("kb_name").strip()
|
| 385 |
tenant_id = objs[0].tenant_id
|
|
@@ -491,7 +491,7 @@ def upload_parse():
|
|
| 491 |
objs = APIToken.query(token=token)
|
| 492 |
if not objs:
|
| 493 |
return get_json_result(
|
| 494 |
-
data=False, message='
|
| 495 |
|
| 496 |
if 'file' not in request.files:
|
| 497 |
return get_json_result(
|
|
@@ -514,7 +514,7 @@ def list_chunks():
|
|
| 514 |
objs = APIToken.query(token=token)
|
| 515 |
if not objs:
|
| 516 |
return get_json_result(
|
| 517 |
-
data=False, message='
|
| 518 |
|
| 519 |
req = request.json
|
| 520 |
|
|
@@ -554,7 +554,7 @@ def list_kb_docs():
|
|
| 554 |
objs = APIToken.query(token=token)
|
| 555 |
if not objs:
|
| 556 |
return get_json_result(
|
| 557 |
-
data=False, message='
|
| 558 |
|
| 559 |
req = request.json
|
| 560 |
tenant_id = objs[0].tenant_id
|
|
@@ -594,7 +594,7 @@ def docinfos():
|
|
| 594 |
objs = APIToken.query(token=token)
|
| 595 |
if not objs:
|
| 596 |
return get_json_result(
|
| 597 |
-
data=False, message='
|
| 598 |
req = request.json
|
| 599 |
doc_ids = req["doc_ids"]
|
| 600 |
docs = DocumentService.get_by_ids(doc_ids)
|
|
@@ -608,7 +608,7 @@ def document_rm():
|
|
| 608 |
objs = APIToken.query(token=token)
|
| 609 |
if not objs:
|
| 610 |
return get_json_result(
|
| 611 |
-
data=False, message='
|
| 612 |
|
| 613 |
tenant_id = objs[0].tenant_id
|
| 614 |
req = request.json
|
|
@@ -670,7 +670,7 @@ def completion_faq():
|
|
| 670 |
objs = APIToken.query(token=token)
|
| 671 |
if not objs:
|
| 672 |
return get_json_result(
|
| 673 |
-
data=False, message='
|
| 674 |
|
| 675 |
e, conv = API4ConversationService.get_by_id(req["conversation_id"])
|
| 676 |
if not e:
|
|
@@ -809,7 +809,7 @@ def retrieval():
|
|
| 809 |
objs = APIToken.query(token=token)
|
| 810 |
if not objs:
|
| 811 |
return get_json_result(
|
| 812 |
-
data=False, message='
|
| 813 |
|
| 814 |
req = request.json
|
| 815 |
kb_ids = req.get("kb_id", [])
|
|
|
|
| 141 |
objs = APIToken.query(token=token)
|
| 142 |
if not objs:
|
| 143 |
return get_json_result(
|
| 144 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 145 |
try:
|
| 146 |
if objs[0].source == "agent":
|
| 147 |
e, cvs = UserCanvasService.get_by_id(objs[0].dialog_id)
|
|
|
|
| 182 |
objs = APIToken.query(token=token)
|
| 183 |
if not objs:
|
| 184 |
return get_json_result(
|
| 185 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 186 |
req = request.json
|
| 187 |
e, conv = API4ConversationService.get_by_id(req["conversation_id"])
|
| 188 |
if not e:
|
|
|
|
| 348 |
objs = APIToken.query(token=token)
|
| 349 |
if not objs:
|
| 350 |
return get_json_result(
|
| 351 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 352 |
|
| 353 |
try:
|
| 354 |
e, conv = API4ConversationService.get_by_id(conversation_id)
|
|
|
|
| 357 |
|
| 358 |
conv = conv.to_dict()
|
| 359 |
if token != APIToken.query(dialog_id=conv['dialog_id'])[0].token:
|
| 360 |
+
return get_json_result(data=False, message='Authentication error: API key is invalid for this conversation_id!"',
|
| 361 |
code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 362 |
|
| 363 |
for referenct_i in conv['reference']:
|
|
|
|
| 379 |
objs = APIToken.query(token=token)
|
| 380 |
if not objs:
|
| 381 |
return get_json_result(
|
| 382 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 383 |
|
| 384 |
kb_name = request.form.get("kb_name").strip()
|
| 385 |
tenant_id = objs[0].tenant_id
|
|
|
|
| 491 |
objs = APIToken.query(token=token)
|
| 492 |
if not objs:
|
| 493 |
return get_json_result(
|
| 494 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 495 |
|
| 496 |
if 'file' not in request.files:
|
| 497 |
return get_json_result(
|
|
|
|
| 514 |
objs = APIToken.query(token=token)
|
| 515 |
if not objs:
|
| 516 |
return get_json_result(
|
| 517 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 518 |
|
| 519 |
req = request.json
|
| 520 |
|
|
|
|
| 554 |
objs = APIToken.query(token=token)
|
| 555 |
if not objs:
|
| 556 |
return get_json_result(
|
| 557 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 558 |
|
| 559 |
req = request.json
|
| 560 |
tenant_id = objs[0].tenant_id
|
|
|
|
| 594 |
objs = APIToken.query(token=token)
|
| 595 |
if not objs:
|
| 596 |
return get_json_result(
|
| 597 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 598 |
req = request.json
|
| 599 |
doc_ids = req["doc_ids"]
|
| 600 |
docs = DocumentService.get_by_ids(doc_ids)
|
|
|
|
| 608 |
objs = APIToken.query(token=token)
|
| 609 |
if not objs:
|
| 610 |
return get_json_result(
|
| 611 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 612 |
|
| 613 |
tenant_id = objs[0].tenant_id
|
| 614 |
req = request.json
|
|
|
|
| 670 |
objs = APIToken.query(token=token)
|
| 671 |
if not objs:
|
| 672 |
return get_json_result(
|
| 673 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 674 |
|
| 675 |
e, conv = API4ConversationService.get_by_id(req["conversation_id"])
|
| 676 |
if not e:
|
|
|
|
| 809 |
objs = APIToken.query(token=token)
|
| 810 |
if not objs:
|
| 811 |
return get_json_result(
|
| 812 |
+
data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR)
|
| 813 |
|
| 814 |
req = request.json
|
| 815 |
kb_ids = req.get("kb_id", [])
|
api/apps/canvas_app.py
CHANGED
|
@@ -94,7 +94,7 @@ def getsse(canvas_id):
|
|
| 94 |
token = token[1]
|
| 95 |
objs = APIToken.query(beta=token)
|
| 96 |
if not objs:
|
| 97 |
-
return get_data_error_result(message='
|
| 98 |
e, c = UserCanvasService.get_by_id(canvas_id)
|
| 99 |
if not e:
|
| 100 |
return get_data_error_result(message="canvas not found.")
|
|
|
|
| 94 |
token = token[1]
|
| 95 |
objs = APIToken.query(beta=token)
|
| 96 |
if not objs:
|
| 97 |
+
return get_data_error_result(message='Authentication error: API key is invalid!"')
|
| 98 |
e, c = UserCanvasService.get_by_id(canvas_id)
|
| 99 |
if not e:
|
| 100 |
return get_data_error_result(message="canvas not found.")
|
api/apps/conversation_app.py
CHANGED
|
@@ -126,7 +126,7 @@ def getsse(dialog_id):
|
|
| 126 |
token = token[1]
|
| 127 |
objs = APIToken.query(beta=token)
|
| 128 |
if not objs:
|
| 129 |
-
return get_data_error_result(message='
|
| 130 |
try:
|
| 131 |
e, conv = DialogService.get_by_id(dialog_id)
|
| 132 |
if not e:
|
|
|
|
| 126 |
token = token[1]
|
| 127 |
objs = APIToken.query(beta=token)
|
| 128 |
if not objs:
|
| 129 |
+
return get_data_error_result(message='Authentication error: API key is invalid!"')
|
| 130 |
try:
|
| 131 |
e, conv = DialogService.get_by_id(dialog_id)
|
| 132 |
if not e:
|
api/apps/sdk/session.py
CHANGED
|
@@ -405,7 +405,7 @@ def chatbot_completions(dialog_id):
|
|
| 405 |
token = token[1]
|
| 406 |
objs = APIToken.query(beta=token)
|
| 407 |
if not objs:
|
| 408 |
-
return get_error_data_result(message='
|
| 409 |
|
| 410 |
if "quote" not in req:
|
| 411 |
req["quote"] = False
|
|
@@ -432,7 +432,7 @@ def agent_bot_completions(agent_id):
|
|
| 432 |
token = token[1]
|
| 433 |
objs = APIToken.query(beta=token)
|
| 434 |
if not objs:
|
| 435 |
-
return get_error_data_result(message='
|
| 436 |
|
| 437 |
if "quote" not in req:
|
| 438 |
req["quote"] = False
|
|
|
|
| 405 |
token = token[1]
|
| 406 |
objs = APIToken.query(beta=token)
|
| 407 |
if not objs:
|
| 408 |
+
return get_error_data_result(message='Authentication error: API key is invalid!"')
|
| 409 |
|
| 410 |
if "quote" not in req:
|
| 411 |
req["quote"] = False
|
|
|
|
| 432 |
token = token[1]
|
| 433 |
objs = APIToken.query(beta=token)
|
| 434 |
if not objs:
|
| 435 |
+
return get_error_data_result(message='Authentication error: API key is invalid!"')
|
| 436 |
|
| 437 |
if "quote" not in req:
|
| 438 |
req["quote"] = False
|
api/utils/api_utils.py
CHANGED
|
@@ -293,7 +293,7 @@ def token_required(func):
|
|
| 293 |
objs = APIToken.query(token=token)
|
| 294 |
if not objs:
|
| 295 |
return get_json_result(
|
| 296 |
-
data=False, message='
|
| 297 |
)
|
| 298 |
kwargs['tenant_id'] = objs[0].tenant_id
|
| 299 |
return func(*args, **kwargs)
|
|
|
|
| 293 |
objs = APIToken.query(token=token)
|
| 294 |
if not objs:
|
| 295 |
return get_json_result(
|
| 296 |
+
data=False, message='Authentication error: API key is invalid!', code=settings.RetCode.AUTHENTICATION_ERROR
|
| 297 |
)
|
| 298 |
kwargs['tenant_id'] = objs[0].tenant_id
|
| 299 |
return func(*args, **kwargs)
|
rag/utils/infinity_conn.py
CHANGED
|
@@ -26,7 +26,8 @@ from rag.utils.doc_store_conn import (
|
|
| 26 |
|
| 27 |
logger = logging.getLogger('ragflow.infinity_conn')
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
assert "_id" not in condition
|
| 31 |
cond = list()
|
| 32 |
for k, v in condition.items():
|
|
@@ -59,12 +60,13 @@ def concat_dataframes(df_list: list[pl.DataFrame], selectFields: list[str]) -> p
|
|
| 59 |
return pl.concat(df_list)
|
| 60 |
schema = dict()
|
| 61 |
for field_name in selectFields:
|
| 62 |
-
if field_name == 'score()':
|
| 63 |
schema['SCORE'] = str
|
| 64 |
else:
|
| 65 |
schema[field_name] = str
|
| 66 |
return pl.DataFrame(schema=schema)
|
| 67 |
|
|
|
|
| 68 |
@singleton
|
| 69 |
class InfinityConnection(DocStoreConnection):
|
| 70 |
def __init__(self):
|
|
@@ -80,7 +82,7 @@ class InfinityConnection(DocStoreConnection):
|
|
| 80 |
connPool = ConnectionPool(infinity_uri)
|
| 81 |
inf_conn = connPool.get_conn()
|
| 82 |
res = inf_conn.show_current_node()
|
| 83 |
-
if res.error_code == ErrorCode.OK and res.server_status=="started":
|
| 84 |
self._migrate_db(inf_conn)
|
| 85 |
self.connPool = connPool
|
| 86 |
connPool.release_conn(inf_conn)
|
|
@@ -360,7 +362,13 @@ class InfinityConnection(DocStoreConnection):
|
|
| 360 |
for knowledgebaseId in knowledgebaseIds:
|
| 361 |
table_name = f"{indexName}_{knowledgebaseId}"
|
| 362 |
table_list.append(table_name)
|
| 363 |
-
table_instance =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
kb_res, _ = table_instance.output(["*"]).filter(f"id = '{chunkId}'").to_pl()
|
| 365 |
logger.debug(f"INFINITY get table: {str(table_list)}, result: {str(kb_res)}")
|
| 366 |
df_list.append(kb_res)
|
|
@@ -403,7 +411,7 @@ class InfinityConnection(DocStoreConnection):
|
|
| 403 |
d[k] = "###".join(v)
|
| 404 |
elif k == 'kb_id':
|
| 405 |
if isinstance(d[k], list):
|
| 406 |
-
d[k] = d[k][0]
|
| 407 |
elif k == "position_int":
|
| 408 |
assert isinstance(v, list)
|
| 409 |
arr = [num for row in v for num in row]
|
|
@@ -440,7 +448,7 @@ class InfinityConnection(DocStoreConnection):
|
|
| 440 |
newValue[k] = " ".join(v)
|
| 441 |
elif k == 'kb_id':
|
| 442 |
if isinstance(newValue[k], list):
|
| 443 |
-
newValue[k] = newValue[k][0]
|
| 444 |
elif k == "position_int":
|
| 445 |
assert isinstance(v, list)
|
| 446 |
arr = [num for row in v for num in row]
|
|
|
|
| 26 |
|
| 27 |
logger = logging.getLogger('ragflow.infinity_conn')
|
| 28 |
|
| 29 |
+
|
| 30 |
+
def equivalent_condition_to_str(condition: dict) -> str | None:
|
| 31 |
assert "_id" not in condition
|
| 32 |
cond = list()
|
| 33 |
for k, v in condition.items():
|
|
|
|
| 60 |
return pl.concat(df_list)
|
| 61 |
schema = dict()
|
| 62 |
for field_name in selectFields:
|
| 63 |
+
if field_name == 'score()': # Workaround: fix schema is changed to score()
|
| 64 |
schema['SCORE'] = str
|
| 65 |
else:
|
| 66 |
schema[field_name] = str
|
| 67 |
return pl.DataFrame(schema=schema)
|
| 68 |
|
| 69 |
+
|
| 70 |
@singleton
|
| 71 |
class InfinityConnection(DocStoreConnection):
|
| 72 |
def __init__(self):
|
|
|
|
| 82 |
connPool = ConnectionPool(infinity_uri)
|
| 83 |
inf_conn = connPool.get_conn()
|
| 84 |
res = inf_conn.show_current_node()
|
| 85 |
+
if res.error_code == ErrorCode.OK and res.server_status == "started":
|
| 86 |
self._migrate_db(inf_conn)
|
| 87 |
self.connPool = connPool
|
| 88 |
connPool.release_conn(inf_conn)
|
|
|
|
| 362 |
for knowledgebaseId in knowledgebaseIds:
|
| 363 |
table_name = f"{indexName}_{knowledgebaseId}"
|
| 364 |
table_list.append(table_name)
|
| 365 |
+
table_instance = None
|
| 366 |
+
try:
|
| 367 |
+
table_instance = db_instance.get_table(table_name)
|
| 368 |
+
except Exception:
|
| 369 |
+
logger.warning(
|
| 370 |
+
f"Table not found: {table_name}, this knowledge base isn't created in Infinity. Maybe it is created in other document engine.")
|
| 371 |
+
continue
|
| 372 |
kb_res, _ = table_instance.output(["*"]).filter(f"id = '{chunkId}'").to_pl()
|
| 373 |
logger.debug(f"INFINITY get table: {str(table_list)}, result: {str(kb_res)}")
|
| 374 |
df_list.append(kb_res)
|
|
|
|
| 411 |
d[k] = "###".join(v)
|
| 412 |
elif k == 'kb_id':
|
| 413 |
if isinstance(d[k], list):
|
| 414 |
+
d[k] = d[k][0] # since d[k] is a list, but we need a str
|
| 415 |
elif k == "position_int":
|
| 416 |
assert isinstance(v, list)
|
| 417 |
arr = [num for row in v for num in row]
|
|
|
|
| 448 |
newValue[k] = " ".join(v)
|
| 449 |
elif k == 'kb_id':
|
| 450 |
if isinstance(newValue[k], list):
|
| 451 |
+
newValue[k] = newValue[k][0] # since d[k] is a list, but we need a str
|
| 452 |
elif k == "position_int":
|
| 453 |
assert isinstance(v, list)
|
| 454 |
arr = [num for row in v for num in row]
|