Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

common.py 9.0KB

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