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.

document.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. 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. for k in list(res_dict.keys()):
  43. if k not in self.__dict__:
  44. res_dict.pop(k)
  45. super().__init__(rag, res_dict)
  46. def update(self, update_message: dict):
  47. if "meta_fields" in update_message:
  48. if not isinstance(update_message["meta_fields"], dict):
  49. raise Exception("meta_fields must be a dictionary")
  50. res = self.put(f"/datasets/{self.dataset_id}/documents/{self.id}", update_message)
  51. res = res.json()
  52. if res.get("code") != 0:
  53. raise Exception(res["message"])
  54. self._update_from_dict(self.rag, res.get("data", {}))
  55. return self
  56. def download(self):
  57. res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}")
  58. try:
  59. res = res.json()
  60. raise Exception(res.get("message"))
  61. except json.JSONDecodeError:
  62. return res.content
  63. def list_chunks(self, page=1, page_size=30, keywords="", id=""):
  64. data = {"keywords": keywords, "page": page, "page_size": page_size, "id": id}
  65. res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", data)
  66. res = res.json()
  67. if res.get("code") == 0:
  68. chunks = []
  69. for data in res["data"].get("chunks"):
  70. chunk = Chunk(self.rag, data)
  71. chunks.append(chunk)
  72. return chunks
  73. raise Exception(res.get("message"))
  74. def add_chunk(self, content: str, important_keywords: list[str] = [], questions: list[str] = []):
  75. res = self.post(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", {"content": content, "important_keywords": important_keywords, "questions": questions})
  76. res = res.json()
  77. if res.get("code") == 0:
  78. return Chunk(self.rag, res["data"].get("chunk"))
  79. raise Exception(res.get("message"))
  80. def delete_chunks(self, ids: list[str] | None = None):
  81. res = self.rm(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", {"chunk_ids": ids})
  82. res = res.json()
  83. if res.get("code") != 0:
  84. raise Exception(res.get("message"))