agentic_huge_data_base / wiki
页面 LightRAG · 8.5 示例与快速开始·DeepWiki 中文全文译文

8.5 · 示例与快速开始(Examples and Quick Start)

轻量图谱增强检索 · 聚焦本章的模块关系、源码依据与实现要点。

项目LightRAG 章节8.5 状态全文译文 模块检索、召回与索引、模型调用与提供方适配、图谱与关系、测试、发布与运维
源码线索
  • .gitignore
  • examples/graph_visual_with_html.py
  • examples/graph_visual_with_neo4j.py
  • examples/insert_custom_kg.py
  • examples/lightrag_ag2_multiagent_demo.py
  • examples/lightrag_azure_openai_demo.py
  • examples/lightrag_gemini_demo.py
  • examples/lightrag_ollama_demo.py
  • examples/lightrag_openai_compatible_demo.py
  • examples/lightrag_openai_demo.py
模块标签
  • 检索、召回与索引
  • 模型调用与提供方适配
  • 图谱与关系
  • 测试、发布与运维
  • 界面与交互

章节正文

示例与快速开始

此页面内容来自 DeepWiki 重组后的对应页面(来源:9-3.zh.md)

使用示例

相关源文件

本章引用的主要源码文件:

  • .gitignore
  • examples/generate_query.py
  • examples/insert_custom_kg.py
  • examples/lightrag_azure_openai_demo.py
  • examples/lightrag_ollama_demo.py
  • examples/lightrag_openai_compatible_demo.py
  • examples/lightrag_openai_demo.py
  • examples/lightrag_openai_mongodb_graph_demo.py
  • examples/unofficial-sample/lightrag_bedrock_demo.py
  • examples/unofficial-sample/lightrag_cloudflare_demo.py
  • examples/unofficial-sample/lightrag_hf_demo.py
  • examples/unofficial-sample/lightrag_llamaindex_direct_demo.py
  • examples/unofficial-sample/lightrag_llamaindex_litellm_demo.py
  • examples/unofficial-sample/lightrag_llamaindex_litellm_opik_demo.py
  • examples/unofficial-sample/lightrag_lmdeploy_demo.py
  • examples/unofficial-sample/lightrag_nvidia_demo.py
  • examples/unofficial-sample/lightrag_openai_neo4j_milvus_redis_demo.py
  • lightrag/llm/zhipu.py
  • reproduce/Step_0.py
  • reproduce/Step_1.py
  • reproduce/Step_1_openai_compatible.py
  • reproduce/Step_2.py
  • reproduce/Step_3.py
  • reproduce/Step_3_openai_compatible.py
  • tests/test_zhipu_llm.py

本页面提供了 LightRAG 仓库中官方和社区示例的技术文档。这些示例演示了如何集成各种大语言模型(LLM)提供商、存储后端以及高级编排模式。

官方提供商示例

LightRAG 支持多种大语言模型(LLM)和嵌入向量提供商。以下示例展示了使用特定模型函数初始化 LightRAG 类的标准模式。

OpenAI 及 OpenAI 兼容接口

OpenAI 示例是大多数用户的参考实现。它使用了 gpt_4o_mini_completeopenai_embed examples/lightrag_openai_demo.py:6-7

对于提供 OpenAI 兼容 API 的提供商(如 DeepSeek 或 Upstage),可以在自定义包装器中使用 openai_complete_if_cache 函数 examples/lightrag_openai_compatible_demo.py:88-99

Ollama(本地推理)

Ollama 示例演示了如何配置本地大语言模型(LLM)和嵌入向量模型。它使用了 ollama_model_completeollama_embed examples/lightrag_ollama_demo.py:8-9

  • 上下文管理:通过 llm_model_kwargs 设置 num_ctx examples/lightrag_ollama_demo.py:93
  • 嵌入向量包装:为避免双重包装,在创建自定义 EmbeddingFunc 时通过 ollama_embed.func 访问原始函数 examples/lightrag_ollama_demo.py:103-104
Azure OpenAI

Azure 实现需要为端点和部署名称设置特定的环境变量 examples/lightrag_azure_openai_demo.py:14-21。它使用 openai 库中的标准 AzureOpenAI 客户端来定义自定义的 llm_model_funcembedding_func examples/lightrag_azure_openai_demo.py:32-69

智谱AI(ZhipuAI)

智谱AI集成(lightrag/llm/zhipu.py)包含以下高级功能:

  • 官方思考模式:转发 thinking 参数以启用推理输出 lightrag/llm/zhipu.py:137-138
  • 思维链(CoT):如果 enable_cot 为 True,则将 reasoning_content 格式化为 <think> 标签内的内容 lightrag/llm/zhipu.py:147-150
  • 动态安装:使用 pipmaster 在运行时安装缺失的 zhipuailightrag/llm/zhipu.py:12-13

专用存储示例

虽然默认存储使用本地文件(NetworkX 和 NanoVectorDB),但 LightRAG 提供了适用于生产级数据库的示例。

后端类型示例文件关键实体/配置
图存储examples/lightrag_openai_mongodb_graph_demo.py使用 MongoGraphStorage 进行知识图谱持久化。
向量存储examples/unofficial-sample/lightrag_openai_neo4j_milvus_redis_demo.py结合 MilvusVectorStorageNeo4JStorage
键值存储examples/unofficial-sample/lightrag_openai_neo4j_milvus_redis_demo.py使用 RedisKVStorage 进行缓存和文档状态管理。

自定义知识图谱插入

LightRAG 允许用户绕过标准抽取管线,使用 insert_custom_kg 方法插入预定义的知识图谱 examples/insert_custom_kg.py:115

自定义知识图谱插入的数据流:

  1. 实体:包含 entity_nameentity_typedescription 的字典列表 examples/insert_custom_kg.py:22-59
  2. 关系:包含 src_idtgt_idweightkeywords 的字典列表 examples/insert_custom_kg.py:60-85
  3. 片段:与实体/关系关联的文本段 examples/insert_custom_kg.py:86-112
自定义知识图谱集成图
LightRAG · 自定义知识图谱集成图 · 图 1
LightRAG · 自定义知识图谱集成图 · 图 1

来源:examples/insert_custom_kg.py:15-115lightrag/lightrag.py:1-50

复现与基准测试脚本

reproduce/ 目录包含一个多步骤的管线,用于数据集准备和批量评估。

工作流步骤
  1. 步骤 0(Step_0.py:数据集下载和初始格式化。
  2. 步骤 1(Step_1.py:使用 rag.insert() 将文本上下文批量插入到检索增强生成(RAG)系统中 reproduce/Step_1.py:17
  3. 步骤 2(Step_2.py:基于数据集内容生成测试查询。
  4. 步骤 3(Step_3.py:执行批量查询。它使用正则表达式从文本文件中提取问题 reproduce/Step_3.py:13,并在循环中通过 rag.aquery 运行这些查询 reproduce/Step_3.py:51
批量评估逻辑

脚本 reproduce/Step_3.py 使用 always_get_an_event_loop() 在同步主块中管理异步查询执行 reproduce/Step_3.py:29。它将结果保存到 JSON 文件中,维护成功查询列表和错误日志 reproduce/Step_3.py:31-52

开发者工具

查询生成

examples/generate_query.py 脚本自动化了评估数据集的创建过程。它使用专门的提示词来识别给定数据集的潜在用户和任务,为每个用户/任务组合生成 5 个高级问题 examples/generate_query.py:32-49

实现图:批量处理
LightRAG · 实现图:批量处理 · 图 2
LightRAG · 实现图:批量处理 · 图 2

来源:reproduce/Step_3.py:18-52reproduce/Step_3.py:61-63

非官方社区示例

仓库包含社区贡献的适用于各种环境的示例:

  • Cloudflareexamples/unofficial-sample/lightrag_cloudflare_demo.py
  • Bedrock(AWS)examples/unofficial-sample/lightrag_bedrock_demo.py
  • 多智能体(AG2):与 AG2 智能体集成,用于复杂的检索增强生成(RAG)任务。
  • LlamaIndex 集成:展示如何在 LlamaIndex 管线中将 LightRAG 作为组件使用的示例 examples/unofficial-sample/lightrag_llamaindex_direct_demo.py