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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. KB_APP_URL = f"/{VERSION}/kb"
  23. DOCUMENT_APP_URL = f"/{VERSION}/document"
  24. CHUNK_API_URL = f"/{VERSION}/chunk"
  25. # CHAT_ASSISTANT_API_URL = "/api/v1/chats"
  26. # SESSION_WITH_CHAT_ASSISTANT_API_URL = "/api/v1/chats/{chat_id}/sessions"
  27. # SESSION_WITH_AGENT_API_URL = "/api/v1/agents/{agent_id}/sessions"
  28. # KB APP
  29. def create_kb(auth, payload=None, *, headers=HEADERS, data=None):
  30. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/create", headers=headers, auth=auth, json=payload, data=data)
  31. return res.json()
  32. def list_kbs(auth, params=None, payload=None, *, headers=HEADERS, data=None):
  33. if payload is None:
  34. payload = {}
  35. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/list", headers=headers, auth=auth, params=params, json=payload, data=data)
  36. return res.json()
  37. def update_kb(auth, payload=None, *, headers=HEADERS, data=None):
  38. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/update", headers=headers, auth=auth, json=payload, data=data)
  39. return res.json()
  40. def rm_kb(auth, payload=None, *, headers=HEADERS, data=None):
  41. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/rm", headers=headers, auth=auth, json=payload, data=data)
  42. return res.json()
  43. def detail_kb(auth, params=None, *, headers=HEADERS):
  44. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/detail", headers=headers, auth=auth, params=params)
  45. return res.json()
  46. def list_tags_from_kbs(auth, params=None, *, headers=HEADERS):
  47. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/tags", headers=headers, auth=auth, params=params)
  48. return res.json()
  49. def list_tags(auth, dataset_id, params=None, *, headers=HEADERS):
  50. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/tags", headers=headers, auth=auth, params=params)
  51. return res.json()
  52. def rm_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  53. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/rm_tags", headers=headers, auth=auth, json=payload, data=data)
  54. return res.json()
  55. def rename_tags(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  56. res = requests.post(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/rename_tags", headers=headers, auth=auth, json=payload, data=data)
  57. return res.json()
  58. def knowledge_graph(auth, dataset_id, params=None, *, headers=HEADERS):
  59. res = requests.get(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/knowledge_graph", headers=headers, auth=auth, params=params)
  60. return res.json()
  61. def delete_knowledge_graph(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
  62. res = requests.delete(url=f"{HOST_ADDRESS}{KB_APP_URL}/{dataset_id}/delete_knowledge_graph", headers=headers, auth=auth, json=payload, data=data)
  63. return res.json()
  64. def batch_create_datasets(auth, num):
  65. ids = []
  66. for i in range(num):
  67. res = create_kb(auth, {"name": f"kb_{i}"})
  68. ids.append(res["data"]["kb_id"])
  69. return ids
  70. # DOCUMENT APP
  71. def upload_documents(auth, payload=None, files_path=None):
  72. url = f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/upload"
  73. if files_path is None:
  74. files_path = []
  75. fields = []
  76. file_objects = []
  77. try:
  78. if payload:
  79. for k, v in payload.items():
  80. fields.append((k, str(v)))
  81. for fp in files_path:
  82. p = Path(fp)
  83. f = p.open("rb")
  84. fields.append(("file", (p.name, f)))
  85. file_objects.append(f)
  86. m = MultipartEncoder(fields=fields)
  87. res = requests.post(
  88. url=url,
  89. headers={"Content-Type": m.content_type},
  90. auth=auth,
  91. data=m,
  92. )
  93. return res.json()
  94. finally:
  95. for f in file_objects:
  96. f.close()
  97. def create_document(auth, payload=None, *, headers=HEADERS, data=None):
  98. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/create", headers=headers, auth=auth, json=payload, data=data)
  99. return res.json()
  100. def list_documents(auth, params=None, payload=None, *, headers=HEADERS, data=None):
  101. if payload is None:
  102. payload = {}
  103. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/list", headers=headers, auth=auth, params=params, json=payload, data=data)
  104. return res.json()
  105. def delete_document(auth, payload=None, *, headers=HEADERS, data=None):
  106. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/rm", headers=headers, auth=auth, json=payload, data=data)
  107. return res.json()
  108. def parse_documents(auth, payload=None, *, headers=HEADERS, data=None):
  109. res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/run", headers=headers, auth=auth, json=payload, data=data)
  110. return res.json()
  111. def bulk_upload_documents(auth, kb_id, num, tmp_path):
  112. fps = []
  113. for i in range(num):
  114. fp = create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt")
  115. fps.append(fp)
  116. res = upload_documents(auth, {"kb_id": kb_id}, fps)
  117. document_ids = []
  118. for document in res["data"]:
  119. document_ids.append(document["id"])
  120. return document_ids
  121. # CHUNK APP
  122. def add_chunk(auth, payload=None, *, headers=HEADERS, data=None):
  123. res = requests.post(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/create", headers=headers, auth=auth, json=payload, data=data)
  124. return res.json()
  125. def list_chunks(auth, payload=None, *, headers=HEADERS):
  126. res = requests.post(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/list", headers=headers, auth=auth, json=payload)
  127. return res.json()
  128. def get_chunk(auth, params=None, *, headers=HEADERS):
  129. res = requests.get(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/get", headers=headers, auth=auth, params=params)
  130. return res.json()
  131. def update_chunk(auth, payload=None, *, headers=HEADERS):
  132. res = requests.post(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/set", headers=headers, auth=auth, json=payload)
  133. return res.json()
  134. def delete_chunks(auth, payload=None, *, headers=HEADERS):
  135. res = requests.post(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/rm", headers=headers, auth=auth, json=payload)
  136. return res.json()
  137. def retrieval_chunks(auth, payload=None, *, headers=HEADERS):
  138. res = requests.post(url=f"{HOST_ADDRESS}{CHUNK_API_URL}/retrieval_test", headers=headers, auth=auth, json=payload)
  139. return res.json()
  140. def batch_add_chunks(auth, doc_id, num):
  141. chunk_ids = []
  142. for i in range(num):
  143. res = add_chunk(auth, {"doc_id": doc_id, "content_with_weight": f"chunk test {i}"})
  144. chunk_ids.append(res["data"]["chunk_id"])
  145. return chunk_ids