Ollama 提供了与 Anthropic Messages API 的兼容性,帮助现有应用程序连接到 Ollama,包括 Claude Code 等工具。
用法
环境变量
要使用期望 Anthropic API(如 Claude Code)的工具,请设置以下环境变量:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # 需要但会被忽略
export ANTHROPIC_BASE_URL=http://localhost:11434简单的 /v1/messages 示例
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # 需要但会被忽略
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Hello, how are you?'}
]
)
print(message.content[0].text)javascript
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama", // 需要但会被忽略
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, how are you?" }],
});
console.log(message.content[0].text);shell
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: ollama" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"messages": [{ "role": "user", "content": "Hello, how are you?" }]
}'流式传输示例
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
with client.messages.stream(
model='qwen3-coder',
max_tokens=1024,
messages=[{'role': 'user', 'content': 'Count from 1 to 10'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)javascript
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const stream = await anthropic.messages.stream({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Count from 1 to 10" }],
});
for await (const event of stream) {
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
process.stdout.write(event.delta.text);
}
}shell
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"stream": true,
"messages": [{ "role": "user", "content": "Count from 1 to 10" }]
}'工具调用示例
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
tools=[
{
'name': 'get_weather',
'description': 'Get the current weather in a location',
'input_schema': {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. San Francisco, CA'
}
},
'required': ['location']
}
}
],
messages=[{'role': 'user', 'content': "What's the weather in San Francisco?"}]
)
for block in message.content:
if block.type == 'tool_use':
print(f'Tool: {block.name}')
print(f'Input: {block.input}')javascript
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "Get the current weather in a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
},
required: ["location"],
},
},
],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
});
for (const block of message.content) {
if (block.type === "tool_use") {
console.log("Tool:", block.name);
console.log("Input:", block.input);
}
}shell
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
}
},
"required": ["location"]
}
}
],
"messages": [{ "role": "user", "content": "What is the weather in San Francisco?" }]
}'与 Claude Code 配合使用
Claude Code 可以配置为使用 Ollama 作为其后端。
推荐模型
对于编码用例,推荐使用 glm-4.7、minimax-m2.1 和 qwen3-coder 等模型。
在使用前下载模型:
shell
ollama pull qwen3-coder注意:Qwen 3 coder 是一个需要至少 24GB VRAM 才能顺畅运行的 30B 参数模型。更长的上下文长度需要更多的资源。
shell
ollama pull glm-4.7:cloud快速设置
shell
ollama launch claude这将提示您选择一个模型,自动配置 Claude Code 并启动它。如不启动而进行配置:
shell
ollama launch claude --config手动设置
设置环境变量并运行 Claude Code:
shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coder或者在您的 shell 配置文件中设置环境变量:
shell
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434然后使用任何 Ollama 模型运行 Claude Code:
shell
claude --model qwen3-coder端点
/v1/messages
支持的功能
- [x] 消息 (Messages)
- [x] 流式传输 (Streaming)
- [x] 系统提示 (System prompts)
- [x] 多轮对话 (Multi-turn conversations)
- [x] 视觉 (Vision - images)
- [x] 工具 (Tools - function calling)
- [x] 工具结果 (Tool results)
- [x] 思维/扩展思维 (Thinking/extended thinking)
支持的请求字段
- [x]
model - [x]
max_tokens - [x]
messages- [x] 文本
content - [x] 图像
content(base64) - [x] 内容块数组
- [x]
tool_use块 - [x]
tool_result块 - [x]
thinking块
- [x] 文本
- [x]
system(字符串或数组) - [x]
stream - [x]
temperature - [x]
top_p - [x]
top_k - [x]
stop_sequences - [x]
tools - [x]
thinking - [ ]
tool_choice - [ ]
metadata
支持的响应字段
- [x]
id - [x]
type - [x]
role - [x]
model - [x]
content(文本、tool_use、thinking 块) - [x]
stop_reason(end_turn, max_tokens, tool_use) - [x]
usage(input_tokens, output_tokens)
流式传输事件
- [x]
message_start - [x]
content_block_start - [x]
content_block_delta(text_delta, input_json_delta, thinking_delta) - [x]
content_block_stop - [x]
message_delta - [x]
message_stop - [x]
ping - [x]
error
模型
Ollama 支持本地模型和云模型。
本地模型
在使用前拉取本地模型:
shell
ollama pull qwen3-coder推荐的本地模型:
qwen3-coder- 擅长编码任务gpt-oss:20b- 强大的通用模型
云模型
云模型无需下载即可立即使用:
glm-4.7:cloud- 高性能云模型minimax-m2.1:cloud- 快速云模型
默认模型名称
对于依赖 Anthropic 默认模型名称(如 claude-3-5-sonnet)的工具,请使用 ollama cp 复制现有模型名称:
shell
ollama cp qwen3-coder claude-3-5-sonnet之后,此新模型名称可以在 model 字段中指定:
shell
curl http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'与 Anthropic API 的差异
行为差异
- 接受 API 密钥但未进行验证
- 接受
anthropic-version头部但未使用 - Token 计数是基于底层模型分词器的近似值
不支持的功能
以下 Anthropic API 功能目前不支持:
| 功能 | 描述 |
|---|---|
/v1/messages/count_tokens | Token 计数端点 |
tool_choice | 强制特定工具使用或禁用工具 |
metadata | 请求元数据 (user_id) |
| Prompt caching | 用于缓存前缀的 cache_control 块 |
| Batches API | /v1/messages/batches 用于异步批量处理 |
| Citations | citations 内容块 |
| PDF 支持 | 带有 PDF 文件的 document 内容块 |
| 服务器发送错误 | 流式传输过程中的 error 事件 (错误返回 HTTP 状态) |
部分支持
| 功能 | 状态 |
|---|---|
| 图像内容 | 支持 Base64 图像;不支持 URL 图像 |
| 扩展思维 | 基本支持;接受 budget_tokens 但未强制执行 |