最简单的LangChain和RAG

ollama的API:https://docs.ollama.com/api/ps

ollama的API:https://github.com/ollama/ollama/blob/main/docs/api.md?utm_source=chatgpt.com

cpp 复制代码
Ollama API
├── 推理类(最常用)
│   ├── /api/generate   ← 单轮生成
│   ├── /api/chat       ← 多轮对话
│   └── /api/embed      ← 向量 embedding
│
├── 模型管理
│   ├── /api/tags       ← 已有模型
│   ├── /api/pull       ← 下载模型
│   ├── /api/push       ← 推送模型
│   ├── /api/create    ← 构建模型
│   └── /api/delete    ← 删除模型
│
├── 运行控制
│   ├── /api/show      ← 模型信息
│   └── /api/ps        ← 当前运行模型
│
└── 底层服务
    └── /api/version   ← Ollama 版本
cpp 复制代码
curl http://localhost:11434/api/tags
cpp 复制代码
curl http://localhost:11434/api/generate \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-r1:7b",
    "prompt": "用一句话解释什么是UNet",
    "stream": false
  }'

//这个将会是流式的

cpp 复制代码
curl http://localhost:11434/api/chat \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-r1:7b",
    "messages": [
    {"role": "system", "content": "你是一个AI老师"},
    {"role": "user", "content": "解释什么是UNet"}
  ]
  }'

//这会在ollama中创建一个my-deepseek的model,它来自eepseek-r1:7b,与它的区别是,以后每次问题问题之前都会自动加上一句你是AI工程师。

cpp 复制代码
POST /api/create
{
  "name": "my-deepseek",
  "modelfile": "FROM deepseek-r1:7b\nSYSTEM 你是AI工程师"
}

//把input的字符串通过embed模型,转换成ebbedding

cpp 复制代码
curl http://localhost:11434/api/embed   -X POST   -H "Content-Type: application/json" \
-d '{
        "model": "nomic-embed-text",
        "input": "UNet 是什么"
  }'

一个最小的LangChain

//环境准备

cpp 复制代码
curl -fsSL https://ollama.com/install.sh | sh
ollama serve //开启服务
ollama pull deepseek-r1:7b
ollama run deepseek-r1:7b
pip install langchain langchain-community
ollama pull nomic-embed-text  //embedding 模型

//代码

cpp 复制代码
from langchain_community.llms import Ollama

# 连接本地 ollama
llm = Ollama(
    model="deepseek-r1:7b",
    base_url="http://localhost:11434"
)

# 最简单的调用
response = llm.invoke("用一句话解释什么是卷积神经网络") #不会暴露 thinking 不会暴露 context 只拿到response
print(response)

一个最小的RAG

用LangChain做RAG,读取本地数据

//环境准备

cpp 复制代码
curl -fsSL https://ollama.com/install.sh | sh
ollama serve //开启服务
ollama pull deepseek-r1:7b
ollama run deepseek-r1:7b
pip install langchain langchain-community langchain-text-splitters faiss-cpu
ollama pull nomic-embed-text  //embedding 模型

//代码

cpp 复制代码
from langchain_community.llms import Ollama
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA


OLLAMA_URL = "http://localhost:11434"
README_PATH = "/home/yqw/tt/ai/README.md"


# 1. LLM(你原来那套)
llm = Ollama(
    model="deepseek-r1:7b",
    base_url=OLLAMA_URL
)


# 2. 读取 README.md
loader = TextLoader(README_PATH)
documents = loader.load()


# 3. 切分文档
splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=100
)
documents = splitter.split_documents(documents)


# 4. Embedding
embeddings = OllamaEmbeddings(
    model="nomic-embed-text",
    base_url=OLLAMA_URL
)

vectorstore = FAISS.from_documents(documents, embeddings)


# 5. 构建 QA 链
qa = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vectorstore.as_retriever(),
    chain_type="stuff"
)


# 6. 提问
question = "README.md 这个项目是做什么的,同时介绍一下吴亦凡的现状?"

result = qa.invoke({"query": question})
answer = result["result"]

print("Q:", question)
print("A:", answer)
相关推荐
大郭鹏宇4 小时前
基于 LangGraph 构建智能分诊系统(一):项目概述与环境搭建
大数据·人工智能·microsoft·langchain
玉鸯5 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
chaors9 小时前
DeepRearchSystem 0x00:初识
langchain·llm·ai编程
BerryS3N13 小时前
深度解析 LangChain V1.3:架构演进、核心源码剖析与企业级应用落地指南
架构·langchain
大郭鹏宇16 小时前
基于 LangGraph 构建智能分诊系统(三):FastAPI 接口与 Gradio WebUI 实战
大数据·网络·数据库·人工智能·langchain
BraveWang16 小时前
【LangChain 1.x】11、长期记忆|Store 跨会话持久化与向量搜索
langchain
bloxed1 天前
大模型应用-进阶核心技能【13课:第二阶段工程化与部署】
langchain·大模型应用
BraveWang1 天前
【LangChain 1.x】10、短期记忆|checkpointer 持久化与上下文治理三件套
langchain
zl_dfq2 天前
LangChain 之 【内存\Redis\Pinecone向量存储原理与实战】
langchain
吃饱了得干活2 天前
别再手动解析 LLM 输出了!LangChain 四种结构化输出方案对比
后端·python·langchain