本地LLM企业部署2026:Granite 4.2 与 Ollama 实战

·阅读约15分钟·Evergreen Tools Team
Local LLM

💡 工具推荐调试 prompt 与上下文预算时,试试 Evergreen Tools 的 Token计数器JSON格式化工具API测试工具,全部免费!

2026年8月26日,IBM 发布 Granite 4.2——面向自托管的最新开源权重模型家族:3B、8B、30B 三个参数版本,原生 128,000 token 上下文窗口,其中 8B 和 30B 经过 agentic 强化学习训练,具备使用终端、搜索网页、调用外部工具的能力。Ars Technica 的报道指出,这是「Granite 家族中以推理为重点的版本」。对于把数据边界看得比什么都重要的企业,这套组合意味着:推理模型可以跑在自己的机房里。本文给出从 Ollama 拉模型到生产部署的完整实战。

1. 为什么企业今年转向本地 LLM

Granite 4.2 选择了一个微妙的时机:企业对数据主权的要求与对推理能力的渴望同时达到顶峰。本地模型不再是「够用就好」的备选,而是「可预测的企业部署」的主角——没有按 token 计费的账单、没有数据出境、没有第三方停机。128K 原生上下文让本地模型能处理真实的企业文档,而 8B/30B 的 agentic 训练让它们不只是聊天,而是能干活的代理。对金融、医疗、国防等受监管行业来说,多年来的架构约束是「敏感数据不能出境」;一个推理能力接近前沿、又能完全留在内网的模型,一夜之间改变了他们的架构讨论。

// Pull and run Granite 4.2 with Ollama.
// Ars Technica (Aug 26, 2026): Granite 4.2 comes in 3B, 8B, and 30B
// variants with a native 128,000-token context window.
$ ollama pull granite4.2:8b
$ ollama run granite4.2:8b

>>> what is the capital of France?
Paris. (chain-of-thought traces on the reasoning release)

// Programmatic access
from ollama import Client

client = Client(host="http://localhost:11434")
resp = client.chat(
    model="granite4.2:8b",
    messages=[{"role": "user", "content": "Summarize this incident report in 3 bullets."}],
    options={"num_ctx": 128_000},
)
print(resp["message"]["content"])
On-prem Deployment

2. 用 Ollama 五步拉起 Granite 4.2

Ollama 是目前自托管最简单的入口:一条 pull、一条 run 就完成部署。8B 是默认工作马——有 agentic 强化学习训练,显存要求适中;30B 留给硬推理任务;3B 适合边缘和延迟敏感场景。官方 API 与 OpenAI 兼容,现有代码几乎不用改就能切换。需要 128K 上下文时记得在 options 里显式设置 num_ctx。

// The 8B and 30B variants were trained through an agentic
// reinforcement-learning block for terminal use, web search,
// and external tools. Wire them up with native tool calling.
const tools = [
  {
    type: "function",
    function: {
      name: "run_sql",
      description: "Execute a read-only SQL query against the warehouse",
      parameters: {
        type: "object",
        properties: {
          query: { type: "string", description: "The SQL query" },
        },
        required: ["query"],
      },
    },
  },
  {
    type: "function",
    function: {
      name: "search_docs",
      description: "Search internal runbooks",
      parameters: {
        type: "object",
        properties: {
          q: { type: "string" },
        },
        required: ["q"],
      },
    },
  },
];

const run = await client.chat({
  model: "granite4.2:8b",
  messages: [{ role: "user", content: "Which services had p95 latency > 2s yesterday?" }],
  tools,
});

if (run.message.tool_calls) {
  for (const call of run.message.tool_calls) {
    console.log(call.function.name, call.function.arguments);
    // execute, append tool result, and continue the loop
  }
}

3. agentic 能力:工具调用与代理循环

Granite 4.2 的 8B/30B 经过 agentic 强化学习块训练,专门强化了终端使用、网页搜索和外部工具调用。这意味着你可以在本地跑一个完整的代理循环:模型生成工具调用 → 执行 → 把结果喂回去 → 继续推理。对运维团队来说,这等于把「查 SQL、查 runbook、做诊断」这类重复劳动交给一个不出内网的代理。与靠提示词硬凑工具调用的通用模型相比,实战差别在可靠性:模型在训练中专门学过工具调用,产出的函数调用格式远比「被提示词哄着调用工具」的通用模型稳定——当代理能接触到生产系统时,这正是你最需要的性质。

// A minimal on-prem agent loop with the reasoning release.
// "Granite 4.2 is the reasoning-focused release" — chain-of-thought
// and intermediate results are carried forward across steps.
import { Ollama } from "ollama";

const ollama = new Ollama({ host: "http://llm.internal:11434" });

async function agentLoop(task: string, maxSteps = 6) {
  const messages = [
    { role: "system", content: "You are an on-prem operations agent. Reason step by step, then call tools. Never invent telemetry." },
    { role: "user", content: task },
  ];

  for (let step = 0; step < maxSteps; step++) {
    const res = await ollama.chat({ model: "granite4.2:30b", messages, tools: TOOLS });
    messages.push(res.message);

    if (!res.message.tool_calls?.length) {
      return res.message.content; // final answer
    }
    for (const call of res.message.tool_calls) {
      const result = await executeTool(call.function.name, call.function.arguments);
      messages.push({ role: "tool", content: JSON.stringify(result) });
    }
  }
  throw new Error("step limit exceeded");
}

4. 推理优先版本意味着什么

「推理」在这里不是玄学,而是功能性的 chain-of-thought:模型把中间结果逐步向前传递,多步推理更严谨。代价是更慢的响应和更高的算力需求。在选型上,这要求你把「需要严谨推理」的任务(诊断、规划、代码审查)与「需要速度」的任务(摘要、分类、改写)分开,分别路由到 30B 和 8B,甚至 3B。

// 128K context: use it deliberately, not by accident.
// Long context means slower inference and higher memory. Budget it.

function estimateTokens(text: string): number {
  // ~4 chars per token is a decent heuristic for English
  return Math.ceil(text.length / 4);
}

function trimToBudget(docs: string[], budgetTokens = 96_000): string {
  let used = 0;
  const kept: string[] = [];
  for (const d of docs) {
    const t = estimateTokens(d);
    if (used + t > budgetTokens) break;
    kept.push(d);
    used += t;
  }
  return kept.join("\n\n");
}

// Reserve the rest of the window for the task, tool results, and
// chain-of-thought intermediates. 128K is a ceiling, not a default.
Model Deployment

5. 128K 上下文:天花板不是默认值

128K 是能力上限,不是使用建议。上下文越长,推理越慢、显存越高。生产实践是给上下文做预算:按文档重要性截断到 96K 以内,把窗口留给任务描述、工具结果和思维链中间产物。一个小技巧:4 个字符约等于 1 个 token,写个估算函数就能在塞进窗口前先裁剪。

// Enterprise deployment checklist (Granite 4.2, self-hosted)
# 1. Model registry
ollama pull granite4.2:3b   # edge / latency-sensitive
ollama pull granite4.2:8b   # default workhorse (agentic RL)
ollama pull granite4.2:30b  # hard reasoning tasks

# 2. Quantization for smaller footprints
ollama pull granite4.2:8b-q4_K_M   # ~5GB, minimal quality loss

# 3. Serving config (predictable enterprise deployment)
OLLAMA_HOST=0.0.0.0:11434
OLLAMA_NUM_PARALLEL=4
OLLAMA_MAX_LOADED_MODELS=2
OLLAMA_KEEP_ALIVE=30m

# 4. Governance: log every request for audit
{
  "audit": {
    "log_all_prompts": true,
    "log_tool_calls": true,
    "retention_days": 90
  }
}

6. 生产部署清单

把 3B/8B/30B 分别部署到边缘、默认、硬任务三个层;需要更小 footprint 就上 q4_K_M 量化;用 OLLAMA_NUM_PARALLEL 和 KEEP_ALIVE 调吞吐;最重要的是治理:全量记录 prompt 与工具调用日志,保留 90 天。IBM 强调的「可预测的企业部署」不是口号——它是配置、量化、审计三件套的组合。

📌 常见问题 FAQ

Granite 4.2 有哪些版本?

3B、8B、30B 三个参数版本,都是 decoder-only 架构,原生 128,000 token 上下文。8B 和 30B 经过 agentic 强化学习训练,3B 支持工具但没有同等水平的专门训练。

为什么选本地模型而不是 API?

数据不出境、没有按 token 计费、没有第三方停机,符合企业数据主权要求。Granite 4.2 的 128K 上下文和 agentic 能力让本地模型首次能处理真实企业任务。

8B 和 30B 怎么选?

8B 是默认工作马:agentic 训练完整、显存适中,适合大多数任务。30B 留给需要严谨多步推理的任务。3B 适合边缘设备和延迟敏感场景。

128K 上下文要怎么用?

把它当天花板而不是默认值。上下文越长推理越慢、显存越高。生产实践是给上下文做预算,文档截断到 96K 以内,把窗口留给任务描述、工具结果和思维链中间产物。

量化会影响质量吗?

q4_K_M 级别的量化对大多数任务质量损失极小,换来约一半的显存占用。推理密集型任务建议用未量化或 q8 版本,可以在评估集上对比后再决定。