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)