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

dataset.py 3.5KB

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. from .document import Document
  17. from .base import Base
  18. class DataSet(Base):
  19. class ParserConfig(Base):
  20. def __init__(self, rag, res_dict):
  21. super().__init__(rag, res_dict)
  22. def __init__(self, rag, res_dict):
  23. self.id = ""
  24. self.name = ""
  25. self.avatar = ""
  26. self.tenant_id = None
  27. self.description = ""
  28. self.language = "English"
  29. self.embedding_model = ""
  30. self.permission = "me"
  31. self.document_count = 0
  32. self.chunk_count = 0
  33. self.chunk_method = "naive"
  34. self.parser_config = None
  35. self.pagerank = 0
  36. for k in list(res_dict.keys()):
  37. if k not in self.__dict__:
  38. res_dict.pop(k)
  39. super().__init__(rag, res_dict)
  40. def update(self, update_message: dict):
  41. res = self.put(f'/datasets/{self.id}',
  42. update_message)
  43. res = res.json()
  44. if res.get("code") != 0:
  45. raise Exception(res["message"])
  46. def upload_documents(self, document_list: list[dict]):
  47. url = f"/datasets/{self.id}/documents"
  48. files = [("file", (ele["display_name"], ele["blob"])) for ele in document_list]
  49. res = self.post(path=url, json=None, files=files)
  50. res = res.json()
  51. if res.get("code") == 0:
  52. doc_list = []
  53. for doc in res["data"]:
  54. document = Document(self.rag, doc)
  55. doc_list.append(document)
  56. return doc_list
  57. raise Exception(res.get("message"))
  58. def list_documents(self, id: str | None = None, keywords: str | None = None, page: int = 1, page_size: int = 30,
  59. orderby: str = "create_time", desc: bool = True):
  60. res = self.get(f"/datasets/{self.id}/documents",
  61. params={"id": id, "keywords": keywords, "page": page, "page_size": page_size, "orderby": orderby,
  62. "desc": desc})
  63. res = res.json()
  64. documents = []
  65. if res.get("code") == 0:
  66. for document in res["data"].get("docs"):
  67. documents.append(Document(self.rag, document))
  68. return documents
  69. raise Exception(res["message"])
  70. def delete_documents(self, ids: list[str] | None = None):
  71. res = self.rm(f"/datasets/{self.id}/documents", {"ids": ids})
  72. res = res.json()
  73. if res.get("code") != 0:
  74. raise Exception(res["message"])
  75. def async_parse_documents(self, document_ids):
  76. res = self.post(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})
  77. res = res.json()
  78. if res.get("code") != 0:
  79. raise Exception(res.get("message"))
  80. def async_cancel_parse_documents(self, document_ids):
  81. res = self.rm(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})
  82. res = res.json()
  83. if res.get("code") != 0:
  84. raise Exception(res.get("message"))