From 1d65299791ae086602b4efad0efc59171334f93b Mon Sep 17 00:00:00 2001 From: liuhua <10215101452@stu.ecnu.edu.cn> Date: Tue, 17 Dec 2024 16:03:37 +0800 Subject: [PATCH] Fix rerank_model bug in chat and markdown bug (#4061) ### What problem does this PR solve? Fix rerank_model bug in chat and markdown bug #4000 #3992 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --------- Co-authored-by: liuhua <10215101452@stu.ecun.edu.cn> --- api/apps/sdk/chat.py | 12 +++++++----- docs/references/http_api_reference.md | 6 +++--- docs/references/python_api_reference.md | 2 +- rag/app/naive.py | 6 ++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/apps/sdk/chat.py b/api/apps/sdk/chat.py index 784578521..5f7aa6a5e 100644 --- a/api/apps/sdk/chat.py +++ b/api/apps/sdk/chat.py @@ -82,7 +82,8 @@ def create(tenant_id): req["top_k"] = req.get("top_k", 1024) req["rerank_id"] = req.get("rerank_id", "") if req.get("rerank_id"): - if not TenantLLMService.query(tenant_id=tenant_id,llm_name=req.get("rerank_id"),model_type="rerank"): + value_rerank_model = ["BAAI/bge-reranker-v2-m3","maidalun1020/bce-reranker-base_v1"] + if req["rerank_id"] not in value_rerank_model and not TenantLLMService.query(tenant_id=tenant_id,llm_name=req.get("rerank_id"),model_type="rerank"): return get_error_data_result(f"`rerank_model` {req.get('rerank_id')} doesn't exist") if not req.get("llm_id"): req["llm_id"] = tenant.llm_id @@ -161,7 +162,7 @@ def update(tenant_id,chat_id): req["do_refer"]=req.pop("show_quotation") if "dataset_ids" in req: if not ids: - return get_error_data_result("`datasets` can't be empty") + return get_error_data_result("`dataset_ids` can't be empty") if ids: for kb_id in ids: kbs = KnowledgebaseService.accessible(kb_id=kb_id, user_id=tenant_id) @@ -188,9 +189,6 @@ def update(tenant_id,chat_id): e, tenant = TenantService.get_by_id(tenant_id) if not e: return get_error_data_result(message="Tenant not found!") - if req.get("rerank_model"): - if not TenantLLMService.query(tenant_id=tenant_id,llm_name=req.get("rerank_model"),model_type="rerank"): - return get_error_data_result(f"`rerank_model` {req.get('rerank_model')} doesn't exist") # prompt prompt = req.get("prompt") key_mapping = {"parameters": "variables", @@ -210,6 +208,10 @@ def update(tenant_id,chat_id): req["prompt_config"] = req.pop("prompt") e, res = DialogService.get_by_id(chat_id) res = res.to_json() + if req.get("rerank_id"): + value_rerank_model = ["BAAI/bge-reranker-v2-m3","maidalun1020/bce-reranker-base_v1"] + if req["rerank_id"] not in value_rerank_model and not TenantLLMService.query(tenant_id=tenant_id,llm_name=req.get("rerank_id"),model_type="rerank"): + return get_error_data_result(f"`rerank_model` {req.get('rerank_id')} doesn't exist") if "name" in req: if not req.get("name"): return get_error_data_result(message="`name` is not empty.") diff --git a/docs/references/http_api_reference.md b/docs/references/http_api_reference.md index 10505402d..64901b5b7 100644 --- a/docs/references/http_api_reference.md +++ b/docs/references/http_api_reference.md @@ -1380,7 +1380,7 @@ curl --request POST \ - `"frequency penalty"`: `float` Similar to the presence penalty, this reduces the model’s tendency to repeat the same words frequently. Defaults to `0.7`. - `"max_token"`: `integer` - The maximum length of the model's output, measured in the number of tokens (words or pieces of words). If disabled, you lift the maximum token limit, allowing the model to determine the number of tokens in its responses. Defaults to `512`. + The maximum length of the model's output, measured in the number of tokens (words or pieces of words). If disabled, you lift the maximum token limit, allowing the model to determine the number of tokens in its responses. Defaults to `512`. - `"prompt"`: (*Body parameter*), `object` Instructions for the LLM to follow. If it is not explicitly set, a JSON object with the following values will be generated as the default. A `prompt` JSON object contains the following attributes: - `"similarity_threshold"`: `float` RAGFlow employs either a combination of weighted keyword similarity and weighted vector cosine similarity, or a combination of weighted keyword similarity and weighted reranking score during retrieval. This argument sets the threshold for similarities between the user query and chunks. If a similarity score falls below this threshold, the corresponding chunk will be excluded from the results. The default value is `0.2`. @@ -2243,7 +2243,7 @@ Success: "data": { "form": {}, "label": "Answer", - "name": "对话_0" + "name": "dialog_0" }, "dragging": false, "height": 44, @@ -2362,7 +2362,7 @@ curl --request POST \ --data-binary ' { "lang":"English" - "file":"明天天气如何" + "file":"How is the weather tomorrow?" }' ``` diff --git a/docs/references/python_api_reference.md b/docs/references/python_api_reference.md index f1ac5ce05..6cb068902 100644 --- a/docs/references/python_api_reference.md +++ b/docs/references/python_api_reference.md @@ -1435,7 +1435,7 @@ session = create_session(AGENT_ID,rag_object) --- -## Converse with agent without `begin` component +## Converse with agent ```python Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]] diff --git a/rag/app/naive.py b/rag/app/naive.py index f9f1ca879..fac626ab0 100644 --- a/rag/app/naive.py +++ b/rag/app/naive.py @@ -169,11 +169,13 @@ class Markdown(MarkdownParser): sections = [] tbls = [] for sec in remainder.split("\n"): - if num_tokens_from_string(sec) > 10 * self.chunk_token_num: + if num_tokens_from_string(sec) > 3 * self.chunk_token_num: sections.append((sec[:int(len(sec) / 2)], "")) sections.append((sec[int(len(sec) / 2):], "")) else: - if sections and sections[-1][0].strip().find("#") == 0: + if sec.strip().find("#") == 0: + sections.append((sec, "")) + elif sections and sections[-1][0].strip().find("#") == 0: sec_, _ = sections.pop(-1) sections.append((sec_ + "\n" + sec, "")) else: