Ollama 2026:让本地大模型触手可及 Ollama 在 2026 年已经成为"本地运行大模型"的代名词。这个由 Go 语言编写的轻量级工具,让任何人都能在自己的电脑上运行大语言模型——无需 GPU 集群,无需复杂配置,一条命令即可开始。截至 2026 年 6 月,Ollama 累计下载量超过 2000 万次,月活用户超过 300 万。
2026 核心特性 版本亮点 特性 Ollama 0.1 (2024) Ollama 0.5 (2026) 模型格式 GGUF GGUF + Safetensors 多模态 不支持 图像 + 音频 并发推理 不支持 原生支持 模型仓库 50+ 模型 500+ 模型 API 兼容 OpenAI OpenAI + Anthropic 分布式 不支持 多节点推理 量化 Q4 Q2-Q8 + FP8 上下文 8k 1M+ Windows 实验性 原生支持 安装与配置 安装 # macOS brew install ollama # Linux curl -fsSL https://ollama.com/install.sh | sh # Windows (PowerShell) winget install Ollama.Ollama # Docker docker run -d --name ollama -p 11434:11434 -v ollama_data:/root/.ollama ollama/ollama:0.5 配置 # 环境变量配置 export OLLAMA_HOST=0.0.0.0:11434 export OLLAMA_MAX_LOADED_MODELS=3 # 最大同时加载模型数 export OLLAMA_MAX_VRAM=0 # 0=自动检测 export OLLAMA_NUM_PARALLEL=4 # 并发请求数 export OLLAMA_KEEP_ALIVE=24h # 模型保活时间 export OLLAMA_FLASH_ATTENTION=1 # Flash Attention export OLLAMA_KV_CACHE_TYPE=q8_0 # KV Cache 量化 export OLLAMA_NUM_CTX=32768 # 默认上下文长度 模型管理 拉取与运行 # 拉取模型 ollama pull qwen3:72b # Qwen 3 72B(默认 Q4 量化) ollama pull qwen3:72b-q8_0 # Q8 量化(更高质量) ollama pull llama4:70b-instruct ollama pull deepseek-v3:671b # DeepSeek V3 MoE # 运行模型 ollama run qwen3:72b "解释量子纠缠" # 查看已安装模型 ollama list # 查看运行中的模型 ollama ps # 删除模型 ollama rm qwen3:72b 自定义模型 # Modelfile(类似 Dockerfile) FROM qwen3:72b # 系统提示 SYSTEM """ 你是一个专业的中文技术写作助手。你的任务是: 1. 撰写清晰、准确的技术文档 2. 使用中文,技术术语保留英文 3. 包含代码示例和对比表格 4. 保持客观、专业的语气 """ # 参数 PARAMETER temperature 0.3 PARAMETER top_p 0.9 PARAMETER num_ctx 32768 PARAMETER repeat_penalty 1.1 PARAMETER stop "<|im_end|>" # 模板 TEMPLATE """ {{ if .System }}<|im_start|>system {{ .System }}<|im_end|> {{ end }}<|im_start|>user {{ .Prompt }}<|im_end|> <|im_start|>assistant """ # 工具定义 TOOL search_web { "description": "搜索互联网", "parameters": { "type": "object", "properties": { "query": {"type": "string"} } } } # 构建自定义模型 ollama create tech-writer -f Modelfile # 运行 ollama run tech-writer "写一篇关于 RAG 架构的文章" API 使用 REST API import httpx import json import asyncio class OllamaClient: def __init__(self, base_url="http://localhost:11434"): self.base_url = base_url self.client = httpx.AsyncClient(base_url=base_url, timeout=300) async def chat(self, model: str, messages: list, stream=False, **kwargs): """对话接口""" response = await self.client.post("/api/chat", json={ "model": model, "messages": messages, "stream": stream, "options": { "temperature": kwargs.get("temperature", 0.7), "top_p": kwargs.get("top_p", 0.9), "num_ctx": kwargs.get("num_ctx", 32768), }, "tools": kwargs.get("tools", []), }) return response.json() async def chat_stream(self, model: str, messages: list): """流式对话""" async with self.client.stream("POST", "/api/chat", json={ "model": model, "messages": messages, "stream": True, }) as response: async for line in response.aiter_lines(): data = json.loads(line) if data.get("message", {}).get("content"): yield data["message"]["content"] if data.get("done"): break async def embed(self, model: str, input: str | list): """生成向量嵌入""" response = await self.client.post("/api/embed", json={ "model": model, "input": input, }) return response.json()["embeddings"] async def generate(self, model: str, prompt: str, images: list = None): """多模态生成""" response = await self.client.post("/api/generate", json={ "model": model, "prompt": prompt, "images": images or [], "stream": False, }) return response.json() # 使用 client = OllamaClient() # 对话 result = await client.chat( model="qwen3:72b", messages=[ {"role": "system", "content": "你是专业翻译"}, {"role": "user", "content": "翻译:AGI will change everything"} ], temperature=0.3 ) # 流式输出 async for chunk in client.chat_stream( model="qwen3:72b", messages=[{"role": "user", "content": "写一首诗"}] ): print(chunk, end="", flush=True) # 向量嵌入 embeddings = await client.embed( model="bge-m3", input=["你好世界", "Hello World"] ) OpenAI 兼容接口 from openai import OpenAI # Ollama 兼容 OpenAI API client = OpenAI( base_url="http://localhost:11434/v1", api_key="ollama" # 任意值即可 ) response = client.chat.completions.create( model="qwen3:72b", messages=[{"role": "user", "content": "Hello!"}], stream=True ) 工具调用 # Ollama 2026 原生支持工具调用 result = await client.chat( model="qwen3:72b", messages=[{"role": "user", "content": "北京今天天气怎么样?"}], tools=[{ "type": "function", "function": { "name": "get_weather", "description": "获取天气信息", "parameters": { "type": "object", "properties": { "city": {"type": "string"} }, "required": ["city"] } } }] ) # 模型返回工具调用 if result.get("message", {}).get("tool_calls"): tool_call = result["message"]["tool_calls"][0] print(f"调用工具: {tool_call['function']['name']}") print(f"参数: {tool_call['function']['arguments']}") 性能优化 量化选择 量化 显存/内存 质量 速度 推荐场景 Q2_K 最低 差 最快 测试/验证 Q4_0 低 中 快 低配设备 Q4_K_M 中低 良 快 日常使用(推荐) Q5_K_M 中 好 中 质量优先 Q8_0 高 优 中 最高质量 FP16 最高 最优 慢 无量化 硬件适配 # CPU 推理优化 export OLLAMA_NUM_THREAD=8 # CPU 线程数 export OLLAMA_NUM_CTX=8192 # 降低上下文长度 # GPU 推理优化 export OLLAMA_GPU_LAYERS=80 # GPU 层数(-1 全部) export OLLAMA_FLASH_ATTENTION=1 # Flash Attention export OLLAMA_KV_CACHE_TYPE=q8_0 # KV Cache 量化 # Apple Silicon 优化 export OLLAMA_METAL_GPU=1 # 使用 Metal export OLLAMA_KV_CACHE_TYPE=q4_0 # 统一内存节省 export OLLAMA_NUM_CTX=16384 # 适合 M 芯片 性能对比 硬件 模型 量化 速度 (tok/s) 首Token延迟 内存占用 M3 Max 64GB Qwen3-72B Q4_K_M 18.5 2.1s 42 GB M3 Max 64GB Qwen3-32B Q4_K_M 45.2 0.8s 20 GB RTX 4090 24GB Qwen3-14B Q4_K_M 85.3 0.3s 9 GB RTX 4090 24GB Qwen3-32B Q4_K_M 42.1 0.5s 20 GB 2×A100 80GB Qwen3-72B Q8_0 55.8 0.4s 75 GB CPU 32-core Qwen3-7B Q4_K_M 12.3 1.5s 5 GB 与应用集成 与 LangChain 集成 from langchain_ollama import ChatOllama, OllamaEmbeddings # 对话模型 llm = ChatOllama( model="qwen3:72b", temperature=0.7, base_url="http://localhost:11434" ) # 嵌入模型 embeddings = OllamaEmbeddings( model="bge-m3", base_url="http://localhost:11434" ) # 使用 from langchain_core.messages import HumanMessage response = llm.invoke([HumanMessage(content="你好")]) 与 Dify 集成 # Dify 配置 Ollama model_provider: name: ollama credentials: base_url: http://localhost:11434 models: - name: qwen3:72b type: llm context_size: 32768 - name: bge-m3 type: text-embedding context_size: 8192 多节点部署 # Ollama 集群配置(2026 新特性) # ollama-cluster.yaml nodes: - name: node-1 host: 192.168.1.101 port: 11434 gpu: "RTX 4090" models: ["qwen3:32b", "bge-m3"] - name: node-2 host: 192.168.1.102 port: 11434 gpu: "RTX 4090" models: ["qwen3:32b"] # 负载均衡 - name: node-3 host: 192.168.1.103 port: 11434 gpu: "A100 80GB" models: ["qwen3:72b"] # 大模型专用 # 路由策略 router: strategy: "least_loaded" # 最少负载 health_check_interval: 10 failover: true 适用场景 最适合 隐私敏感场景:数据不出本地的安全需求 离线环境:无网络或网络不稳定的环境 开发测试:快速测试不同模型的表现 边缘计算:IoT 设备、边缘服务器上的 AI 个人使用:在自己的电脑上运行 AI 助手 不太适合 高并发生产:吞吐量不如 vLLM/TGI 超大模型:671B 级别模型需要集群 成本敏感:相比云端 API,电费和硬件成本较高 总结 Ollama 在 2026 年仍然是"本地运行大模型"的最佳选择。它的核心价值不在于性能极致——vLLM 和 TensorRT-LLM 在吞吐量上更优——而在于"简单"。一条命令安装,一条命令运行,OpenAI 兼容 API,跨平台支持,这些特性让 Ollama 成为开发者在本地使用大模型的首选。
...