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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import networkx as nx
  19. from api import settings
  20. from api.db import LLMType
  21. from api.db.services.document_service import DocumentService
  22. from api.db.services.knowledgebase_service import KnowledgebaseService
  23. from api.db.services.llm_service import LLMBundle
  24. from api.db.services.user_service import TenantService
  25. from graphrag.general.index import WithCommunity, Dealer, WithResolution
  26. from graphrag.light.graph_extractor import GraphExtractor
  27. from rag.utils.redis_conn import RedisDistributedLock
  28. settings.init_settings()
  29. if __name__ == "__main__":
  30. parser = argparse.ArgumentParser()
  31. parser.add_argument('-t', '--tenant_id', default=False, help="Tenant ID", action='store', required=True)
  32. parser.add_argument('-d', '--doc_id', default=False, help="Document ID", action='store', required=True)
  33. args = parser.parse_args()
  34. e, doc = DocumentService.get_by_id(args.doc_id)
  35. if not e:
  36. raise LookupError("Document not found.")
  37. kb_id = doc.kb_id
  38. chunks = [d["content_with_weight"] for d in
  39. settings.retrievaler.chunk_list(args.doc_id, args.tenant_id, [kb_id], max_count=6,
  40. fields=["content_with_weight"])]
  41. chunks = [("x", c) for c in chunks]
  42. RedisDistributedLock.clean_lock(kb_id)
  43. _, tenant = TenantService.get_by_id(args.tenant_id)
  44. llm_bdl = LLMBundle(args.tenant_id, LLMType.CHAT, tenant.llm_id)
  45. _, kb = KnowledgebaseService.get_by_id(kb_id)
  46. embed_bdl = LLMBundle(args.tenant_id, LLMType.EMBEDDING, kb.embd_id)
  47. dealer = Dealer(GraphExtractor, args.tenant_id, kb_id, llm_bdl, chunks, "English", embed_bdl=embed_bdl)
  48. print(json.dumps(nx.node_link_data(dealer.graph), ensure_ascii=False, indent=2))
  49. dealer = WithResolution(args.tenant_id, kb_id, llm_bdl, embed_bdl)
  50. dealer = WithCommunity(args.tenant_id, kb_id, llm_bdl, embed_bdl)
  51. print("------------------ COMMUNITY REPORT ----------------------\n", dealer.community_reports)
  52. print(json.dumps(dealer.community_structure, ensure_ascii=False, indent=2))