Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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