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.

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