Skip to content

Commit

Permalink
Fix some issues in API (infiniflow#2902)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

Fix some issues in API

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Co-authored-by: liuhua <[email protected]>
  • Loading branch information
Feiue and liuhua authored Oct 21, 2024
1 parent 609cfa7 commit 1935c3b
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 162 deletions.
51 changes: 25 additions & 26 deletions api/apps/sdk/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@
@token_required
def create(tenant_id):
req=request.json
if not req.get("knowledgebases"):
return get_error_data_result(retmsg="knowledgebases are required")
kb_list = []
for kb in req.get("knowledgebases"):
if not kb["id"]:
return get_error_data_result(retmsg="knowledgebase needs id")
if not KnowledgebaseService.query(id=kb["id"], tenant_id=tenant_id):
return get_error_data_result(retmsg="you do not own the knowledgebase")
# if not DocumentService.query(kb_id=kb["id"]):
# return get_error_data_result(retmsg="There is a invalid knowledgebase")
kb_list.append(kb["id"])
req["kb_ids"] = kb_list
ids= req.get("knowledgebases")
if not ids:
return get_error_data_result(retmsg="`knowledgebases` is required")
for kb_id in ids:
kbs = KnowledgebaseService.query(id=kb_id,tenant_id=tenant_id)
if not kbs:
return get_error_data_result(f"You don't own the dataset {kb_id}")
kb=kbs[0]
if kb.chunk_num == 0:
return get_error_data_result(f"The dataset {kb_id} doesn't own parsed file")
req["kb_ids"] = ids
# llm
llm = req.get("llm")
if llm:
Expand Down Expand Up @@ -81,24 +80,24 @@ def create(tenant_id):
else:
req["llm_id"] = tenant.llm_id
if not req.get("name"):
return get_error_data_result(retmsg="name is required.")
return get_error_data_result(retmsg="`name` is required.")
if DialogService.query(name=req["name"], tenant_id=tenant_id, status=StatusEnum.VALID.value):
return get_error_data_result(retmsg="Duplicated chat name in creating dataset.")
return get_error_data_result(retmsg="Duplicated chat name in creating chat.")
# tenant_id
if req.get("tenant_id"):
return get_error_data_result(retmsg="tenant_id must not be provided.")
return get_error_data_result(retmsg="`tenant_id` must not be provided.")
req["tenant_id"] = tenant_id
# prompt more parameter
default_prompt = {
"system": """你是一个智能助手,请总结知识库的内容来回答问题,请列举知识库中的数据详细回答。当所有知识库内容都与问题无关时,你的回答必须包括“知识库中未找到您要的答案!”这句话。回答需要考虑聊天历史。
以下是知识库:
{knowledge}
以上是知识库。""",
"prologue": "您好,我是您的助手小樱,长得可爱又善良,can I help you?",
"system": """You are an intelligent assistant. Please summarize the content of the knowledge base to answer the question. Please list the data in the knowledge base and answer in detail. When all knowledge base content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the knowledge base!" Answers need to consider chat history.
Here is the knowledge base:
{knowledge}
The above is the knowledge base.""",
"prologue": "Hi! I'm your assistant, what can I do for you?",
"parameters": [
{"key": "knowledge", "optional": False}
],
"empty_response": "Sorry! 知识库中未找到相关内容!"
"empty_response": "Sorry! No relevant content was found in the knowledge base!"
}
key_list_2 = ["system", "prologue", "parameters", "empty_response"]
if "prompt_config" not in req:
Expand Down Expand Up @@ -149,7 +148,7 @@ def update(tenant_id,chat_id):
req =request.json
if "knowledgebases" in req:
if not req.get("knowledgebases"):
return get_error_data_result(retmsg="knowledgebases can't be empty value")
return get_error_data_result(retmsg="`knowledgebases` can't be empty value")
kb_list = []
for kb in req.get("knowledgebases"):
if not kb["id"]:
Expand Down Expand Up @@ -189,10 +188,10 @@ def update(tenant_id,chat_id):
res = res.to_json()
if "llm_id" in req:
if not TenantLLMService.query(llm_name=req["llm_id"]):
return get_error_data_result(retmsg="the model_name does not exist.")
return get_error_data_result(retmsg="The `model_name` does not exist.")
if "name" in req:
if not req.get("name"):
return get_error_data_result(retmsg="name is not empty.")
return get_error_data_result(retmsg="`name` is not empty.")
if req["name"].lower() != res["name"].lower() \
and len(
DialogService.query(name=req["name"], tenant_id=tenant_id, status=StatusEnum.VALID.value)) > 0:
Expand Down Expand Up @@ -224,7 +223,7 @@ def delete(tenant_id):
req = request.json
ids = req.get("ids")
if not ids:
return get_error_data_result(retmsg="ids are required")
return get_error_data_result(retmsg="`ids` are required")
for id in ids:
if not DialogService.query(tenant_id=tenant_id, id=id, status=StatusEnum.VALID.value):
return get_error_data_result(retmsg=f"You don't own the chat {id}")
Expand All @@ -234,7 +233,7 @@ def delete(tenant_id):

@manager.route('/chat', methods=['GET'])
@token_required
def list(tenant_id):
def list_chat(tenant_id):
id = request.args.get("id")
name = request.args.get("name")
chat = DialogService.query(id=id,name=name,status=StatusEnum.VALID.value)
Expand Down
53 changes: 36 additions & 17 deletions api/apps/sdk/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,38 @@
from api.db.services.user_service import TenantService
from api.settings import RetCode
from api.utils import get_uuid
from api.utils.api_utils import get_result, token_required,get_error_data_result
from api.utils.api_utils import get_result, token_required, get_error_data_result, valid


@manager.route('/dataset', methods=['POST'])
@token_required
def create(tenant_id):
req = request.json
e, t = TenantService.get_by_id(tenant_id)
permission = req.get("permission")
language = req.get("language")
chunk_method = req.get("chunk_method")
valid_permission = ("me", "team")
valid_language =("Chinese", "English")
valid_chunk_method = ("naive","manual","qa","table","paper","book","laws","presentation","picture","one","knowledge_graph","email")
check_validation=valid(permission,valid_permission,language,valid_language,chunk_method,valid_chunk_method)
if check_validation:
return check_validation
if "tenant_id" in req or "embedding_model" in req:
return get_error_data_result(
retmsg="Tenant_id or embedding_model must not be provided")
retmsg="`tenant_id` or `embedding_model` must not be provided")
chunk_count=req.get("chunk_count")
document_count=req.get("document_count")
if chunk_count or document_count:
return get_error_data_result(retmsg="chunk_count or document_count must be 0 or not be provided")
return get_error_data_result(retmsg="`chunk_count` or `document_count` must be 0 or not be provided")
if "name" not in req:
return get_error_data_result(
retmsg="Name is not empty!")
retmsg="`name` is not empty!")
req['id'] = get_uuid()
req["name"] = req["name"].strip()
if req["name"] == "":
return get_error_data_result(
retmsg="Name is not empty string!")
retmsg="`name` is not empty string!")
if KnowledgebaseService.query(name=req["name"], tenant_id=tenant_id, status=StatusEnum.VALID.value):
return get_error_data_result(
retmsg="Duplicated knowledgebase name in creating dataset.")
Expand All @@ -55,7 +65,7 @@ def create(tenant_id):
key_mapping = {
"chunk_num": "chunk_count",
"doc_num": "document_count",
"parser_id": "parse_method",
"parser_id": "chunk_method",
"embd_id": "embedding_model"
}
mapped_keys = {new_key: req[old_key] for new_key, old_key in key_mapping.items() if old_key in req}
Expand Down Expand Up @@ -90,7 +100,7 @@ def delete(tenant_id):
File2DocumentService.delete_by_document_id(doc.id)
if not KnowledgebaseService.delete_by_id(id):
return get_error_data_result(
retmsg="Delete dataset error.(Database serror)")
retmsg="Delete dataset error.(Database error)")
return get_result(retcode=RetCode.SUCCESS)

@manager.route('/dataset/<dataset_id>', methods=['PUT'])
Expand All @@ -103,30 +113,39 @@ def update(tenant_id,dataset_id):
invalid_keys = {"id", "embd_id", "chunk_num", "doc_num", "parser_id"}
if any(key in req for key in invalid_keys):
return get_error_data_result(retmsg="The input parameters are invalid.")
permission = req.get("permission")
language = req.get("language")
chunk_method = req.get("chunk_method")
valid_permission = ("me", "team")
valid_language =("Chinese", "English")
valid_chunk_method = ("naive","manual","qa","table","paper","book","laws","presentation","picture","one","knowledge_graph","email")
check_validation=valid(permission,valid_permission,language,valid_language,chunk_method,valid_chunk_method)
if check_validation:
return check_validation
if "tenant_id" in req:
if req["tenant_id"] != tenant_id:
return get_error_data_result(
retmsg="Can't change tenant_id.")
retmsg="Can't change `tenant_id`.")
e, kb = KnowledgebaseService.get_by_id(dataset_id)
if "chunk_count" in req:
if req["chunk_count"] != kb.chunk_num:
return get_error_data_result(
retmsg="Can't change chunk_count.")
retmsg="Can't change `chunk_count`.")
req.pop("chunk_count")
if "document_count" in req:
if req['document_count'] != kb.doc_num:
return get_error_data_result(
retmsg="Can't change document_count.")
retmsg="Can't change `document_count`.")
req.pop("document_count")
if "parse_method" in req:
if kb.chunk_num != 0 and req['parse_method'] != kb.parser_id:
if "chunk_method" in req:
if kb.chunk_num != 0 and req['chunk_method'] != kb.parser_id:
return get_error_data_result(
retmsg="If chunk count is not 0, parse method is not changable.")
req['parser_id'] = req.pop('parse_method')
retmsg="If `chunk_count` is not 0, `chunk_method` is not changeable.")
req['parser_id'] = req.pop('chunk_method')
if "embedding_model" in req:
if kb.chunk_num != 0 and req['parse_method'] != kb.parser_id:
if kb.chunk_num != 0 and req['embedding_model'] != kb.embd_id:
return get_error_data_result(
retmsg="If chunk count is not 0, parse method is not changable.")
retmsg="If `chunk_count` is not 0, `embedding_method` is not changeable.")
req['embd_id'] = req.pop('embedding_model')
if "name" in req:
req["name"] = req["name"].strip()
Expand Down Expand Up @@ -162,7 +181,7 @@ def list(tenant_id):
key_mapping = {
"chunk_num": "chunk_count",
"doc_num": "document_count",
"parser_id": "parse_method",
"parser_id": "chunk_method",
"embd_id": "embedding_model"
}
renamed_data = {}
Expand Down
40 changes: 23 additions & 17 deletions api/apps/sdk/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ def upload(dataset_id, tenant_id):
def update_doc(tenant_id, dataset_id, document_id):
req = request.json
if not KnowledgebaseService.query(id=dataset_id, tenant_id=tenant_id):
return get_error_data_result(retmsg='You do not own the dataset.')
return get_error_data_result(retmsg="You don't own the dataset.")
doc = DocumentService.query(kb_id=dataset_id, id=document_id)
if not doc:
return get_error_data_result(retmsg='The dataset not own the document.')
return get_error_data_result(retmsg="The dataset doesn't own the document.")
doc = doc[0]
if "chunk_count" in req:
if req["chunk_count"] != doc.chunk_num:
return get_error_data_result(retmsg="Can't change chunk_count.")
return get_error_data_result(retmsg="Can't change `chunk_count`.")
if "token_count" in req:
if req["token_count"] != doc.token_num:
return get_error_data_result(retmsg="Can't change token_count.")
return get_error_data_result(retmsg="Can't change `token_count`.")
if "progress" in req:
if req['progress'] != doc.progress:
return get_error_data_result(retmsg="Can't change progress.")
return get_error_data_result(retmsg="Can't change `progress`.")

if "name" in req and req["name"] != doc.name:
if pathlib.Path(req["name"].lower()).suffix != pathlib.Path(doc.name.lower()).suffix:
Expand All @@ -121,16 +121,16 @@ def update_doc(tenant_id, dataset_id, document_id):
FileService.update_by_id(file.id, {"name": req["name"]})
if "parser_config" in req:
DocumentService.update_parser_config(doc.id, req["parser_config"])
if "parser_method" in req:
if doc.parser_id.lower() == req["parser_method"].lower():
if "chunk_method" in req:
if doc.parser_id.lower() == req["chunk_method"].lower():
return get_result()

if doc.type == FileType.VISUAL or re.search(
r"\.(ppt|pptx|pages)$", doc.name):
return get_error_data_result(retmsg="Not supported yet!")

e = DocumentService.update_by_id(doc.id,
{"parser_id": req["parser_method"], "progress": 0, "progress_msg": "",
{"parser_id": req["chunk_method"], "progress": 0, "progress_msg": "",
"run": TaskStatus.UNSTART.value})
if not e:
return get_error_data_result(retmsg="Document not found!")
Expand Down Expand Up @@ -196,7 +196,7 @@ def list_docs(dataset_id, tenant_id):
"chunk_num": "chunk_count",
"kb_id": "knowledgebase_id",
"token_num": "token_count",
"parser_id": "parser_method"
"parser_id": "chunk_method"
}
renamed_doc = {}
for key, value in doc.items():
Expand All @@ -213,7 +213,7 @@ def delete(tenant_id,dataset_id):
return get_error_data_result(retmsg=f"You don't own the dataset {dataset_id}. ")
req = request.json
if not req.get("ids"):
return get_error_data_result(retmsg="ids is required")
return get_error_data_result(retmsg="`ids` is required")
doc_ids = req["ids"]
root_folder = FileService.get_root_folder(tenant_id)
pf_id = root_folder["id"]
Expand Down Expand Up @@ -457,7 +457,7 @@ def rm_chunk(tenant_id,dataset_id,document_id):

@manager.route('/dataset/<dataset_id>/document/<document_id>/chunk/<chunk_id>', methods=['PUT'])
@token_required
def set(tenant_id,dataset_id,document_id,chunk_id):
def update_chunk(tenant_id,dataset_id,document_id,chunk_id):
try:
res = ELASTICSEARCH.get(
chunk_id, search.index_name(
Expand Down Expand Up @@ -519,9 +519,15 @@ def retrieval_test(tenant_id):
req = request.json
if not req.get("datasets"):
return get_error_data_result("`datasets` is required.")
kb_id = req["datasets"]
if isinstance(kb_id, str): kb_id = [kb_id]
for id in kb_id:
kb_ids = req["datasets"]
kbs = KnowledgebaseService.get_by_ids(kb_ids)
embd_nms = list(set([kb.embd_id for kb in kbs]))
if len(embd_nms) != 1:
return get_result(
retmsg='Knowledge bases use different embedding models or does not exist."',
retcode=RetCode.AUTHENTICATION_ERROR)
if isinstance(kb_ids, str): kb_ids = [kb_ids]
for id in kb_ids:
if not KnowledgebaseService.query(id=id,tenant_id=tenant_id):
return get_error_data_result(f"You don't own the dataset {id}.")
if "question" not in req:
Expand All @@ -538,7 +544,7 @@ def retrieval_test(tenant_id):
else:
highlight = True
try:
e, kb = KnowledgebaseService.get_by_id(kb_id[0])
e, kb = KnowledgebaseService.get_by_id(kb_ids[0])
if not e:
return get_error_data_result(retmsg="Knowledgebase not found!")
embd_mdl = TenantLLMService.model_instance(
Expand All @@ -554,7 +560,7 @@ def retrieval_test(tenant_id):
question += keyword_extraction(chat_mdl, question)

retr = retrievaler if kb.parser_id != ParserType.KG else kg_retrievaler
ranks = retr.retrieval(question, embd_mdl, kb.tenant_id, kb_id, page, size,
ranks = retr.retrieval(question, embd_mdl, kb.tenant_id, kb_ids, page, size,
similarity_threshold, vector_similarity_weight, top,
doc_ids, rerank_mdl=rerank_mdl, highlight=highlight)
for c in ranks["chunks"]:
Expand All @@ -580,6 +586,6 @@ def retrieval_test(tenant_id):
return get_result(data=ranks)
except Exception as e:
if str(e).find("not_found") > 0:
return get_result(retmsg=f'No chunk found! Check the chunk statu s please!',
return get_result(retmsg=f'No chunk found! Check the chunk status please!',
retcode=RetCode.DATA_ERROR)
return server_error_response(e)
10 changes: 5 additions & 5 deletions api/apps/sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def create(tenant_id,chat_id):
"message": [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
}
if not conv.get("name"):
return get_error_data_result(retmsg="Name can not be empty.")
return get_error_data_result(retmsg="`name` can not be empty.")
ConversationService.save(**conv)
e, conv = ConversationService.get_by_id(conv["id"])
if not e:
Expand All @@ -62,11 +62,11 @@ def update(tenant_id,chat_id,session_id):
if not DialogService.query(id=chat_id, tenant_id=tenant_id, status=StatusEnum.VALID.value):
return get_error_data_result(retmsg="You do not own the session")
if "message" in req or "messages" in req:
return get_error_data_result(retmsg="Message can not be change")
return get_error_data_result(retmsg="`message` can not be change")
if "reference" in req:
return get_error_data_result(retmsg="Reference can not be change")
return get_error_data_result(retmsg="`reference` can not be change")
if "name" in req and not req.get("name"):
return get_error_data_result(retmsg="Name can not be empty.")
return get_error_data_result(retmsg="`name` can not be empty.")
if not ConversationService.update_by_id(conv_id, req):
return get_error_data_result(retmsg="Session updates error")
return get_result()
Expand All @@ -87,7 +87,7 @@ def completion(tenant_id,chat_id):
"message": [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
}
if not conv.get("name"):
return get_error_data_result(retmsg="Name can not be empty.")
return get_error_data_result(retmsg="`name` can not be empty.")
ConversationService.save(**conv)
e, conv = ConversationService.get_by_id(conv["id"])
session_id=conv.id
Expand Down
4 changes: 2 additions & 2 deletions api/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,8 @@ class Dialog(DataBaseModel):
default="simple",
help_text="simple|advanced",
index=True)
prompt_config = JSONField(null=False, default={"system": "", "prologue": "您好,我是您的助手小樱,长得可爱又善良,can I help you?",
"parameters": [], "empty_response": "Sorry! 知识库中未找到相关内容!"})
prompt_config = JSONField(null=False, default={"system": "", "prologue": "Hi! I'm your assistant, what can I do for you?",
"parameters": [], "empty_response": "Sorry! No relevant content was found in the knowledge base!"})

similarity_threshold = FloatField(default=0.2)
vector_similarity_weight = FloatField(default=0.3)
Expand Down
Loading

0 comments on commit 1935c3b

Please sign in to comment.