Ollama มีความเข้ากันได้กับ Anthropic Messages API เพื่อช่วยเชื่อมต่อแอปพลิเคชันที่มีอยู่กับ Ollama รวมถึงเครื่องมืออย่าง Claude Code
การใช้งาน
ตัวแปรสภาพแวดล้อม
หากต้องการใช้ Ollama กับเครื่องมือที่ต้องการใช้ 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" }]
}'ตัวอย่างการเรียกใช้เครื่องมือ (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?" }]
}'การใช้งานกับ Claude Code
Claude Code สามารถกำหนดค่าให้ใช้ Ollama เป็นแบ็กเอนด์ได้
โมเดลที่แนะนำ
สำหรับการใช้งานด้านการเขียนโค้ด โมเดลอย่าง glm-4.7 minimax-m2.1 และ qwen3-coder เป็นที่แนะนำ
ดาวน์โหลดโมเดลก่อนใช้งาน:
shell
ollama pull qwen3-coderข้อควรทราบ: Qwen 3 coder เป็นโมเดลที่มีพารามิเตอร์ 30B ต้องการ VRAM อย่างน้อย 24GB เพื่อให้ทำงานได้ลื่นไหล หากต้องการใช้ความยาวบริบทที่ยาวกว่านี้ จะต้องการ VRAM มากกว่า
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
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434จากนั้นรัน Claude Code ด้วยโมเดล Ollama ใดก็ได้:
shell
claude --model qwen3-coderจุดสิ้นสุด (Endpoints)
/v1/messages
ฟีเจอร์ที่รองรับ
- [x] ข้อความ
- [x] สตรีม
- [x] พรอมต์ระบบ
- [x] การสนทนาหลายรอบ
- [x] วิสชัน (รูปภาพ)
- [x] เครื่องมือ (การเรียกใช้ฟังก์ชัน)
- [x] ผลลัพธ์ของเครื่องมือ
- [x] การคิด/การคิดแบบขยาย
ฟิลด์คำขอที่รองรับ
- [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ถูกรับไว้แต่ไม่ถูกใช้งาน - จำนวนโทเคนเป็นการประมาณการตามโทเคนไอเซอร์ของโมเดลพื้นฐาน
ฟีเจอร์ที่ไม่รองรับ
ฟีเจอร์ของ Anthropic API ที่ไม่รองรับในปัจจุบันมีดังนี้:
| ฟีเจอร์ | คำอธิบาย |
|---|---|
/v1/messages/count_tokens | จุดสิ้นสุดสำหรับนับจำนวนโทเคน |
tool_choice | การบังคับให้ใช้เครื่องมือที่กำหนดหรือปิดการใช้งานเครื่องมือ |
metadata | เมตาดาต้าในคำขอ (user_id) |
| การแคชพรอมต์ | บล็อก cache_control สำหรับแคชคำนำหน้า |
| API แบตช์ | /v1/messages/batches สำหรับการประมวลผลแบตช์แบบอะซิงโครนัส |
| การอ้างอิง | บล็อกเนื้อหา citations |
| การรองรับ PDF | บล็อกเนื้อหา document ที่ใช้กับไฟล์ PDF |
| ข้อผิดพลาดที่ส่งจากเซิร์ฟเวอร์ | กิจกรรม error ระหว่างการสตรีม (ข้อผิดพลาดจะส่งคืนสถานะ HTTP) |
การรองรับบางส่วน
| ฟีเจอร์ | สถานะ |
|---|---|
| เนื้อหารูปภาพ | รองรับรูปภาพ Base64; ไม่รองรับรูปภาพจาก URL |
| การคิดแบบขยาย | รองรับพื้นฐาน; ค่า budget_tokens ถูกรับไว้แต่ไม่บังคับใช้ |