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.

knowledge_graph.py 1.2KB

123456789101112131415161718192021222324252627282930
  1. import re
  2. from graphrag.index import build_knowlege_graph_chunks
  3. from rag.app import naive
  4. from rag.nlp import rag_tokenizer, tokenize_chunks
  5. def chunk(filename, binary, tenant_id, from_page=0, to_page=100000,
  6. lang="Chinese", callback=None, **kwargs):
  7. parser_config = kwargs.get(
  8. "parser_config", {
  9. "chunk_token_num": 512, "delimiter": "\n!?。;!?", "layout_recognize": False})
  10. eng = lang.lower() == "english"
  11. parser_config["layout_recognize"] = False
  12. sections = naive.chunk(filename, binary, from_page=from_page, to_page=to_page, section_only=True, parser_config=parser_config)
  13. chunks = build_knowlege_graph_chunks(tenant_id, sections, callback,
  14. parser_config.get("entity_types", ["organization", "person", "location", "event", "time"])
  15. )
  16. for c in chunks: c["docnm_kwd"] = filename
  17. doc = {
  18. "docnm_kwd": filename,
  19. "title_tks": rag_tokenizer.tokenize(re.sub(r"\.[a-zA-Z]+$", "", filename)),
  20. "knowledge_graph_kwd": "text"
  21. }
  22. doc["title_sm_tks"] = rag_tokenizer.fine_grained_tokenize(doc["title_tks"])
  23. chunks.extend(tokenize_chunks(sections, doc, eng))
  24. return chunks