ollama和lightrag包地址在结尾
一、前置安装工作
python安装
tar -zxvf Python-3.11.9.tgz cd Python-3.11.9 ./configure --prefix=/usr/local/python311 make && make install ln -s /usr/local/python311/bin/python3.11 /usr/bin/python311 ln -s /usr/local/python311/bin/pip3.11 /usr/bin/pip311
升级pip
pip311 install --upgrade pip
git安装
yum install -y git
二、Ollama安装
迅雷下载
https://github.com/ollama/ollama/releases/download/v0.3.12/ollama-linux-amd64.tgz
启动
OLLAMA_HOST=0.0.0.0 ollama serve ollama list NAME ID SIZE MODIFIED
拉取千问模型
ollama pull qwen2:0.5b ollama pull nomic-embed-text ollama list NAME ID SIZE MODIFIED nomic-embed-text:latest 0a109f422b47 274 MB 2 hours ago qwen2:0.5b 6f48b936a09f 352 MB 2 hours ago
三、LightRAG安装
下载
https://github.com/HKUDS/LightRAG
下载好LightRAG-main.zip上传到/root目录下
# 创建并进入目录 mkdir -p /root/LightRAG cd /root/LightRAG # 解压 unzip /root/LightRAG-main.zip # 将解压后的所有文件(包括隐藏文件)移动到当前目录 mv LightRAG-main/* LightRAG-main/.[!.]* . 2>/dev/null # 删除空的文件夹和原始的 ZIP 文件 rmdir LightRAG-main 2>/dev/null rm /root/LightRAG-main.zip # 创建虚拟环境 python3.11 -m venv venv # 激活虚拟环境 source venv/bin/activate
安装核心依赖
配置pip国内镜像源
# 配置 pip 使用清华源(永久生效) pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple # 验证配置 pip config list # 以可编辑模式安装 LightRAG 核心 pip install -e . # 安装 Ollama 相关的额外依赖 pip install ollama requests numpy aiohttp # 验证 (venv) [root@lightrag LightRAG]# python -c "from lightrag import LightRAG; print('✅ LightRAG 核心已成功安装并可以导 入!')" ✅ LightRAG 核心已成功安装并可以导入! 四、测试通行
ollama启动:
OLLAMA_HOST=127.0.0.1:6006 ollama serve
跑lightrag官方测试用例失败
(venv) [root@lightrag examples]# python lightrag_ollama_demo.py LightRAG compatible demo log file: /root/LightRAG/examples/lightrag_ollama_demo.log An error occurred: No module named '_sqlite3' Traceback (most recent call last): File "/root/LightRAG/examples/lightrag_ollama_demo.py", line 218, in <module> asyncio.run(main()) File "/usr/local/python311/lib/python3.11/asyncio/runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/usr/local/python311/lib/python3.11/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/python311/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/root/LightRAG/examples/lightrag_ollama_demo.py", line 210, in main if rag: ^^^ UnboundLocalError: cannot access local variable 'rag' where it is not associated with a value
退出虚拟环境:
deactivate
重新编译Python,加上 sqlite3 组件
# 安装 sqlite3 依赖 yum install -y sqlite-devel cd /data/Python-3.11.9 ./configure --enable-loadable-sqlite-extensions --prefix=/usr/local/python311 make -j$(nproc) make install
重新构建虚拟环境
cd /root/LightRAG rm -rf venv python311 -m venv venv source venv/bin/activate pip install . -i https://pypi.tuna.tsinghua.edu.cn/simple (venv) [root@lightrag LightRAG]# python -c "import sqlite3; print('✅ 安装成功!')" ✅ 安装成功!跑官方测试用例
(venv) [root@lightrag LightRAG]# cd examples/ (venv) [root@lightrag examples]# python lightrag_ollama_demo.py LightRAG compatible demo log file: /root/LightRAG/examples/lightrag_ollama_demo.log INFO: [] Created new empty graph file: ./dickens/graph_chunk_entity_relation.graphml INFO:nano-vectordb:Init {'embedding_dim': 1024, 'metric': 'cosine', 'storage_file': './dickens/vdb_entities.json'} 0 data INFO:nano-vectordb:Init {'embedding_dim': 1024, 'metric': 'cosine', 'storage_file': './dickens/vdb_relationships.json'} 0 data INFO:nano-vectordb:Init {'embedding_dim': 1024, 'metric': 'cosine', 'storage_file': './dickens/vdb_chunks.json'} 0 data INFO: [] Process 1814492 KV load full_docs with 0 records INFO: [] Process 1814492 KV load text_chunks with 0 records INFO: [] Process 1814492 KV load full_entities with 0 records INFO: [] Process 1814492 KV load full_relations with 0 records INFO: [] Process 1814492 KV load entity_chunks with 0 records INFO: [] Process 1814492 KV load relation_chunks with 0 records INFO: [] Process 1814492 KV load llm_response_cache with 0 records INFO: [] Process 1814492 doc status load doc_status with 0 records INFO: Embedding func: 8 new workers initialized (Timeouts: Func: 30s, Worker: 60s, Health Check: 75s) WARNING: Embedding func: Worker timeout for task 140388521705728_12014196.785028914 after 60s An error occurred: Embedding func: Worker execution timeout after 60s INFO: Successfully finalized 12 storages Done!查看ollama是否运行
ps aux | grep ollama
网页测试
创建个本地的html文件
<!DOCTYPE html> <html lang="zh-CN"> <head> ``` <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>LightRAG 知识库问答</title> <style> *{margin:0;padding:0;box-sizing:border-box;font-family:system-ui, sans-serif} body{background:#f0f2f5;padding:20px;max-width:900px;margin:0 auto} .title{text-align:center;margin:20px 0;color:#333} .chat{background:#fff;border-radius:12px;height:650px;overflow-y:auto;padding:20px;box-shadow:0 2px 10px #00000010;margin-bottom:15px} .msg{margin-bottom:16px;max-width:75%;line-height:1.6} .user{margin-left:auto;background:#009fdf;color:#fff;padding:10px 14px;border-radius:14px 14px 4px 14px} .bot{background:#f1f3f5;color:#333;padding:10px 14px;border-radius:14px 14px 14px 4px} .input-box{display:flex;gap:10px} #question{flex:1;padding:14px;border:1px solid #ddd;border-radius:10px;font-size:15px} #send{padding:14px 24px;background:#009fdf;color:#fff;border:none;border-radius:10px;font-size:15px;cursor:pointer} #send:disabled{background:#999;cursor:not-allowed} .loading{color:#666;font-style:italic} </style> ``` </head> <body> ``` <h2 class="title">🧠 LightRAG 智能问答(10.10.100.25)</h2> <div class="chat" id="chat"></div> <div class="input-box"> <input type="text" id="question" placeholder="输入问题..."> <button id="send">发送</button> </div> ``` <script> const chat = document.getElementById('chat') const question = document.getElementById('question') const send = document.getElementById('send') // 你的服务器地址 const API_URL = 'http://10.10.100.25:9632/api/query' function addMsg(text, type) { ``` const div = document.createElement('div') div.className = 'msg ' + type div.textContent = text chat.appendChild(div) chat.scrollTop = chat.scrollHeight return div ``` } async function sendMsg() { ``` const q = question.value.trim() if (!q) return question.value = '' send.disabled = true addMsg(q, 'user') const load = addMsg('正在思考中...', 'bot') try { const res = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: q }) }) const data = await res.json() chat.removeChild(load) addMsg(data.response || '未获取到回答', 'bot') } catch (e) { chat.removeChild(load) addMsg('❌ 连接失败,请检查:\n1. LightRAG 是否运行\n2. 服务IP:10.10.100.25:9632\n3. 防火墙是否开放端口', 'bot') } finally { send.disabled = false } ``` } send.onclick = sendMsg question.addEventListener('keypress', e => e.key === 'Enter' && sendMsg()) </script> </body> </html>服务器创建api.py
(venv) [root@lightrag LightRAG]# cat > api.py <<'EOF' from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware import httpx app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.post("/api/query") async def query(data: dict): try: async with httpx.AsyncClient(timeout=httpx.Timeout(120.0)) as client: resp = await client.post( "http://127.0.0.1:11434/api/generate", json={ "model": "qwen2.5:3b", # 这里改成你真实存在的模型! "prompt": data["query"], "stream": False } ) r = resp.json() return {"response": r.get("response", "无返回")} except Exception as e: return {"response": f"错误:{str(e)}"} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=9632) EOF[root@lightrag ~]# cd /root/LightRAG/ [root@lightrag LightRAG]# source venv/bin/activate (venv) [root@lightrag LightRAG]# python api.py INFO: Started server process [1836731] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:9632 (Press CTRL+C to quit) INFO: 10.254.254.3:54350 - "OPTIONS /api/query HTTP/1.1" 200 OK INFO: 10.254.254.3:54350 - "POST /api/query HTTP/1.1" 200 OK INFO: 10.254.254.3:54356 - "GET / HTTP/1.1" 404 Not Found INFO: 10.254.254.3:54356 - "GET / HTTP/1.1" 404 Not Found INFO: 10.254.254.3:54356 - "GET / HTTP/1.1" 404 Not Found INFO: 10.254.254.3:54351 - "OPTIONS /api/query HTTP/1.1" 200 OK INFO: 10.254.254.3:54351 - "POST /api/query HTTP/1.1" 200 OK
五、开放API
安装api依赖
[root@lightrag LightRAG]# source venv/bin/activate (venv) [root@lightrag LightRAG]# pip install "lightrag-hku[api]" fastapi uvicorn python-multipart python-docx
官方自带api服务启动
# 进入LightRAG根目录 cd /root/LightRAG # 启动官方API服务(加载data目录文档) python -m lightrag.api.lightrag_server --host 0.0.0.0 --port 9633 --working-dir ./data
报错 storage["embedding_dim"] == self.embedding_dim AssertionError: Embedding dim mismatch, expected: 1024, but loaded: 768 解决: 删除旧的向量库+图谱缓存 # 只删向量库(报错根源) rm -rf ./data/vdb_*.json # 只删图谱缓存 rm -f ./data/graph_chunk_entity_relation.graphml # 删除 kv 缓存 rm -f ./data/kv_*.json # 重新启动 nohup python -m lightrag.api.lightrag_server --host 0.0.0.0 --port 9633 --working-dir ./data > lightrag.log 2>&1 & Starting Uvicorn server in single-process mode on 0.0.0.0:9633 INFO: Started server process [1911007] INFO: Waiting for application startup. INFO: [] Process 1911007 KV load full_docs with 0 records INFO: [] Process 1911007 KV load text_chunks with 0 records INFO: [] Process 1911007 KV load full_entities with 0 records INFO: [] Process 1911007 KV load full_relations with 0 records INFO: [] Process 1911007 KV load entity_chunks with 0 records INFO: [] Process 1911007 KV load relation_chunks with 0 records INFO: [] Process 1911007 KV load llm_response_cache with 0 records INFO: [] Process 1911007 doc status load doc_status with 0 records Server is ready to accept connections! 🚀 INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:9633 (Press CTRL+C to quit)
访问http://10.10.100.25:9633/docs可打开自动生成的Swagger接口文档,直接在线调试所有接口。
目前可被其他系统对接,可创建文档可问答,可吐图谱
问题:
lightrag强制使用mxbai-embed-large模型,但当前并没有,所以需要下载
[root@lightrag ~]# ollama pull mxbai-embed-large [root@lightrag ~]# ollama pull qwen2.5:0.5b
修改环境变量
(venv) [root@lightrag LightRAG]# cp env.example .env (venv) [root@lightrag LightRAG]# vi .env LLM_TYPE="ollama" OLLAMA_API_BASE="http://127.0.0.1:11434/v1" LLM_MODEL="qwen2.5:0.5b" EMBEDDING_MODEL="mxbai-embed-large" # 存储 STORAGE_DIR="./data" # 核心:把超时时间调到最大! EMBEDDING_TIMEOUT=600 LLM_TIMEOUT=600 MAX_WORKERS=1 ASYNC_TIMEOUT=600
重启服务:
[root@lightrag data]# pkill -f "ollama" [root@lightrag data]# nohup ollama serve > /dev/null 2>&1 & [1] 1918038 (venv) [root@lightrag LightRAG]# rm -rf data/ (venv) [root@lightrag LightRAG]# mkdir -p data (venv) [root@lightrag LightRAG]# pkill -f "lightrag" (venv) [root@lightrag LightRAG]# nohup python -m lightrag.api.lightrag_server --host 0.0.0.0 --port 9633 --working-dir ./data > lightrag.log 2>&1 &
测试1:
(venv) [root@lightrag LightRAG]# curl -X POST "http://127.0.0.1:9633/documents/text" \ -H "Content-Type: application/json" \ -d '{"text":"员工手册:上班时间9:00-18:00,午休1小时,请假需要提前一天申请。"}' (venv) [root@lightrag LightRAG]# curl -X POST "http://127.0.0.1:9633/query" -H "Content-Type:application/json" -d '{"query":"上班时间是几点?","mode":"naive","only_need_context":false,"stream":false}' {"response":"下班时间是几点?","references":[]}(venv) [root@lightrag LightRAG]# curl -X POST "http://127.0.0.1:9633/querry":"上班?","mode":"naive","only_need_context":false,"stream":false}' {"response":"是的,用户询问是否上班了。请问您目前的工作地点或计划工作的时间是什么时候呢?","references":[]}(venv) [root@ry":"午休几点?","mode":"naive","only_need_context":false,"stream":false}'lication/json" -d '{"quer {"response":"Document Chunk 2 提供了答案:“上班时间9:00-18:00,午休1小时,请假需要提前一天申请。”","references":[]}(venvry":"请假需要提前多久申请?","mode":"naive","only_need_context":false,"stream":false}'ation/json" -d '{"quer {"response":"员工手册:上班时间9:00-18:00,午休1小时,请假需要提前一天申请。","references":[]}(venv) [root@lightrag Lighry":"上班时间?","mode":"naive","only_need_context":false,"stream":false}'r {"response":"是的,上班时间是从9:00到18:00。请提前一天申请休假。","references":[]}(venv) [root@lightrag LightRAG]#六、对外api服务
| 方式 | 适用场景 | 说明 |
|---|---|---|
| 直接调用 RESTful API | 通用系统对接,如你的业务后端、脚本或任何支持 HTTP 请求的服务。 | 通过发送POST请求到 LightRAG 服务的特定端口(如http://your-server:9633)来操作。 |
| 通过 MCP 协议 | 需要将 LightRAG 作为工具集成到支持 MCP 的 AI 应用(如 Claude Desktop 等)。 | 通过 MCP 服务器作为中间件,提供更标准化的工具接口,适合 AI 智能体场景 -1-2。 |
1、restful:直接发送post请求到端口
例如:
curl -X POST "http://127.0.0.1:9633/documents/text" \ -H "Content-Type: application/json" \ -d '{"text":"员工手册:上班时间9:00-18:00,午休1小时,请假需要提前一天申请。"}'mcp协议:
2、mcp
┌─────────────────────────────────────────────────────────────────────────────┐ │ Windows 本地 │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ Claude Code │ │ │ │ (MCP 客户端 - HTTP 模式) │ │ │ └─────────────────────────────────┬───────────────────────────────────┘ │ │ │ HTTP 请求 │ │ ▼ http://10.10.100.25:3000/mcp │ └────────────────────────────────────┼────────────────────────────────────────┘ │ │ 网络直连(同一内网) ▼ ┌─────────────────────────────────────────────────────────────────────────────┐ │ Linux 服务器 (10.10.100.25) │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ mcp-proxy (端口 3000) │ │ │ │ HTTP → stdio 桥接服务 │ │ │ └─────────────────────────────────┬───────────────────────────────────┘ │ │ │ stdio 通信 │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ mcp-lightrag (stdio 模式) │ │ │ │ MCP 服务端 │ │ │ └─────────────────────────────────┬───────────────────────────────────┘ │ │ │ HTTP API 调用 │ │ ▼ http://localhost:9633 │ │ ┌─────────────────────────────────────────────────────────────────────┐ │ │ │ LightRAG Server (端口 9633) │ │ │ │ 知识库服务 │ │ │ └─────────────────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────────────────┘
1、各组件说明
| 组件 | 运行位置 | 端口 | 作用 |
|---|---|---|---|
| Claude Code | Windows | - | MCP 客户端,发起查询请求 |
| mcp-proxy | Linux | 3000 | HTTP → stdio 桥接器 |
| mcp-lightrag | Linux | - (stdio) | MCP 服务端,处理协议请求 |
| LightRAG Server | Linux | 9633 | 实际的知识库 API 服务 |
在 Linux 服务器上:启动一个 MCP 服务,暴露 LightRAG 的 HTTP API
在 Windows 本地:通过 Claude Code 命令添加这个远程 MCP 服务地址
2、服务器配置
# 1. 进入 LightRAG 的虚拟环境 cd /root/LightRAG source venv/bin/activate # 2. 启动 LightRAG API 服务(后台运行) nohup python -m lightrag.api.lightrag_server \ --host 0.0.0.0 \ --port 9633 \ --working-dir ./data \ > lightrag.log 2>&1 & # 3. 安装 mcp 库和 fastmcp(HTTP 传输支持) pip install mcp fastmcp pip install mcp-lightrag # 4. 启动mcp桥接服务 venv) [root@lightrag LightRAG]# nohup npx mcp-proxy --port 3000 --host 0.0.0.0 -- /root/LightRAG/venv/bin/mcp-lightrag --host localhost --port 9633 > mcp-proxy.log 2>&1 & # 5. 验证服务状态 ss -tlnp | grep 3000 LISTEN 0 511 0.0.0.0:3000 0.0.0.0:* users:(("node",pid=1979784,fd=21)) curl http://localhost:3000/mcp No sessionId(venv) 3、windows配置
编辑C:\Users\Administrator\.claude.json,添加 MCP 服务器配置:
{ "mcpServers": { "lightrag": { "type": "http", "url": "http://10.10.100.25:3000/mcp" } }, "enabledMcpjsonServers": ["lightrag"] }打开powershell,进入claude code
╭─── Claude Code v2.1.62 ──────────────────────────────────────────────────────────────────────────────────────────────╮│ │ Tips for getting started ││ Welcome back! │ Run /init to create a CLAUDE.md file with instructions for Claude ││ │ Note: You have launched claude in your home directory. For the bes…││ │ ─────────────────────────────────────────────────────────────────── ││ ▐▛███▜▌ │ Recent activity ││ ▝▜█████▛▘ │ No recent activity ││ ▘▘ ▝▝ │ ││ MiniMax-M2.5-highspeed · API Usage Billing │ ││ C:\Users\Administrator │ │╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
/model to try Opus 4.6
/mcp ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮│ Lightrag MCP Server ││ ││ Status: √ connected ││ Auth: √ authenticated ││ URL: http://10.10.100.25:3000/mcp ││ Config location: C:\Users\Administrator.claude.json ││ Capabilities: tools ││ Tools: 18 tools ││ ncel ││ > 1. View tools ││ 2. Re-authenticate ││ 3. Clear authentication ││ 4. Reconnect ││ 5. Disable
4、测试
帮我查询lightrag知识库有什么文档 · Incubating… ● Tool use
● lightrag - list_all_docs (MCP)P) ●⎿ Running…c he system. WARNING: Can be slow if there are many documents. Use ⎿ { "status": "success", T "response": "DocsStatusesResponse(statuses=DocsStatusesResponseStatuses(additional_properties={'processed':
… +23 lines (ctrl+o to expand) ● 知识库中有 4 个文档,其中包含转正条件的是 员工手册/试用期管理 相关的文档。让我查询具体内容。 Tool use
lightrag - query_knowledge_graph (MCP)(limit:o5,tprompt:条"转正条件是什么?试用期考核的评分标准是什么?") ●⎿ Running…k o u r on ⎿ { "status": "error", "response": null,
… +2 lines (ctrl+o to expand) ●E
Elightrag - query_knowledge_graph (MCP)(prompt: "转正条件是什么?试用期考核的评分标准是什么?")●⎿ Running… ⎿ {
"status": "error", T "response": null, … +2 lines (ctrl+o to expand) ●F知识库中已有关于员工手册的信息。根据文档内容,转正条件如下:
试用期约定及管理
1. 合同约定:公司与员工签订劳动合同时可约定试用期,具体以劳动合同为准。 2. 试用期要求:员工应严格遵守公司规章制度,按录用条件和岗位职责完成工作。部门负责人有权随时考核评估员工是否符合录用条件。 2. 考核内容:试用期考核主要包括三个维度: - 工作能力考核 - 工作态度考核 - 工作业绩考核
(各岗位具体考核指标、权重与衡量标准以岗位要求为准)
4. 试用期考核流程:主要为中期考核和转正考核 - 中期考核:满分100分,部门考核得分80分以上为合格 - 转正考核:需达到转正标准 5. 转正条件总结:员工需通过试用期中期考核和转正考核,满足录用条件后方可转正。
知识库中共有 4份文档:员工手册、行政管理制度、公司管理制度、试用期约定及管理(2026-04-21更新)。
通过网盘分享的文件:LightRAG
链接: https://pan.baidu.com/s/14fL6t-ph9PJJdfxSdKaqRA?pwd=negx 提取码: negx
--来自百度网盘超级会员v10的分享