您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #
  2. # Copyright 2025 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 pathlib import Path
  17. import requests
  18. from configs import HOST_ADDRESS, VERSION
  19. from requests_toolbelt import MultipartEncoder
  20. from utils.file_utils import create_txt_file
  21. HEADERS = {"Content-Type": "application/json"}
  22. KB_APP_URL = f"/{VERSION}/kb"
  23. DOCUMENT_APP_URL = f"/{VERSION}/document"
  24. # FILE_API_URL = "/api/v1/datasets/{dataset_id}/documents"
  25. # FILE_CHUNK_API_URL = "/api/v1/datasets/{dataset_id}/chunks"
  26. # CHUNK_API_URL = "/api/v1/datasets/{dataset_id}/documents/{document_id}/chunks"
  27. # CHAT_ASSISTANT_API_URL = "/api/v1/chats"
  28. # SESSION_WITH_CHAT_ASSISTANT_API_URL = "/api/v1/chats/{chat_id}/sessions"
  29. # SESSION_WITH_AGENT_API_URL = "/api/v1/agents/{agent_id}/sessions"
  30. # KB APP
  31. def create_kb(auth, payload=None, *, headers=HEADERS, data=None):
  32. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/create", headers=headers, auth=auth, json=payload, data=data)
  33. return res.json()
  34. def list_kbs(auth, params=None, payload=None, *, headers=HEADERS, data=None):
  35. if payload is None:
  36. payload = {}
  37. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/list", headers=headers, auth=auth, params=params, json=payload, data=data)
  38. return res.json()
  39. def update_kb(auth, payload=None, *, headers=HEADERS, data=None):
  40. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/update", headers=headers, auth=auth, json=payload, data=data)
  41. return res.json()
  42. def rm_kb(auth, payload=None, *, headers=HEADERS, data=None):
  43. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/rm", headers=headers, auth=auth, json=payload, data=data)
  44. return res.json()
  45. def detail_kb(auth, params=None, *, headers=HEADERS):
  46. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/detail", headers=headers, auth=auth, params=params)
  47. return res.json()
  48. def list_tags_from_kbs(auth, params=None, *, headers=HEADERS):
  49. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/tags", headers=headers, auth=auth, params=params)
  50. return res.json()
  51. def list_tags(auth, dataset_id, params=None, *, headers=HEADERS):
  52. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/tags", headers=headers, auth=auth, params=params)
  53. return res.json()
  54. def rm_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  55. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/rm_tags", headers=headers, auth=auth, json=payload, data=data)
  56. return res.json()
  57. def rename_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  58. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/rename_tags", headers=headers, auth=auth, json=payload, data=data)
  59. return res.json()
  60. def knowledge_graph(auth, dataset_id, params=None, *, headers=HEADERS):
  61. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/knowledge_graph", headers=headers, auth=auth, params=params)
  62. return res.json()
  63. def delete_knowledge_graph(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  64. res = requests.delete(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/delete_knowledge_graph", headers=headers, auth=auth, json=payload, data=data)
  65. return res.json()
  66. def batch_create_datasets(auth, num):
  67. ids = []
  68. for i in range(num):
  69. res = create_kb(auth, {"name": f"kb_{i}"})
  70. ids.append(res["data"]["kb_id"])
  71. return ids
  72. # DOCUMENT APP
  73. def upload_documents(auth, payload=None, files_path=None):
  74. url = f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/upload"
  75. if files_path is None:
  76. files_path = []
  77. fields = []
  78. file_objects = []
  79. try:
  80. if payload:
  81. for k, v in payload.items():
  82. fields.append((k, str(v)))
  83. for fp in files_path:
  84. p = Path(fp)
  85. f = p.open("rb")
  86. fields.append(("file", (p.name, f)))
  87. file_objects.append(f)
  88. m = MultipartEncoder(fields=fields)
  89. res = requests.post(
  90. url=url,
  91. headers={"Content-Type": m.content_type},
  92. auth=auth,
  93. data=m,
  94. )
  95. return res.json()
  96. finally:
  97. for f in file_objects:
  98. f.close()
  99. def create_document(auth, payload=None, *, headers=HEADERS, data=None):
  100. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/create", headers=headers, auth=auth, json=payload, data=data)
  101. return res.json()
  102. def list_documents(auth, params=None, payload=None, *, headers=HEADERS, data=None):
  103. if payload is None:
  104. payload = {}
  105. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/list", headers=headers, auth=auth, params=params, json=payload, data=data)
  106. return res.json()
  107. def delete_document(auth, payload=None, *, headers=HEADERS, data=None):
  108. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/rm", headers=headers, auth=auth, json=payload, data=data)
  109. return res.json()
  110. def parse_documents(auth, payload=None, *, headers=HEADERS, data=None):
  111. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/run", headers=headers, auth=auth, json=payload, data=data)
  112. return res.json()
  113. def bulk_upload_documents(auth, kb_id, num, tmp_path):
  114. fps = []
  115. for i in range(num):
  116. fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
  117. fps.append(fp)
  118. res = upload_documents(auth, {"kb_id": kb_id}, fps)
  119. document_ids = []
  120. for document in res["data"]:
  121. document_ids.append(document["id"])
  122. return document_ids