Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

document.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. res = self.put(f'/datasets/{self.dataset_id}/documents/{self.id}',
  48. update_message)
  49. res = res.json()
  50. if res.get("code") != 0:
  51. raise Exception(res["message"])
  52. def download(self):
  53. res = self.get(f"/datasets/{self.dataset_id}/documents/{self.id}")
  54. try:
  55. res = res.json()
  56. raise Exception(res.get("message"))
  57. except json.JSONDecodeError:
  58. return res.content
  59. def list_chunks(self, page=1, page_size=30, keywords=""):
  60. data = {"keywords": keywords, "page": page, "page_size": page_size}
  61. res = self.get(f'/datasets/{self.dataset_id}/documents/{self.id}/chunks', data)
  62. res = res.json()
  63. if res.get("code") == 0:
  64. chunks = []
  65. for data in res["data"].get("chunks"):
  66. chunk = Chunk(self.rag, data)
  67. chunks.append(chunk)
  68. return chunks
  69. raise Exception(res.get("message"))
  70. def add_chunk(self, content: str, important_keywords: list[str] = [], questions: list[str] = []):
  71. res = self.post(f'/datasets/{self.dataset_id}/documents/{self.id}/chunks',
  72. {"content": content, "important_keywords": important_keywords, "questions": questions})
  73. res = res.json()
  74. if res.get("code") == 0:
  75. return Chunk(self.rag, res["data"].get("chunk"))
  76. raise Exception(res.get("message"))
  77. def delete_chunks(self, ids: list[str] | None = None):
  78. res = self.rm(f"/datasets/{self.dataset_id}/documents/{self.id}/chunks", {"chunk_ids": ids})
  79. res = res.json()
  80. if res.get("code") != 0:
  81. raise Exception(res.get("message"))