リクエストボディ
|
基本的な呼び出し
Python
import anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
message = client.messages.create(
model="qwen3.7-plus",
max_tokens=1024,
system="You are a helpful assistant",
messages=[
{
"role": "user",
"content": "Who are you?"
}
],
thinking={"type": "disabled"},
)
print(message.content[0].text)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
async function main() {
const message = await anthropic.messages.create({
model: "qwen3.7-plus",
max_tokens: 1024,
system: "You are a helpful assistant",
messages: [{
role: "user",
content: "Who are you?"
}],
thinking: { type: "disabled" },
});
console.log(message.content[0].text);
}
main().catch(console.error);
curlcurl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-plus",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"role": "user",
"content": "Who are you?"
}
],
"thinking": {"type": "disabled"}
}'
ストリーミング
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.7-plus",
max_tokens=1024,
stream=True,
messages=[
{
"role": "user",
"content": "Give a brief introduction to artificial intelligence."
}
],
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,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.7-plus",
max_tokens: 1024,
stream: true,
messages: [{
role: "user",
content: "Give a brief introduction to artificial intelligence."
}],
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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
--no-buffer \
-d '{
"model": "qwen3.7-plus",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": "Give a brief introduction to artificial intelligence."
}
],
"thinking": {"type": "disabled"}
}'
拡張思考
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.7-plus",
max_tokens=2048,
stream=True,
thinking={
"type": "enabled",
"budget_tokens": 1024
},
messages=[
{
"role": "user",
"content": "Analyze the future prospects of quantum computing."
}
]
)
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,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.7-plus",
max_tokens: 2048,
stream: true,
thinking: { type: "enabled", budget_tokens: 1024 },
messages: [{
role: "user",
content: "Analyze the future prospects of quantum computing."
}]
});
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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-plus",
"max_tokens": 2048,
"stream": true,
"thinking": {
"type": "enabled",
"budget_tokens": 1024
},
"messages": [
{
"role": "user",
"content": "Analyze the future prospects of quantum computing."
}
]
}'
画像認識
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.7-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/20250414/mqqmiy/animal_01.jpg",
},
},
{
"type": "text",
"text": "Describe the content of this image."
},
],
}
],
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,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.7-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/20250414/mqqmiy/animal_01.jpg",
},
},
{ type: "text", text: "Describe the content of this image." },
],
}],
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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-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"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
stream = client.messages.create(
model="qwen3.7-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/20251208/zpupby/3e81ef38-98f0-4d55-bbb6-259334ca18d0.mp4",
},
},
{
"type": "text",
"text": "Describe the content of this video."
},
],
}
],
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,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
const stream = await anthropic.messages.create({
model: "qwen3.7-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/20251208/zpupby/3e81ef38-98f0-4d55-bbb6-259334ca18d0.mp4",
},
},
{ type: "text", text: "Describe the content of this video." },
],
}],
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://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-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"}
}'
関数呼び出し
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
tools = [
{
"name": "get_weather",
"description": "Get weather information for a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name"
}
},
"required": ["city"]
}
}
]
message = client.messages.create(
model="qwen3.7-plus",
max_tokens=1024,
tools=tools,
messages=[
{
"role": "user",
"content": "What's the weather like in Hangzhou today?"
}
]
)
print(message.content)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
async function main() {
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
const message = await anthropic.messages.create({
model: "qwen3.7-plus",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "Get weather information for a specified city",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "City name" }
},
required: ["city"],
},
},
],
messages: [{
role: "user",
content: "What's the weather like in Hangzhou today?"
}],
});
console.log(JSON.stringify(message.content, null, 2));
}
main().catch(console.error);
curlcurl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-plus",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get weather information for a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name"
}
},
"required": ["city"]
}
}
],
"messages": [
{
"role": "user",
"content": "What's the weather like in Hangzhou today?"
}
]
}'
プロンプトキャッシュ
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
# コードリポジトリのコンテンツをシミュレートします。キャッシュ可能な最小長 (1024 トークン) に達する必要があります
long_text_content = "<Your Code Here>" * 400
def get_completion(user_input):
response = client.messages.create(
# プロンプトキャッシュをサポートするモデルを選択します
model="qwen3.7-plus",
max_tokens=1024,
system=[
{
"type": "text",
"text": long_text_content,
# テキストブロックに cache_control を追加して、キャッシュのブレークポイントをマークします。messages 配列のコンテンツブロックにも配置できます
"cache_control": {"type": "ephemeral"},
}
],
messages=[
{"role": "user", "content": user_input},
],
)
return response
# 最初のリクエスト:キャッシュを作成
first = get_completion("What does this code do?")
print(f"Cache creation tokens: {first.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {first.usage.cache_read_input_tokens}")
print("=" * 20)
# 2 番目のリクエスト:同じ長いコンテンツ、異なる質問 -> キャッシュヒット
second = get_completion("How can this code be optimized?")
print(f"Cache creation tokens: {second.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {second.usage.cache_read_input_tokens}")
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
// コードリポジトリのコンテンツをシミュレートします。キャッシュ可能な最小長 (1024 トークン) に達する必要があります
const longTextContent = "<Your Code Here>".repeat(400);
async function getCompletion(userInput) {
return client.messages.create({
// プロンプトキャッシュをサポートするモデルを選択します
model: "qwen3.7-plus",
max_tokens: 1024,
system: [
{
type: "text",
text: longTextContent,
// テキストブロックに cache_control を追加して、キャッシュのブレークポイントをマークします。messages 配列のコンテンツブロックにも配置できます
cache_control: { type: "ephemeral" },
},
],
messages: [{ role: "user", content: userInput }],
});
}
// 最初のリクエスト:キャッシュを作成
const first = await getCompletion("What does this code do?");
console.log(`Cache creation tokens: ${first.usage.cache_creation_input_tokens}`);
console.log(`Cache read tokens: ${first.usage.cache_read_input_tokens}`);
console.log("=".repeat(20));
// 2 番目のリクエスト:同じ長いコンテンツ、異なる質問 -> キャッシュヒット
const second = await getCompletion("How can this code be optimized?");
console.log(`Cache creation tokens: ${second.usage.cache_creation_input_tokens}`);
console.log(`Cache read tokens: ${second.usage.cache_read_input_tokens}`);
curlcurl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "qwen3.7-plus",
"max_tokens": 1024,
"system": [
{
"type": "text",
"text": "<ここに 1024 トークン以上のキャッシュ可能なコンテンツを配置してください>",
"cache_control": {"type": "ephemeral"}
}
],
"messages": [
{"role": "user", "content": "What does this code do?"}
]
}'
構造化出力
Pythonimport anthropic
import os
client = anthropic.Anthropic(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
message = client.messages.create(
model="deepseek-v4-pro",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "このメールから主要な情報を抽出してください:田中一郎 (ichiro.tanaka@example.com) は Enterprise プランに興味があり、来週火曜日の午後 2 時にデモを希望しています。"
}
],
output_config={
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"plan_interest": {"type": "string"},
"demo_requested": {"type": "boolean"}
},
"required": ["name", "email", "plan_interest", "demo_requested"],
"additionalProperties": False
}
}
},
)
print(message.content[0].text)
TypeScriptimport Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: process.env.DASHSCOPE_API_KEY,
// {WorkspaceId} を実際のワークスペース ID に置き換えてください。URL はリージョンによって異なります。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
});
async function main() {
const message = await anthropic.messages.create({
model: "deepseek-v4-pro",
max_tokens: 1024,
messages: [{
role: "user",
content: "このメールから主要な情報を抽出してください:田中一郎 (ichiro.tanaka@example.com) は Enterprise プランに興味があり、来週火曜日の午後 2 時にデモを希望しています。"
}],
output_config: {
format: {
type: "json_schema",
schema: {
type: "object",
properties: {
name: { type: "string" },
email: { type: "string" },
plan_interest: { type: "string" },
demo_requested: { type: "boolean" }
},
required: ["name", "email", "plan_interest", "demo_requested"],
additionalProperties: false
}
}
}
});
console.log(message.content[0].text);
}
main().catch(console.error);
curlcurl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "x-api-key: $DASHSCOPE_API_KEY" \
-d '{
"model": "deepseek-v4-pro",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "このメールから主要な情報を抽出してください: John Smith (john@example.com) はエンタープライズプランに興味があり、来週の火曜日の午後2時にデモのスケジュール設定を希望しています。"
}
],
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"},
"plan_interest": {"type": "string"},
"demo_requested": {"type": "boolean"}
},
"required": ["name", "email", "plan_interest", "demo_requested"],
"additionalProperties": false
}
}
}
}'
|