Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from flask import request, jsonify
  17. from api.db import LLMType
  18. from api.db.services.knowledgebase_service import KnowledgebaseService
  19. from api.db.services.llm_service import LLMBundle
  20. from api import settings
  21. from api.utils.api_utils import validate_request, build_error_result, apikey_required
  22. from rag.app.tag import label_question
  23. @manager.route('/dify/retrieval', methods=['POST']) # noqa: F821
  24. @apikey_required
  25. @validate_request("knowledge_id", "query")
  26. def retrieval(tenant_id):
  27. req = request.json
  28. question = req["query"]
  29. kb_id = req["knowledge_id"]
  30. use_kg = req.get("use_kg", False)
  31. retrieval_setting = req.get("retrieval_setting", {})
  32. similarity_threshold = float(retrieval_setting.get("score_threshold", 0.0))
  33. top = int(retrieval_setting.get("top_k", 1024))
  34. try:
  35. e, kb = KnowledgebaseService.get_by_id(kb_id)
  36. if not e:
  37. return build_error_result(message="Knowledgebase not found!", code=settings.RetCode.NOT_FOUND)
  38. if kb.tenant_id != tenant_id:
  39. return build_error_result(message="Knowledgebase not found!", code=settings.RetCode.NOT_FOUND)
  40. embd_mdl = LLMBundle(kb.tenant_id, LLMType.EMBEDDING.value, llm_name=kb.embd_id)
  41. ranks = settings.retrievaler.retrieval(
  42. question,
  43. embd_mdl,
  44. kb.tenant_id,
  45. [kb_id],
  46. page=1,
  47. page_size=top,
  48. similarity_threshold=similarity_threshold,
  49. vector_similarity_weight=0.3,
  50. top=top,
  51. rank_feature=label_question(question, [kb])
  52. )
  53. if use_kg:
  54. ck = settings.kg_retrievaler.retrieval(question,
  55. [tenant_id],
  56. [kb_id],
  57. embd_mdl,
  58. LLMBundle(kb.tenant_id, LLMType.CHAT))
  59. if ck["content_with_weight"]:
  60. ranks["chunks"].insert(0, ck)
  61. records = []
  62. for c in ranks["chunks"]:
  63. c.pop("vector", None)
  64. records.append({
  65. "content": c["content_with_weight"],
  66. "score": c["similarity"],
  67. "title": c["docnm_kwd"],
  68. "metadata": {}
  69. })
  70. return jsonify({"records": records})
  71. except Exception as e:
  72. if str(e).find("not_found") > 0:
  73. return build_error_result(
  74. message='No chunk found! Check the chunk status please!',
  75. code=settings.RetCode.NOT_FOUND
  76. )
  77. return build_error_result(message=str(e), code=settings.RetCode.SERVER_ERROR)