Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

common.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. INVALID_API_TOKEN = "invalid_key_123"
  26. DATASET_NAME_LIMIT = 128
  27. DOCUMENT_NAME_LIMIT = 128
  28. # DATASET MANAGEMENT
  29. def create_dataset(auth, payload):
  30. res = requests.post(
  31. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  32. headers=HEADERS,
  33. auth=auth,
  34. json=payload,
  35. )
  36. return res.json()
  37. def list_dataset(auth, params=None):
  38. res = requests.get(
  39. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  40. headers=HEADERS,
  41. auth=auth,
  42. params=params,
  43. )
  44. return res.json()
  45. def update_dataset(auth, dataset_id, payload):
  46. res = requests.put(
  47. url=f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}",
  48. headers=HEADERS,
  49. auth=auth,
  50. json=payload,
  51. )
  52. return res.json()
  53. def delete_dataset(auth, payload=None):
  54. res = requests.delete(
  55. url=f"{HOST_ADDRESS}{DATASETS_API_URL}",
  56. headers=HEADERS,
  57. auth=auth,
  58. json=payload,
  59. )
  60. return res.json()
  61. def create_datasets(auth, num):
  62. ids = []
  63. for i in range(num):
  64. res = create_dataset(auth, {"name": f"dataset_{i}"})
  65. ids.append(res["data"]["id"])
  66. return ids
  67. # FILE MANAGEMENT WITHIN DATASET
  68. def upload_documnets(auth, dataset_id, files_path=None):
  69. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  70. if files_path is None:
  71. files_path = []
  72. fields = []
  73. for i, fp in enumerate(files_path):
  74. p = Path(fp)
  75. fields.append(("file", (p.name, p.open("rb"))))
  76. m = MultipartEncoder(fields=fields)
  77. res = requests.post(
  78. url=url,
  79. headers={"Content-Type": m.content_type},
  80. auth=auth,
  81. data=m,
  82. )
  83. return res.json()
  84. def batch_upload_documents(auth, dataset_id, num, tmp_path):
  85. fps = []
  86. for i in range(num):
  87. fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
  88. fps.append(fp)
  89. res = upload_documnets(auth, dataset_id, fps)
  90. document_ids = []
  91. for document in res["data"]:
  92. document_ids.append(document["id"])
  93. return document_ids
  94. def download_document(auth, dataset_id, document_id, save_path):
  95. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  96. res = requests.get(url=url, auth=auth, stream=True)
  97. try:
  98. if res.status_code == 200:
  99. with open(save_path, "wb") as f:
  100. for chunk in res.iter_content(chunk_size=8192):
  101. f.write(chunk)
  102. finally:
  103. res.close()
  104. return res
  105. def list_documnet(auth, dataset_id, params=None):
  106. url = f"{HOST_ADDRESS}{FILE_API_URL}".format(dataset_id=dataset_id)
  107. res = requests.get(
  108. url=url,
  109. headers=HEADERS,
  110. auth=auth,
  111. params=params,
  112. )
  113. return res.json()
  114. def update_documnet(auth, dataset_id, document_id, payload):
  115. url = f"{HOST_ADDRESS}{FILE_API_URL}/{document_id}".format(dataset_id=dataset_id)
  116. res = requests.put(url=url, headers=HEADERS, auth=auth, json=payload)
  117. return res.json()