選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 graphrag import leiden
  19. from graphrag.community_reports_extractor import CommunityReportsExtractor
  20. from graphrag.entity_resolution import EntityResolution
  21. from graphrag.graph_extractor import GraphExtractor
  22. from graphrag.leiden import add_community_info2graph
  23. if __name__ == "__main__":
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument('-t', '--tenant_id', default=False, help="Tenant ID", action='store', required=True)
  26. parser.add_argument('-d', '--doc_id', default=False, help="Document ID", action='store', required=True)
  27. args = parser.parse_args()
  28. from api.db import LLMType
  29. from api.db.services.llm_service import LLMBundle
  30. from api import settings
  31. from api.db.services.knowledgebase_service import KnowledgebaseService
  32. kb_ids = KnowledgebaseService.get_kb_ids(args.tenant_id)
  33. ex = GraphExtractor(LLMBundle(args.tenant_id, LLMType.CHAT))
  34. docs = [d["content_with_weight"] for d in
  35. settings.retrievaler.chunk_list(args.doc_id, args.tenant_id, kb_ids, max_count=6, fields=["content_with_weight"])]
  36. graph = ex(docs)
  37. er = EntityResolution(LLMBundle(args.tenant_id, LLMType.CHAT))
  38. graph = er(graph.output)
  39. comm = leiden.run(graph.output, {})
  40. add_community_info2graph(graph.output, comm)
  41. # print(json.dumps(nx.node_link_data(graph.output), ensure_ascii=False,indent=2))
  42. print(json.dumps(comm, ensure_ascii=False, indent=2))
  43. cr = CommunityReportsExtractor(LLMBundle(args.tenant_id, LLMType.CHAT))
  44. cr = cr(graph.output)
  45. print("------------------ COMMUNITY REPORT ----------------------\n", cr.output)
  46. print(json.dumps(cr.structured_output, ensure_ascii=False, indent=2))