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

common.py 9.2KB

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