当前位置:   article > 正文

微软GraphRAG +本地模型+Gradio 简单测试笔记_graphrag对接国内模型

graphrag对接国内模型

安装

  1. pip install graphrag
  2. mkdir -p ./ragtest/input
  3. #将文档拷贝至 ./ragtest/input/ 下
  4. python -m graphrag.index --init --root ./ragtest

修改settings.yaml

  1. encoding_model: cl100k_base
  2. skip_workflows: []
  3. llm:
  4. api_key: ${GRAPHRAG_API_KEY}
  5. type: openai_chat # or azure_openai_chat
  6. model: qwen2-instruct
  7. model_supports_json: true # recommended if this is available for your model.
  8. # max_tokens: 4000
  9. # request_timeout: 180.0
  10. api_base: http://192.168.2.2:9997/v1/
  11. # api_version: 2024-02-15-preview
  12. # organization: <organization_id>
  13. # deployment_name: <azure_model_deployment_name>
  14. # tokens_per_minute: 150_000 # set a leaky bucket throttle
  15. # requests_per_minute: 10_000 # set a leaky bucket throttle
  16. # max_retries: 10
  17. # max_retry_wait: 10.0
  18. # sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times
  19. concurrent_requests: 5 # the number of parallel inflight requests that may be made
  20. parallelization:
  21. stagger: 0.3
  22. # num_threads: 50 # the number of threads to use for parallel processing
  23. async_mode: threaded # or asyncio
  24. embeddings:
  25. ## parallelization: override the global parallelization settings for embeddings
  26. async_mode: threaded # or asyncio
  27. llm:
  28. api_key: ${GRAPHRAG_API_KEY}
  29. type: openai_embedding # or azure_openai_embedding
  30. model: bge-large-zh-v1.5
  31. api_base: http://127.0.0.1:9997/v1/
  32. # api_version: 2024-02-15-preview
  33. # organization: <organization_id>
  34. # deployment_name: <azure_model_deployment_name>
  35. # tokens_per_minute: 150_000 # set a leaky bucket throttle
  36. # requests_per_minute: 10_000 # set a leaky bucket throttle
  37. # max_retries: 10
  38. # max_retry_wait: 10.0
  39. # sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times
  40. # concurrent_requests: 25 # the number of parallel inflight requests that may be made
  41. # batch_size: 16 # the number of documents to send in a single request
  42. # batch_max_tokens: 8191 # the maximum number of tokens to send in a single request
  43. # target: required # or optional
  44. chunks:
  45. size: 300
  46. overlap: 100
  47. group_by_columns: [id] # by default, we don't allow chunks to cross documents
  48. input:
  49. type: file # or blob
  50. file_type: text # or csv
  51. base_dir: "input"
  52. file_encoding: utf-8
  53. file_pattern: ".*\\.txt$"
  54. cache:
  55. type: file # or blob
  56. base_dir: "cache"
  57. # connection_string: <azure_blob_storage_connection_string>
  58. # container_name: <azure_blob_storage_container_name>
  59. storage:
  60. type: file # or blob
  61. base_dir: "output/${timestamp}/artifacts"
  62. # connection_string: <azure_blob_storage_connection_string>
  63. # container_name: <azure_blob_storage_container_name>
  64. reporting:
  65. type: file # or console, blob
  66. base_dir: "output/${timestamp}/reports"
  67. # connection_string: <azure_blob_storage_connection_string>
  68. # container_name: <azure_blob_storage_container_name>
  69. entity_extraction:
  70. ## llm: override the global llm settings for this task
  71. ## parallelization: override the global parallelization settings for this task
  72. ## async_mode: override the global async_mode settings for this task
  73. prompt: "prompts/entity_extraction.txt"
  74. entity_types: [organization,person,geo,event]
  75. max_gleanings: 0
  76. summarize_descriptions:
  77. ## llm: override the global llm settings for this task
  78. ## parallelization: override the global parallelization settings for this task
  79. ## async_mode: override the global async_mode settings for this task
  80. prompt: "prompts/summarize_descriptions.txt"
  81. max_length: 500
  82. claim_extraction:
  83. ## llm: override the global llm settings for this task
  84. ## parallelization: override the global parallelization settings for this task
  85. ## async_mode: override the global async_mode settings for this task
  86. # enabled: true
  87. prompt: "prompts/claim_extraction.txt"
  88. description: "Any claims or facts that could be relevant to information discovery."
  89. max_gleanings: 0
  90. community_report:
  91. ## llm: override the global llm settings for this task
  92. ## parallelization: override the global parallelization settings for this task
  93. ## async_mode: override the global async_mode settings for this task
  94. prompt: "prompts/community_report.txt"
  95. max_length: 2000
  96. max_input_length: 8000
  97. cluster_graph:
  98. max_cluster_size: 10
  99. embed_graph:
  100. enabled: false # if true, will generate node2vec embeddings for nodes
  101. # num_walks: 10
  102. # walk_length: 40
  103. # window_size: 2
  104. # iterations: 3
  105. # random_seed: 597832
  106. umap:
  107. enabled: false # if true, will generate UMAP embeddings for nodes
  108. snapshots:
  109. graphml: false
  110. raw_entities: false
  111. top_level_nodes: false
  112. local_search:
  113. # text_unit_prop: 0.5
  114. # community_prop: 0.1
  115. # conversation_history_max_turns: 5
  116. # top_k_mapped_entities: 10
  117. # top_k_relationships: 10
  118. # max_tokens: 12000
  119. global_search:
  120. # max_tokens: 12000
  121. # data_max_tokens: 12000
  122. # map_max_tokens: 1000
  123. # reduce_max_tokens: 2000
  124. # concurrency: 32

LLM模型 :Qwen2-72B-Instruct
EMBEDDING模型:  bge-large-zh-v1.5

本地部署模型使用的Xinference

生成索引 图谱

python -m graphrag.index --root ./ragtest

成功界面

全局查询和本地查询

  1. python -m graphrag.query \
  2. --root ./ragtest \
  3. --method global \
  4. "你的问题"
  5. python -m graphrag.query \
  6. --root ./ragtest \
  7. --method local \
  8. "你的问题"

gradio 代码

  1. import sys
  2. import shlex
  3. import gradio as gr
  4. import subprocess
  5. def parse_text(text):
  6. lines = text.split("\n")
  7. lines = [line for line in lines if line != ""]
  8. count = 0
  9. for i, line in enumerate(lines):
  10. if "```" in line:
  11. count += 1
  12. items = line.split('`')
  13. if count % 2 == 1:
  14. lines[i] = f'<pre><code class="language-{items[-1]}">'
  15. else:
  16. lines[i] = f'<br></code></pre>'
  17. else:
  18. if i > 0:
  19. if count % 2 == 1:
  20. line = line.replace("`", "\`")
  21. line = line.replace("<", "&lt;")
  22. line = line.replace(">", "&gt;")
  23. line = line.replace(" ", "&nbsp;")
  24. line = line.replace("*", "&ast;")
  25. line = line.replace("_", "&lowbar;")
  26. line = line.replace("-", "&#45;")
  27. line = line.replace(".", "&#46;")
  28. line = line.replace("!", "&#33;")
  29. line = line.replace("(", "&#40;")
  30. line = line.replace(")", "&#41;")
  31. line = line.replace("$", "&#36;")
  32. lines[i] = "<br>" + line
  33. text = "".join(lines)
  34. return text
  35. def predict(history):
  36. messages = []
  37. for idx, (user_msg, model_msg) in enumerate(history):
  38. if idx == len(history) - 1 and not model_msg:
  39. messages.append({"role": "user", "content": user_msg})
  40. break
  41. if user_msg:
  42. messages.append({"role": "user", "content": user_msg})
  43. if model_msg:
  44. messages.append({"role": "assistant", "content": model_msg})
  45. messages = messages[len(messages) - 1]["content"]
  46. print("\n\n====conversation====\n", messages)
  47. python_path = sys.executable
  48. # 构建命令
  49. cmd = [
  50. python_path, "-m", "graphrag.query",
  51. "--root", "./ragtest",
  52. "--method", "local",
  53. ]
  54. # 安全地添加查询到命令中
  55. cmd.append(shlex.quote(messages))
  56. try:
  57. result = subprocess.run(cmd, capture_output=True, text=True, check=True, encoding='utf-8')
  58. output = result.stdout
  59. if output:
  60. # 提取 "SUCCESS: Local Search Response:" 之后的内容
  61. response = output.split("SUCCESS: Local Search Response:", 1)[-1]
  62. history[-1][1] += response.strip()
  63. yield history
  64. else:
  65. history[-1][1] += "None"
  66. yield history
  67. except subprocess.CalledProcessError as e:
  68. print(e)
  69. with gr.Blocks() as demo:
  70. gr.HTML("""<h1 align="center">GraphRAG 测试</h1>""")
  71. chatbot = gr.Chatbot(height=600)
  72. with gr.Row():
  73. with gr.Column(scale=4):
  74. with gr.Column(scale=12):
  75. user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10, container=False)
  76. with gr.Column(min_width=32, scale=1):
  77. submitBtn = gr.Button("Submit")
  78. def user(query, history):
  79. return "", history + [[parse_text(query), ""]]
  80. submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
  81. predict, [chatbot], chatbot
  82. )
  83. demo.queue()
  84. demo.launch(server_name="0.0.0.0", server_port=9901, inbrowser=True, share=False)

不知道是不是受限于模型能力 还是自己操作问题,个人感觉效果一般 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/943121
推荐阅读
相关标签
  

闽ICP备14008679号