You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. import os
  17. from pathlib import Path
  18. import requests
  19. from libs.utils.file_utils import create_txt_file
  20. from requests_toolbelt import MultipartEncoder
  21. HEADERS = {"Content-Type": "application/json"}
  22. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  23. DATASETS_API_URL = "/api/v1/datasets"
  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. INVALID_API_TOKEN = "invalid_key_123"
  28. DATASET_NAME_LIMIT = 128
  29. DOCUMENT_NAME_LIMIT = 128
  30. # DATASET MANAGEMENT
  31. def create_dataset(auth, payload=None):
  32. res = requests.post(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=HEADERS, auth=auth, json=payload)
  33. return res.json()
  34. def list_dataset(auth, params=None):
  35. res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=HEADERS, auth=auth, params=params)
  36. return res.json()
  37. def update_dataset(auth, dataset_id, payload=None):
  38. res = requests.put(url=f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}", headers=HEADERS, auth=auth, json=payload)
  39. return res.json()
  40. def delete_dataset(auth, payload=None):
  41. res = requests.delete(url=f"{HOST_ADDRESS}{DATASETS_API_URL}", headers=HEADERS, auth=auth, json=payload)
  42. return res.json()
  43. def batch_create_datasets(auth, num):
  44. ids = []
  45. for i in range(num):
  46. res = create_dataset(auth, {"name": f"dataset_{i}"})
  47. ids.append(res["data"]["id"])
  48. return ids
  49. # FILE MANAGEMENT WITHIN DATASET
  50. def upload_documnets(auth, dataset_id, files_path=None):
  51. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  52. if files_path is None:
  53. files_path = []
  54. fields = []
  55. file_objects = []
  56. try:
  57. for fp in files_path:
  58. p = Path(fp)
  59. f = p.open("rb")
  60. fields.append(("file", (p.name, f)))
  61. file_objects.append(f)
  62. m = MultipartEncoder(fields=fields)
  63. res = requests.post(
  64. url=url,
  65. headers={"Content-Type": m.content_type},
  66. auth=auth,
  67. data=m,
  68. )
  69. return res.json()
  70. finally:
  71. for f in file_objects:
  72. f.close()
  73. def download_document(auth, dataset_id, document_id, save_path):
  74. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  75. res = requests.get(url=url, auth=auth, stream=True)
  76. try:
  77. if res.status_code == 200:
  78. with open(save_path, "wb") as f:
  79. for chunk in res.iter_content(chunk_size=8192):
  80. f.write(chunk)
  81. finally:
  82. res.close()
  83. return res
  84. def list_documnet(auth, dataset_id, params=None):
  85. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  86. res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
  87. return res.json()
  88. def update_documnet(auth, dataset_id, document_id, payload=None):
  89. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  90. res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
  91. return res.json()
  92. def delete_documnet(auth, dataset_id, payload=None):
  93. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  94. res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
  95. return res.json()
  96. def parse_documnet(auth, dataset_id, payload=None):
  97. url = f"{HOST_ADDRESS}{FILE_CHUNK_API_URL}".format(dataset_id=dataset_id)
  98. res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
  99. return res.json()
  100. def stop_parse_documnet(auth, dataset_id, payload=None):
  101. url = f"{HOST_ADDRESS}{FILE_CHUNK_API_URL}".format(dataset_id=dataset_id)
  102. res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
  103. return res.json()
  104. def bulk_upload_documents(auth, dataset_id, num, tmp_path):
  105. fps = []
  106. for i in range(num):
  107. fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
  108. fps.append(fp)
  109. res = upload_documnets(auth, dataset_id, fps)
  110. document_ids = []
  111. for document in res["data"]:
  112. document_ids.append(document["id"])
  113. return document_ids
  114. # CHUNK MANAGEMENT WITHIN DATASET
  115. def add_chunk(auth, dataset_id, document_id, payload=None):
  116. url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
  117. res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
  118. return res.json()
  119. def list_chunks(auth, dataset_id, document_id, params=None):
  120. url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
  121. res = requests.get(url=url, headers=HEADERS, auth=auth, params=params)
  122. return res.json()
  123. def update_chunk(auth, dataset_id, document_id, chunk_id, payload=None):
  124. url = f"{HOST_ADDRESS}{CHUNK_API_URL}/{chunk_id}".format(dataset_id=dataset_id, document_id=document_id)
  125. res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
  126. return res.json()
  127. def delete_chunks(auth, dataset_id, document_id, payload=None):
  128. url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
  129. res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
  130. return res.json()
  131. def batch_add_chunks(auth, dataset_id, document_id, num):
  132. chunk_ids = []
  133. for i in range(num):
  134. res = add_chunk(auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  135. chunk_ids.append(res["data"]["chunk"]["id"])
  136. return chunk_ids