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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # Copyright 2024 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 argparse
  17. import json
  18. from api import settings
  19. import networkx as nx
  20. import logging
  21. import trio
  22. from api.db import LLMType
  23. from api.db.services.document_service import DocumentService
  24. from api.db.services.knowledgebase_service import KnowledgebaseService
  25. from api.db.services.llm_service import LLMBundle
  26. from api.db.services.user_service import TenantService
  27. from graphrag.general.index import update_graph
  28. from graphrag.light.graph_extractor import GraphExtractor
  29. settings.init_settings()
  30. def callback(prog=None, msg="Processing..."):
  31. logging.info(msg)
  32. async def main():
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument(
  35. "-t",
  36. "--tenant_id",
  37. default=False,
  38. help="Tenant ID",
  39. action="store",
  40. required=True,
  41. )
  42. parser.add_argument(
  43. "-d",
  44. "--doc_id",
  45. default=False,
  46. help="Document ID",
  47. action="store",
  48. required=True,
  49. )
  50. args = parser.parse_args()
  51. e, doc = DocumentService.get_by_id(args.doc_id)
  52. if not e:
  53. raise LookupError("Document not found.")
  54. kb_id = doc.kb_id
  55. chunks = [
  56. d["content_with_weight"]
  57. for d in settings.retrievaler.chunk_list(
  58. args.doc_id,
  59. args.tenant_id,
  60. [kb_id],
  61. max_count=6,
  62. fields=["content_with_weight"],
  63. )
  64. ]
  65. _, tenant = TenantService.get_by_id(args.tenant_id)
  66. llm_bdl = LLMBundle(args.tenant_id, LLMType.CHAT, tenant.llm_id)
  67. _, kb = KnowledgebaseService.get_by_id(kb_id)
  68. embed_bdl = LLMBundle(args.tenant_id, LLMType.EMBEDDING, kb.embd_id)
  69. graph, doc_ids = await update_graph(
  70. GraphExtractor,
  71. args.tenant_id,
  72. kb_id,
  73. args.doc_id,
  74. chunks,
  75. "English",
  76. llm_bdl,
  77. embed_bdl,
  78. callback,
  79. )
  80. print(json.dumps(nx.node_link_data(graph), ensure_ascii=False, indent=2))
  81. if __name__ == "__main__":
  82. trio.run(main)