From 3827c47515960aefff36bff443426fa80e708894 Mon Sep 17 00:00:00 2001 From: Stephen Hu Date: Thu, 8 May 2025 12:24:38 +0800 Subject: [PATCH] Feat: Add API to support get chunk by id (#7522) ### What problem does this PR solve? https://github.com/infiniflow/ragflow/issues/7519 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: Kevin Hu --- api/apps/api_app.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/apps/api_app.py b/api/apps/api_app.py index 5376ef0ed..27e1606de 100644 --- a/api/apps/api_app.py +++ b/api/apps/api_app.py @@ -345,7 +345,7 @@ def completion(): @manager.route('/conversation/', methods=['GET']) # noqa: F821 # @login_required -def get(conversation_id): +def get_conversation(conversation_id): token = request.headers.get('Authorization').split()[1] objs = APIToken.query(token=token) if not objs: @@ -548,6 +548,31 @@ def list_chunks(): return get_json_result(data=res) +@manager.route('/get_chunk/', methods=['GET']) # noqa: F821 +# @login_required +def get_chunk(chunk_id): + from rag.nlp import search + token = request.headers.get('Authorization').split()[1] + objs = APIToken.query(token=token) + if not objs: + return get_json_result( + data=False, message='Authentication error: API key is invalid!"', code=settings.RetCode.AUTHENTICATION_ERROR) + try: + tenant_id = objs[0].tenant_id + kb_ids = KnowledgebaseService.get_kb_ids(tenant_id) + chunk = settings.docStoreConn.get(chunk_id, search.index_name(tenant_id), kb_ids) + if chunk is None: + return server_error_response(Exception("Chunk not found")) + k = [] + for n in chunk.keys(): + if re.search(r"(_vec$|_sm_|_tks|_ltks)", n): + k.append(n) + for n in k: + del chunk[n] + + return get_json_result(data=chunk) + except Exception as e: + return server_error_response(e) @manager.route('/list_kb_docs', methods=['POST']) # noqa: F821 # @login_required