Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

common.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import requests
  18. HOST_ADDRESS = os.getenv("HOST_ADDRESS", "http://127.0.0.1:9380")
  19. DATASET_NAME_LIMIT = 128
  20. def create_dataset(auth, dataset_name):
  21. authorization = {"Authorization": auth}
  22. url = f"{HOST_ADDRESS}/v1/kb/create"
  23. json = {"name": dataset_name}
  24. res = requests.post(url=url, headers=authorization, json=json)
  25. return res.json()
  26. def list_dataset(auth, page_number, page_size=30):
  27. authorization = {"Authorization": auth}
  28. url = f"{HOST_ADDRESS}/v1/kb/list?page={page_number}&page_size={page_size}"
  29. json = {}
  30. res = requests.post(url=url, headers=authorization, json=json)
  31. return res.json()
  32. def rm_dataset(auth, dataset_id):
  33. authorization = {"Authorization": auth}
  34. url = f"{HOST_ADDRESS}/v1/kb/rm"
  35. json = {"kb_id": dataset_id}
  36. res = requests.post(url=url, headers=authorization, json=json)
  37. return res.json()
  38. def update_dataset(auth, json_req):
  39. authorization = {"Authorization": auth}
  40. url = f"{HOST_ADDRESS}/v1/kb/update"
  41. res = requests.post(url=url, headers=authorization, json=json_req)
  42. return res.json()
  43. def upload_file(auth, dataset_id, path):
  44. authorization = {"Authorization": auth}
  45. url = f"{HOST_ADDRESS}/v1/document/upload"
  46. json_req = {
  47. "kb_id": dataset_id,
  48. }
  49. file = {"file": open(f"{path}", "rb")}
  50. res = requests.post(url=url, headers=authorization, files=file, data=json_req)
  51. return res.json()
  52. def list_document(auth, dataset_id):
  53. authorization = {"Authorization": auth}
  54. url = f"{HOST_ADDRESS}/v1/document/list?kb_id={dataset_id}"
  55. json = {}
  56. res = requests.post(url=url, headers=authorization, json=json)
  57. return res.json()
  58. def get_docs_info(auth, doc_ids):
  59. authorization = {"Authorization": auth}
  60. json_req = {"doc_ids": doc_ids}
  61. url = f"{HOST_ADDRESS}/v1/document/infos"
  62. res = requests.post(url=url, headers=authorization, json=json_req)
  63. return res.json()
  64. def parse_docs(auth, doc_ids):
  65. authorization = {"Authorization": auth}
  66. json_req = {"doc_ids": doc_ids, "run": 1}
  67. url = f"{HOST_ADDRESS}/v1/document/run"
  68. res = requests.post(url=url, headers=authorization, json=json_req)
  69. return res.json()
  70. def parse_file(auth, document_id):
  71. pass