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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(
  33. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  34. headers=HEADERS,
  35. auth=auth,
  36. json=payload,
  37. )
  38. return res.json()
  39. def list_dataset(auth, params=None):
  40. res = requests.get(
  41. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  42. headers=HEADERS,
  43. auth=auth,
  44. params=params,
  45. )
  46. return res.json()
  47. def update_dataset(auth, dataset_id, payload=None):
  48. res = requests.put(
  49. url=f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}",
  50. headers=HEADERS,
  51. auth=auth,
  52. json=payload,
  53. )
  54. return res.json()
  55. def delete_dataset(auth, payload=None):
  56. res = requests.delete(
  57. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  58. headers=HEADERS,
  59. auth=auth,
  60. json=payload,
  61. )
  62. return res.json()
  63. def batch_create_datasets(auth, num):
  64. ids = []
  65. for i in range(num):
  66. res = create_dataset(auth, {"name": f"dataset_{i}"})
  67. ids.append(res["data"]["id"])
  68. return ids
  69. # FILE MANAGEMENT WITHIN DATASET
  70. def upload_documnets(auth, dataset_id, files_path=None):
  71. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  72. if files_path is None:
  73. files_path = []
  74. fields = []
  75. file_objects = []
  76. try:
  77. for fp in files_path:
  78. p = Path(fp)
  79. f = p.open("rb")
  80. fields.append(("file", (p.name, f)))
  81. file_objects.append(f)
  82. m = MultipartEncoder(fields=fields)
  83. res = requests.post(
  84. url=url,
  85. headers={"Content-Type": m.content_type},
  86. auth=auth,
  87. data=m,
  88. )
  89. return res.json()
  90. finally:
  91. for f in file_objects:
  92. f.close()
  93. def download_document(auth, dataset_id, document_id, save_path):
  94. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  95. res = requests.get(url=url, auth=auth, stream=True)
  96. try:
  97. if res.status_code == 200:
  98. with open(save_path, "wb") as f:
  99. for chunk in res.iter_content(chunk_size=8192):
  100. f.write(chunk)
  101. finally:
  102. res.close()
  103. return res
  104. def list_documnet(auth, dataset_id, params=None):
  105. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  106. res = requests.get(
  107. url=url,
  108. headers=HEADERS,
  109. auth=auth,
  110. params=params,
  111. )
  112. return res.json()
  113. def update_documnet(auth, dataset_id, document_id, payload=None):
  114. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  115. res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
  116. return res.json()
  117. def delete_documnet(auth, dataset_id, payload=None):
  118. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  119. res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
  120. return res.json()
  121. def parse_documnet(auth, dataset_id, payload=None):
  122. url = f"{HOST_ADDRESS}{FILE_CHUNK_API_URL}".format(dataset_id=dataset_id)
  123. res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
  124. return res.json()
  125. def stop_parse_documnet(auth, dataset_id, payload=None):
  126. url = f"{HOST_ADDRESS}{FILE_CHUNK_API_URL}".format(dataset_id=dataset_id)
  127. res = requests.delete(url=url, headers=HEADERS, auth=auth, json=payload)
  128. return res.json()
  129. def bulk_upload_documents(auth, dataset_id, num, tmp_path):
  130. fps = []
  131. for i in range(num):
  132. fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
  133. fps.append(fp)
  134. res = upload_documnets(auth, dataset_id, fps)
  135. document_ids = []
  136. for document in res["data"]:
  137. document_ids.append(document["id"])
  138. return document_ids
  139. # CHUNK MANAGEMENT WITHIN DATASET
  140. def add_chunk(auth, dataset_id, document_id, payload=None):
  141. url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
  142. res = requests.post(url=url, headers=HEADERS, auth=auth, json=payload)
  143. return res.json()
  144. def list_chunks(auth, dataset_id, document_id, params=None):
  145. url = f"{HOST_ADDRESS}{CHUNK_API_URL}".format(dataset_id=dataset_id, document_id=document_id)
  146. res = requests.get(
  147. url=url,
  148. headers=HEADERS,
  149. auth=auth,
  150. params=params,
  151. )
  152. return res.json()
  153. def batch_add_chunks(auth, dataset_id, document_id, num):
  154. chunk_ids = []
  155. for i in range(num):
  156. res = add_chunk(auth, dataset_id, document_id, {"content": f"chunk test {i}"})
  157. chunk_ids.append(res["data"]["chunk"]["id"])
  158. return chunk_ids