请求体
|
基础调用
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
message = client.messages.create(
model="qwen3.6-plus",
max_tokens=1024,
system="You are a helpful assistant",
messages=[
{
"role": "user",
"content": "你是谁?"
}
],
thinking={"type": "disabled"},
)
print(message.content[0].text)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
async function main() {
const message = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 1024,
system: "You are a helpful assistant",
messages: [{
role: "user",
content: "你是谁?"
}],
thinking: { type: "disabled" },
});
console.log(message.content[0].text);
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"role": "user",
"content": "你是谁?"
}
],
"thinking": {"type": "disabled"}
}'
流式输出
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.6-plus",
max_tokens=1024,
stream=True,
messages=[
{
"role": "user",
"content": "请简单介绍一下人工智能。"
}
],
thinking={"type": "disabled"},
)
for chunk in stream:
if chunk.type == "content_block_delta":
if hasattr(chunk.delta, 'text'):
print(chunk.delta.text, end="", flush=True)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 1024,
stream: true,
messages: [{
role: "user",
content: "请简单介绍一下人工智能。"
}],
thinking: { type: "disabled" },
});
for await (const chunk of stream) {
if (chunk.type === "content_block_delta" && 'text' in chunk.delta) {
process.stdout.write(chunk.delta.text);
}
}
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
--no-buffer \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": "请简单介绍一下人工智能。"
}
],
"thinking": {"type": "disabled"}
}'
深度思考
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.6-plus",
max_tokens=2048,
stream=True,
thinking={
"type": "enabled",
"budget_tokens": 1024
},
messages=[
{
"role": "user",
"content": "分析一下量子计算的发展前景。"
}
]
)
for chunk in stream:
if chunk.type == "content_block_delta":
if hasattr(chunk.delta, 'thinking'):
print(chunk.delta.thinking, end="", flush=True)
elif hasattr(chunk.delta, 'text'):
print(chunk.delta.text, end="", flush=True)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 2048,
stream: true,
thinking: { type: "enabled", budget_tokens: 1024 },
messages: [{
role: "user",
content: "分析一下量子计算的发展前景。"
}]
});
for await (const chunk of stream) {
if (chunk.type === "content_block_delta") {
if ('thinking' in chunk.delta) {
process.stdout.write(chunk.delta.thinking);
} else if ('text' in chunk.delta) {
process.stdout.write(chunk.delta.text);
}
}
}
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 2048,
"stream": true,
"thinking": {
"type": "enabled",
"budget_tokens": 1024
},
"messages": [
{
"role": "user",
"content": "分析一下量子计算的发展前景。"
}
]
}'
图片理解
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.6-plus",
max_tokens=1024,
stream=True,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250414/mqqmiy/animal_01.jpg",
},
},
{
"type": "text",
"text": "描述这张图片的内容。"
},
],
}
],
thinking={"type": "disabled"},
)
for chunk in stream:
if chunk.type == "content_block_delta":
if hasattr(chunk.delta, 'text'):
print(chunk.delta.text, end="", flush=True)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 1024,
stream: true,
messages: [{
role: "user",
content: [
{
type: "image",
source: {
type: "url",
url: "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250414/mqqmiy/animal_01.jpg",
},
},
{ type: "text", text: "描述这张图片的内容。" },
],
}],
thinking: { type: "disabled" },
});
for await (const chunk of stream) {
if (chunk.type === "content_block_delta" && 'text' in chunk.delta) {
process.stdout.write(chunk.delta.text);
}
}
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250414/mqqmiy/animal_01.jpg"
}
},
{
"type": "text",
"text": "描述这张图片的内容。"
}
]
}
],
"thinking": {"type": "disabled"}
}'
视频理解
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.6-plus",
max_tokens=1024,
stream=True,
messages=[
{
"role": "user",
"content": [
{
"type": "video",
"source": {
"type": "url",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251208/zpupby/3e81ef38-98f0-4d55-bbb6-259334ca18d0.mp4",
},
},
{
"type": "text",
"text": "描述这段视频的内容。"
},
],
}
],
thinking={"type": "disabled"},
)
for chunk in stream:
if chunk.type == "content_block_delta":
if hasattr(chunk.delta, 'text'):
print(chunk.delta.text, end="", flush=True)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 1024,
stream: true,
messages: [{
role: "user",
content: [
{
type: "video",
source: {
type: "url",
url: "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251208/zpupby/3e81ef38-98f0-4d55-bbb6-259334ca18d0.mp4",
},
},
{ type: "text", text: "描述这段视频的内容。" },
],
}],
thinking: { type: "disabled" },
});
for await (const chunk of stream) {
if (chunk.type === "content_block_delta" && 'text' in chunk.delta) {
process.stdout.write(chunk.delta.text);
}
}
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": [
{
"type": "video",
"source": {
"type": "url",
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251208/zpupby/3e81ef38-98f0-4d55-bbb6-259334ca18d0.mp4"
}
},
{
"type": "text",
"text": "描述这段视频的内容。"
}
]
}
],
"thinking": {"type": "disabled"}
}'
Function Call
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
tools = [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
]
message = client.messages.create(
model="qwen3.6-plus",
max_tokens=1024,
tools=tools,
messages=[
{
"role": "user",
"content": "杭州今天天气怎么样?"
}
]
)
print(message.content)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
const message = await anthropic.messages.create({
model: "qwen3.6-plus",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "获取指定城市的天气信息",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "城市名称" }
},
required: ["city"],
},
},
],
messages: [{
role: "user",
content: "杭州今天天气怎么样?"
}],
});
console.log(JSON.stringify(message.content, null, 2));
}
main().catch(console.error);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
],
"messages": [
{
"role": "user",
"content": "杭州今天天气怎么样?"
}
]
}'
显式缓存
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/apps/anthropic",
)
# 模拟代码仓库内容,需达到最小可缓存长度(1024 Token)
long_text_content = "<Your Code Here>" * 400
def get_completion(user_input):
response = client.messages.create(
# 选择支持显式缓存的模型
model="qwen3.6-plus",
max_tokens=1024,
system=[
{
"type": "text",
"text": long_text_content,
# 在 text 块上添加 cache_control 即标记缓存断点;也可放在 messages 数组的 content 块上
"cache_control": {"type": "ephemeral"},
}
],
messages=[
{"role": "user", "content": user_input},
],
)
return response
# 第一次请求:创建缓存
first = get_completion("这段代码的内容是什么")
print(f"创建缓存 Token:{first.usage.cache_creation_input_tokens}")
print(f"命中缓存 Token:{first.usage.cache_read_input_tokens}")
print("=" * 20)
# 第二次请求:长内容相同,仅修改提问 → 命中缓存
second = get_completion("这段代码可以怎么优化")
print(f"创建缓存 Token:{second.usage.cache_creation_input_tokens}")
print(f"命中缓存 Token:{second.usage.cache_read_input_tokens}")
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://dashscope.aliyuncs.com/apps/anthropic",
});
// 模拟代码仓库内容,需达到最小可缓存长度(1024 Token)
const longTextContent = "<Your Code Here>".repeat(400);
async function getCompletion(userInput) {
return client.messages.create({
// 选择支持显式缓存的模型
model: "qwen3.6-plus",
max_tokens: 1024,
system: [
{
type: "text",
text: longTextContent,
// 在 text 块上添加 cache_control 即标记缓存断点;也可放在 messages 数组的 content 块上
cache_control: { type: "ephemeral" },
},
],
messages: [{ role: "user", content: userInput }],
});
}
// 第一次请求:创建缓存
const first = await getCompletion("这段代码的内容是什么");
console.log(`创建缓存 Token:${first.usage.cache_creation_input_tokens}`);
console.log(`命中缓存 Token:${first.usage.cache_read_input_tokens}`);
console.log("=".repeat(20));
// 第二次请求:长内容相同,仅修改提问 → 命中缓存
const second = await getCompletion("这段代码可以怎么优化");
console.log(`创建缓存 Token:${second.usage.cache_creation_input_tokens}`);
console.log(`命中缓存 Token:${second.usage.cache_read_input_tokens}`);
curlcurl -X POST "https://dashscope.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.6-plus",
"max_tokens": 1024,
"system": [
{
"type": "text",
"text": "<请在此处放置长度 ≥ 1024 Token 的可缓存内容>",
"cache_control": {"type": "ephemeral"}
}
],
"messages": [
{"role": "user", "content": "这段代码的内容是什么"}
]
}'
|