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.

smoke.py 2.7KB

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