使用embedchain快速建立rag知识库,本地大模型

初学RAG搭建知识库,强烈建议先利用embedchain这个开源的RAG框架,短短几行代码就能让你快速建立一个私人知识库,并支持随时增加知识源,是理想的学习和使用RAG知识的开源利器

最简单的解决方式:

  • 框架: Embedchain
  • LLM: Ollama
  • 嵌入式模型: nomic-embed-text 或 bge-m3
  • 向量数据库: ChromaDB (默认)

按照步骤依次进行:

安装ollama等模型:

复制代码
pip install embedchain ollama chromadb sentence-transformers

Windows平台, 需要去https://ollama.com/download 下载 Windows 安装程序(.exe),运行安装,务必在安装过程中勾选"Add Ollama to your PATH"。

下载大模型到本地:

复制代码
ollama pull llama3.2

下载embedding model:

复制代码
ollama pull nomic-embed-text

启动ollama

复制代码
ollama serve
测试ollama是否启动
curl http://localhost:11434/api/tags

设置embedchain :

复制代码
from embedchain import App
import os
os.environ["OPENAI_API_KEY"] = "ollama"  # Ollama 不校验,填任意值
os.environ["OPENAI_API_BASE"] = "http://localhost:11434/v1"
config = {
    "vectordb": {
        "provider": "chroma",
        "config": {
            "collection_name": "ollama_openai_compat"
        }
    },
    "llm": {
        "provider": "openai",          # 使用 openai provider
        "config": {
            "model": "llama3.2",       # 模型名称
            "temperature": 0.1,
            "max_tokens": 2000
        }
    },
    "embedder": {
        "provider": "openai",
        "config": {
            "model": "nomic-embed-text",  # 嵌入模型
        }
    }
}

app = App.from_config(config=config)

增加文档:

复制代码
app.add(r'E:\拿破仑传.pdf', data_type='pdf_file')

增加问题:

复制代码
response = app.query("拿破仑去世时间")
print(response)

完整的代码:

复制代码
from embedchain import App
import os
os.environ["OPENAI_API_KEY"] = "ollama"  # Ollama 不校验,填任意值
os.environ["OPENAI_API_BASE"] = "http://localhost:11434/v1"
config = {
    "vectordb": {
        "provider": "chroma",
        "config": {
            "collection_name": "ollama_openai_compat"
        }
    },
    "llm": {
        "provider": "openai",          # 使用 openai provider
        "config": {
            "model": "llama3.2",       # 模型名称
            "temperature": 0.1,
            "max_tokens": 2000
        }
    },
    "embedder": {
        "provider": "openai",
        "config": {
            "model": "nomic-embed-text",  # 嵌入模型
        }
    }
}

app = App.from_config(config=config)
app.add(r'E:\拿破仑传.pdf', data_type='pdf_file')
response = app.query("拿破仑去世时间")
print(response)

最后输出答案:

复制代码
拿破仑于1815年去世。

embedchain github地址:https://github.com/hypier/embedchain

总结一下遇到的问题:

问题1:依赖包版本不兼容

这个问题最好是单独建立一套虚拟环境,然后在这套虚拟环境下执行上面的命令

问题2:sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table ec_chat_history already exists

这个是因为我在测试时已经建立过一次向量数据库,手动删除embedchain.db后问题解决

相关推荐
doiito9 小时前
【Agent Harness】Gliding Horse 上下文感知与智能压缩:让 Agent 的“注意力”永不偏移
ai·rust·架构设计·系统设计·ai agent
doiito1 天前
【Agent Harness】Gliding Horse L2 作战地图深度优化:给多 Agent 上下文装上“精准导航”
ai·rust·架构设计·系统设计·ai agent
妙妙屋(zy)2 天前
Claude Code+CC-Switch+CC-Connect+飞书使用教程
ai
小七-七牛开发者2 天前
Coding Agent 规则管理:CLAUDE.md、Skills、Hooks、Subagents 到底怎么选?
ai·大模型·agent·claude·token·loop·mcp·claudecode·ai coding
doiito2 天前
左脚踩右脚:让 LLM 自进化的 Agent 轨迹训练法——为什么它能补上主流范式的最后一块拼图
ai·系统设计
带刺的坐椅3 天前
从 Claude Code 隐私争议,看 SolonCode 的设计选择
ai·llm·agent·claudecode·soloncode·codingplan
lincats3 天前
Claude Code项目越写越乱?这套清理流程能救你
ai·ai agent·claude code
匹诺曹i3673 天前
memory_search 对了、7B 总结错了:OpenClaw D4 来源引用与 8 个坑
ollama
云燕实验室CloudLab3 天前
《AI开始"抱团"思考了!多智能体 + 思维图到底有多强?》
ai·学习工具·智慧学伴