Ollama cung cấp tính tương thích với API Messages của Anthropic để giúp kết nối các ứng dụng hiện có với Ollama, bao gồm cả các công cụ như Claude Code.
Cách sử dụng
Biến môi trường
Để sử dụng Ollama với các công cụ yêu cầu API Anthropic (như Claude Code), hãy thiết lập các biến môi trường sau:
shell
export ANTHROPIC_AUTH_TOKEN=ollama # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434Ví dụ đơn giản về /v1/messages
python
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # required but ignored
)
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", // required but ignored
});
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?" }]
}'Ví dụ streaming
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" }]
}'Ví dụ gọi công cụ (tool calling)
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?" }]
}'Sử dụng với Claude Code
Claude Code có thể được cấu hình để sử dụng Ollama làm backend.
Mô hình được đề xuất
Đối với các trường hợp sử dụng lập trình, các mô hình như glm-4.7, minimax-m2.1 và qwen3-coder được đề xuất.
Tải mô hình xuống trước khi sử dụng:
shell
ollama pull qwen3-coderLưu ý: Qwen 3 coder là mô hình 30B tham số, yêu cầu ít nhất 24GB VRAM để chạy mượt mà. Cần nhiều dung lượng hơn nếu độ dài ngữ cảnh dài hơn.
shell
ollama pull glm-4.7:cloudThiết lập nhanh
shell
ollama launch claudeLệnh này sẽ nhắc bạn chọn mô hình, tự động cấu hình Claude Code và khởi chạy nó. Để cấu hình mà không khởi chạy:
shell
ollama launch claude --configThiết lập thủ công
Thiết lập các biến môi trường và chạy Claude Code:
shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coderHoặc thiết lập các biến môi trường trong hồ sơ shell của bạn:
shell
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434Sau đó chạy Claude Code với bất kỳ mô hình Ollama nào:
shell
claude --model qwen3-coderĐiểm cuối (Endpoints)
/v1/messages
Tính năng được hỗ trợ
- [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
Trường yêu cầu được hỗ trợ
- [x]
model - [x]
max_tokens - [x]
messages- [x] Text
content - [x] Image
content(base64) - [x] Array of content blocks
- [x]
tool_useblocks - [x]
tool_resultblocks - [x]
thinkingblocks
- [x] Text
- [x]
system(string or array) - [x]
stream - [x]
temperature - [x]
top_p - [x]
top_k - [x]
stop_sequences - [x]
tools - [x]
thinking - [ ]
tool_choice - [ ]
metadata
Trường phản hồi được hỗ trợ
- [x]
id - [x]
type - [x]
role - [x]
model - [x]
content(text, tool_use, thinking blocks) - [x]
stop_reason(end_turn, max_tokens, tool_use) - [x]
usage(input_tokens, output_tokens)
Sự kiện streaming
- [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
Mô hình
Ollama hỗ trợ cả mô hình cục bộ và mô hình đám mây.
Mô hình cục bộ
Tải mô hình cục bộ xuống trước khi sử dụng:
shell
ollama pull qwen3-coderMô hình cục bộ được đề xuất:
qwen3-coder- Rất phù hợp cho các tác vụ lập trìnhgpt-oss:20b- Mô hình đa năng mạnh mẽ
Mô hình đám mây
Các mô hình đám mây có thể sử dụng ngay mà không cần tải xuống:
glm-4.7:cloud- Mô hình đám mây hiệu năng caominimax-m2.1:cloud- Mô hình đám mây tốc độ cao
Tên mô hình mặc định
Đối với các công cụ phụ thuộc vào tên mô hình mặc định của Anthropic như claude-3-5-sonnet, hãy sử dụng lệnh ollama cp để sao chép tên mô hình hiện có:
shell
ollama cp qwen3-coder claude-3-5-sonnetSau đó, tên mô hình mới này có thể được chỉ định trong trường 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!"
}
]
}'Khác biệt so với API Anthropic
Khác biệt về hành vi
- Khóa API được chấp nhận nhưng không được xác thực
- Tiêu đề
anthropic-versionđược chấp nhận nhưng không được sử dụng - Số lượng token là các ước tính dựa trên bộ phân tích token (tokenizer) của mô hình cơ bản
Tính năng không được hỗ trợ
Các tính năng của API Anthropic sau đây hiện không được hỗ trợ:
| Tính năng | Mô tả |
|---|---|
/v1/messages/count_tokens | Điểm cuối đếm token |
tool_choice | Buộc sử dụng công cụ cụ thể hoặc vô hiệu hóa công cụ |
metadata | Siêu dữ liệu yêu cầu (user_id) |
| Prompt caching | Các khối cache_control dùng để lưu bộ nhớ đệm các tiền tố |
| Batches API | /v1/messages/batches dùng để xử lý hàng loạt không đồng bộ |
| Citations | Các khối nội dung citations |
| PDF support | Các khối nội dung document hỗ trợ tệp PDF |
| Server-sent errors | Các sự kiện error được gửi từ máy chủ trong quá trình streaming (lỗi được trả về dưới dạng trạng thái HTTP) |
Hỗ trợ một phần
| Tính năng | Trạng thái |
|---|---|
| Image content | Hỗ trợ hình ảnh base64; không hỗ trợ hình ảnh URL |
| Extended thinking | Hỗ trợ cơ bản; budget_tokens được chấp nhận nhưng không được thực thi |