在桌面应用和编码代理中使用 Ollama,或使用 API 密钥构建应用程序。

开始使用

下载 Ollama,支持 macOS、Windows 或 Linux。打开该应用,或从终端开始使用:

ollama

按照设置提示操作。登录以使用云端模型,或选择本地模型。

连接桌面应用

在 macOS 上,打开 Ollama 并选择 Apps(应用)。连接 Claude 或 ChatGPT (Desktop)。按照提示安装或重启该应用。

在 Settings → Apps(设置 → 应用)中选择你的 Ollama 模型。

  • Claude Desktop — 在 Claude 中使用 Ollama 模型。
  • ChatGPT Desktop — 在 Codex 模式下使用 Ollama 模型。常规 Chat 和语音功能仍使用你常规的 ChatGPT 连接。

使用编码代理

从项目目录启动你的代理:

Claude Code

ollama launch claude

如果需要,Ollama 会提供安装 Claude Code 的选项。详见 Claude Code。

Codex CLI

首先 安装 Codex CLI,然后启动它:

ollama launch codex

有关设置信息,请参见 Codex CLI。

OpenCode

ollama launch opencode

如果需要,Ollama 会提供安装 OpenCode 的选项。详见 OpenCode。

在提示时选择一个模型,然后尝试:

Explain how this repository is organized.

如需更多工具,请参见 Integrations(集成)。若要通过 API 密钥直接连接代理,请参见 Claude Code。

构建应用程序

使用带有 API 密钥的云端模型,或在没有密钥的情况下本地运行模型。云端请求不需要安装 Ollama。

1. 创建 API 密钥

对于本地模型,跳过此步骤并在 Send a request(发送请求)下选择 Local(本地)。

对于云端模型,请登录或创建一个账户,然后创建一个 API 密钥。

在终端中设置你的密钥:

export OLLAMA_API_KEY="your_api_key"
$env:OLLAMA_API_KEY = "your_api_key"

请将密钥保留在你的应用程序服务器上,不要放在浏览器代码或源代码管理中。

2. 发送请求

向 Ollama 发出第一个请求

Cloud(云端)

这些示例使用 gemma4:31b cloud model(云端模型)。

  • Ollama API
  • OpenAI Chat Completions
  • OpenAI Responses
  • Anthropic Messages
curl https://ollama.com/api/chat \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ],
    "stream": false
  }'

从 message.content 读取回答。参见 Ollama’s API and libraries(Ollama 的 API 和库)。

curl https://ollama.com/v1/chat/completions \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 choices[0].message.content 读取回答。关于客户端设置和支持的功能,请参见 OpenAI compatibility(OpenAI 兼容性)。

curl https://ollama.com/v1/responses \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "input": "Say hello in one sentence."
  }'

读取 output 内部的 output_text 内容块。OpenAI Python 客户端将文本作为 response.output_text 暴露出来。

Responses 请求是无状态的:请在每个请求中包含对话历史。previous_response_id 和 conversation 不受支持。请参见 Responses compatibility(Responses 兼容性)。

curl https://ollama.com/v1/messages \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "gemma4:31b",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 content 读取文本块。直接的云端请求使用 bearer 认证。关于客户端设置和支持的功能,请参见 Anthropic compatibility(Anthropic 兼容性)。

OpenAI 和 Anthropic 兼容性各自覆盖了原始 API 的一部分。有关模型和使用限制,请参阅 Cloud。

本地

curl https://ollama.com/api/chat \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ],
    "stream": false
  }'

从 message.content 中读取答案。请参阅 Ollama’s API and libraries。

curl https://ollama.com/v1/chat/completions \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 choices[0].message.content 中读取答案。有关客户端设置和支持的功能,请参阅 OpenAI compatibility。

curl https://ollama.com/v1/responses \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:31b",
    "input": "Say hello in one sentence."
  }'

读取 output 中的 output_text 内容块。OpenAI Python 客户端将文本作为 response.output_text 暴露出来。

Responses 请求是无状态的:请在每个请求中包含对话历史。previous_response_id 和 conversation 不受支持。请参阅 Responses compatibility。

curl https://ollama.com/v1/messages \
  -H "Authorization: Bearer $OLLAMA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "gemma4:31b",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 content 中读取文本块。直接云端请求使用 bearer 认证。有关客户端设置和支持的功能,请参阅 Anthropic compatibility。

在你的计算机上运行 Gemma 4 E2B。无需 API 密钥。

模型下载大小约为 7.2 GB。我们建议至少有 8 GB 的可用 VRAM,或在 Mac 上使用统一内存。更大的上下文窗口需要更多内存。如果 VRAM 较少,Ollama 可以使用系统 RAM,但响应可能会变慢。

Download Ollama 并打开应用。在 Linux 上,如果服务器尚未运行,请使用 ollama serve 启动它。

下载模型:

ollama pull gemma4:e2b

向你的本地服务器发送请求:

  • Ollama API
  • OpenAI Chat Completions
  • OpenAI Responses
  • Anthropic Messages
curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ],
    "stream": false
  }'
curl http://localhost:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 choices[0].message.content 中读取答案。有关客户端设置和支持的功能,请参阅 OpenAI compatibility。

curl http://localhost:11434/v1/responses \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "input": "Say hello in one sentence."
  }'

读取 output 中的 output_text 内容块。OpenAI Python 客户端将文本作为 response.output_text 暴露出来。

Responses 请求是无状态的:请在每个请求中包含对话历史。previous_response_id 和 conversation 不受支持。请参阅 Responses compatibility。

curl http://localhost:11434/v1/messages \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "gemma4:e2b",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 content 中读取文本块。有关客户端设置和支持的功能,请参阅 Anthropic compatibility。

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ],
    "stream": false
  }'
curl http://localhost:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 choices[0].message.content 读取回答。有关客户端设置和支持的功能,请参阅 OpenAI compatibility。

curl http://localhost:11434/v1/responses \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4:e2b",
    "input": "Say hello in one sentence."
  }'

读取 output 内的 output_text 内容块。OpenAI Python 客户端将文本作为 response.output_text 暴露出来。

Responses 请求是无状态的:请在每个请求中包含对话历史。不支持 previous_response_id 和 conversation。请参阅 Responses compatibility。

curl http://localhost:11434/v1/messages \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "gemma4:e2b",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Say hello in one sentence."
      }
    ]
  }'

从 content 读取文本块。有关客户端设置和支持的功能,请参阅 Anthropic compatibility。

在本地运行模型

下载 Ollama,然后运行:

ollama run gemma4:e2b

Ollama 会下载模型并在您的计算机上启动聊天。输入 /bye 退出。

后续步骤

添加 tool calling、stream responses,或浏览 更多模型。