選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

document.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 json
  17. from .base import Base
  18. from .chunk import Chunk
  19. class Document(Base):
  20. class ParserConfig(Base):
  21. def __init__(self, rag, res_dict):
  22. super().__init__(rag, res_dict)
  23. def __init__(self, rag, res_dict):
  24. self.id = ""
  25. self.name = ""
  26. self.thumbnail = None
  27. self.dataset_id = None
  28. self.chunk_method = "naive"
  29. self.parser_config = {"pages": [[1, 1000000]]}
  30. self.source_type = "local"
  31. self.type = ""
  32. self.created_by = ""
  33. self.size = 0
  34. self.token_count = 0
  35. self.chunk_count = 0
  36. self.progress = 0.0
  37. self.progress_msg = ""
  38. self.process_begin_at = None
  39. self.process_duration = 0.0
  40. self.run = "0"
  41. self.status = "1"
  42. self.meta_fields = {}
  43. for k in list(res_dict.keys()):
  44. if k not in self.__dict__:
  45. res_dict.pop(k)
  46. super().__init__(rag, res_dict)
  47. def update(self, update_message: dict):
  48. if "meta_fields" in update_message:
  49. if not isinstance(update_message["meta_fields"], dict):
  50. raise Exception("meta_fields must be a dictionary")
  51. res = self.put(f"/datasets/{self.dataset_id}/documents/{self.id}", update_message)
  52. res = res.json()
  53. if res.get("code") != 0:
  54. raise Exception(res["message"])
  55. self._update_from_dict(self.rag, res.get("data", {}))
  56. return self
  57. def download(self):
  58. res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}")
  59. error_keys = set(["code", "message"])
  60. try:
  61. response = res.json()
  62. actual_keys = set(response.keys())
  63. if actual_keys == error_keys:
  64. raise Exception(res.get("message"))
  65. else:
  66. return res.content
  67. except json.JSONDecodeError:
  68. return res.content
  69. def list_chunks(self, page=1, page_size=30, keywords="", id=""):
  70. data = {"keywords": keywords, "page": page, "page_size": page_size, "id": id}
  71. res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", data)
  72. res = res.json()
  73. if res.get("code") == 0:
  74. chunks = []
  75. for data in res["data"].get("chunks"):
  76. chunk = Chunk(self.rag, data)
  77. chunks.append(chunk)
  78. return chunks
  79. raise Exception(res.get("message"))
  80. def add_chunk(self, content: str, important_keywords: list[str] = [], questions: list[str] = []):
  81. res = self.post(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", {"content": content, "important_keywords": important_keywords, "questions": questions})
  82. res = res.json()
  83. if res.get("code") == 0:
  84. return Chunk(self.rag, res["data"].get("chunk"))
  85. raise Exception(res.get("message"))
  86. def delete_chunks(self, ids: list[str] | None = None):
  87. res = self.rm(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", {"chunk_ids": ids})
  88. res = res.json()
  89. if res.get("code") != 0:
  90. raise Exception(res.get("message"))