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

common.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. res = requests.get(url=url, headers=authorization)
  56. return res.json()
  57. def get_docs_info(auth, doc_ids):
  58. authorization = {"Authorization": auth}
  59. json_req = {"doc_ids": doc_ids}
  60. url = f"{HOST_ADDRESS}/v1/document/infos"
  61. res = requests.post(url=url, headers=authorization, json=json_req)
  62. return res.json()
  63. def parse_docs(auth, doc_ids):
  64. authorization = {"Authorization": auth}
  65. json_req = {"doc_ids": doc_ids, "run": 1}
  66. url = f"{HOST_ADDRESS}/v1/document/run"
  67. res = requests.post(url=url, headers=authorization, json=json_req)
  68. return res.json()
  69. def parse_file(auth, document_id):
  70. pass