Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

common.py 7.4KB

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