リクエストボディ model string (必須)
モデル名。サポートされているモデル:
サポートされているモデル
Qwen-Max : qwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.6-max-preview, qwen3-max, qwen3-max-2026-01-23, qwen3-max-preview
Qwen-Plus : qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-04-20, qwen3.5-plus-2026-02-15, qwen-plus, qwen-plus-latest, qwen-plus-2025-09-11
Qwen-Flash : qwen3.8-flash, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.6-flash-2026-04-16, qwen3.5-flash, qwen3.5-flash-2026-02-23, qwen-flash, qwen-flash-2025-07-28
Qwen-Turbo : qwen-turbo
Qwen-Coder : qwen3-coder-next, qwen3-coder-plus, qwen3-coder-plus-2025-09-23, qwen3-coder-flash
Qwen-VL : qwen3-vl-plus, qwen3-vl-flash, qwen-vl-max, qwen-vl-plus
Qwen オープンソースモデル : qwen3.6-27b, qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b
サードパーティモデル deepseek-v4-pro, deepseek-v4-flash, deepseek-v4-flash-0731, deepseek-v3.2, kimi-k3, kimi-k2.7-code, kimi-k2.6, kimi-k2.5, kimi-k2-thinking, glm-5.2, glm-5.1, glm-5, glm-4.7, glm-4.6, MiniMax-M2.5, MiniMax-M2.1
max_tokens integer (必須)
deepseek-v4-pro, deepseek-v4-flash, deepseek-v4-flash-0731, qwen3.8-max, qwen3.8-flash: max_tokens は、応答コンテンツと Chain-of-Thought コンテンツの両方の最大合計トークン数です。モデルの出力がこの値を超えると、生成は早期に停止し、stop_reason は max_tokens になります。
max_tokens は、応答コンテンツと思考プロセスの合計の長さを制限します。拡張思考が有効な場合、max_tokens > thinking.budget_tokens
glm-5.2: thinking.budget_tokens パラメーターが渡されない場合、max_tokens は応答コンテンツと Chain-of-Thought コンテンツの両方の最大合計トークン数です。モデルの出力がこの値を超えると、生成は早期に停止し、stop_reason は max_tokens になります。thinking.budget_tokens パラメーターが渡される場合、max_tokens は応答コンテンツの最大トークン数のみであり、思考トークンは thinking.budget_tokens によって個別にコントロールされます。
その他のモデル:応答コンテンツの最大トークン数。生成されたコンテンツがこの値を超えると、生成は早期に停止し、stop_reason は max_tokens になります。
max_tokens は思考プロセスの長さを制限しません。拡張思考が有効な場合、思考トークンは thinking.budget_tokens によって個別にコントロールされます。
system string or array (任意)
モデルの動作を定義するシステムプロンプト。system はトップレベルパラメーターであり、messages 配列は system ロールを受け入れません。
文字列は、単一の type="text" ブロックに相当します。プロンプトキャッシュのブレークポイントをマークするには、配列を渡します。
プロパティ
type string (必須)
固定値:text。
text string (必須)
システムプロンプトのテキスト。
cache_control object (任意)
プロンプトキャッシュのブレークポイント。キャッシュヒット時、後続のリクエストはキャッシュ読み取りレートで請求されます。type のみを含み、ephemeral に固定されます。
messages array (必須)
user と assistant のターンが交互に並んだメッセージ配列。
messages 配列要素
role string (必須)
メッセージのロール。有効な値:user、assistant。
content string or array (必須)
プレーンテキスト文字列または構造化されたコンテンツ配列。文字列は、type="text" の単一の content ブロックに相当します。
content 配列要素の型
Text プロパティ
type string (必須)
固定値:text。
text string (必須)
テキストコンテンツ。
cache_control object (任意)
プロンプトキャッシュのブレークポイント。type のみを含み、ephemeral に固定されます。
Image (Vision モデルが必要)
プロパティ
type string (必須)
固定値:image。
source object (必須)
イメージデータのソース。
プロパティ
type string (必須)
有効な値:url (パブリックイメージ URL)、base64 (Base64 エンコード)。
url string
イメージのパブリック URL。type が url の場合に必須です。
media_type string
イメージの MIME タイプ (例:image/jpeg)。type が base64 の場合に必須です。
data string
Base64 エンコードされたイメージデータ。type が base64 の場合に必須です。
Video (Vision モデルが必要)
プロパティ
type string (必須)
固定値:video。
source object (必須)
ビデオデータのソース。
プロパティ
type string (必須)
有効な値:url (パブリックビデオ URL)、base64 (Base64 エンコード)。
url string
ビデオのパブリック URL。type が url の場合に必須です。
media_type string
ビデオの MIME タイプ (例:video/mp4)。type が base64 の場合に必須です。
data string
Base64 エンコードされたビデオデータ。type が base64 の場合に必須です。
Tool use (assistant ロール、モデルによって返されるツール呼び出し命令)
プロパティ
type string (必須)
固定値:tool_use。
id string (必須)
ツール呼び出しの一意の識別子。後続の tool_result で結果を関連付けるために使用されます。
name string (必須)
呼び出されたツールの名前。
input object (必須)
ツール呼び出しの入力パラメーター。構造は、tools 内の対応するツールの input_schema によって決定されます。
cache_control object (任意)
プロンプトキャッシュのブレークポイント。type のみを含み、ephemeral に固定されます。ツール呼び出しのコンテンツはキャッシュプレフィックスに含まれます。
Tool result (user ロール、モデルに返送されるツールの実行結果)
プロパティ
type string (必須)
固定値:tool_result。
tool_use_id string (必須)
tool_use ブロックの id に対応します。
content string (必須)
ツールによって返されたコンテンツ。
cache_control object (任意)
プロンプトキャッシュのブレークポイント。type のみを含み、ephemeral に固定されています。
stream boolean (任意)
ストリーミングを有効にするかどうか。デフォルト値:false。
temperature number (任意)
生成されるテキストの多様性をコントロールします。有効値:[0, 2)。値が大きいほど、よりランダムな結果が生成されます。
注記 この有効値は、公式の Anthropic の有効値 [0.0, 1.0] とは異なります。Anthropic から移行する際は、このパラメーターの値を確認してください。
top_p number (任意)
Nucleus サンプリングの確率しきい値。
temperature と top_p はどちらも生成されるテキストの多様性をコントロールできます。どちらか一方のみを設定することを推奨します。詳細については、「概要 」をご参照ください。
top_k integer (任意)
サンプリング中の候補セットのサイズ。
stop_sequences array (任意)
生成を停止させるトリガーとなるテキストシーケンス。出力は一致したシーケンスの前に終了します。
注記 一致後、応答の stop_reason は end_turn のままであり、応答には一致したシーケンスは含まれません。
thinking object (任意)
拡張思考の構成。有効にすると、モデルは応答する前に推論を行い、応答には thinking タイプのコンテンツブロックが含まれます。すべてのモデルが思考モードをサポートしているわけではありません。
プロパティ
type string (必須)
有効な値:enabled (思考モードを有効にする)、disabled (思考モードを無効にする)。
budget_tokens integer (任意)
思考プロセスの最大トークン数。max_tokens とは別個です。このパラメーターは思考部分を制限し、max_tokens は最終的な応答を制限します。予算が大きいほど、複雑な質問に対してより徹底的な分析が可能になります。type が enabled の場合に有効です。
tools array (任意)
関数呼び出しのためのツール定義。
tool_choice object (任意)
ツール選択戦略:
{"type": "auto"}:モデルがツールを呼び出すかどうかを決定します (デフォルト)。
{"type": "any"}:モデルにいずれかのツールを強制的に呼び出させます。
{"type": "none"}:モデルがツールを呼び出すことを禁止します。
{"type": "tool", "name": "tool_name"}:モデルに指定されたツールを強制的に呼び出させます。
output_config object (任意)
プロパティ
effort string (任意)
モデルの推論の強度をコントロールします。有効な値とデフォルト値はモデルによって異なります。デフォルト値:max。
有効な値:
low と medium は high に、xhigh は max にマッピングされます。
サポートされているモデル:qwen3.8-max, qwen3.8-flash, glm-5.2, deepseek-v4-pro, and deepseek-v4-flash。
format object (任意)
構造化出力の構成。有効にすると、モデルは JSON 文字列を返します。動作はモデルによって異なります。
厳密な構造化出力 :deepseek および glm シリーズのモデルで利用可能です。モデルは提供された JSON スキーマに厳密に従い、同じフィールドタイプと階層を保証します。
通常の構造化出力 :他のすべてのモデルでは、スキーマフィールドの制約は強制されません。API は自動的にプレーンな JSON モード (出力が有効な JSON 文字列であることを保証するのみ) にフォールバックします。このフォールバックモードでは、リクエストは次の両方を満たす必要があります:(1) output_config パラメーターが明示的に提供されていること、(2) system または messages のコンテンツにキーワード「JSON」(大文字と小文字を区別しない) が含まれていること。キーワード「JSON」がない場合、API は 'messages' must contain the word 'json' in some form というエラーをスローします。
プロパティ
type string (必須)
固定値:json_schema。
schema object (必須)
標準の JSON スキーマ仕様に従う JSON スキーマオブジェクト。type (データの型)、properties (フィールド定義)、required (必須フィールド名の配列)、および additionalProperties ( false に設定する必要があります) を含める必要があります。
基本的な呼び出し
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.8-max",
max_tokens=1024,
system="You are a helpful assistant",
messages=[
{
"role": "user",
"content": "あなたは誰ですか?"
}
],
thinking={"type": "disabled"},
)
print(message.content[0].text)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"role": "user",
"content": "あなたは誰ですか?"
}
],
"thinking": {"type": "disabled"}
}'
ストリーミング
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",
)
stream = client.messages.create(
model="qwen3.8-max",
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)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": "人工知能について簡単に紹介してください。"
}
],
"thinking": {"type": "disabled"}
}'
拡張思考
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",
)
stream = client.messages.create(
model="qwen3.8-max",
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)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"max_tokens": 2048,
"stream": true,
"thinking": {
"type": "enabled",
"budget_tokens": 1024
},
"messages": [
{
"role": "user",
"content": "量子コンピューティングの将来性について分析してください。"
}
]
}'
画像認識
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",
)
stream = client.messages.create(
model="qwen3.8-max",
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)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"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"}
}'
動画認識
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",
)
stream = client.messages.create(
model="qwen3.8-max",
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)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"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"}
}'
関数呼び出し
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",
)
tools = [
{
"name": "get_weather",
"description": "指定された都市の天気情報を取得します",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "都市名"
}
},
"required": ["city"]
}
}
]
message = client.messages.create(
model="qwen3.8-max",
max_tokens=1024,
tools=tools,
messages=[
{
"role": "user",
"content": "今日の杭州の天気はどうですか?"
}
]
)
print(message.content)
TypeScript import 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.8-max",
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);
curl curl -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.8-max",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "指定された都市の天気情報を取得します",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "都市名"
}
},
"required": ["city"]
}
}
],
"messages": [
{
"role": "user",
"content": "今日の杭州の天気はどうですか?"
}
]
}'
プロンプトキャッシュ
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",
)
# コードリポジトリのコンテンツをシミュレートします。キャッシュ可能な最小長 (1024 トークン) に達する必要があります
long_text_content = "<Your Code Here>" * 400
def get_completion(user_input):
response = client.messages.create(
# プロンプトキャッシュをサポートするモデルを選択します
model="qwen3.8-max",
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("このコードは何をしますか?")
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("このコードはどのように最適化できますか?")
print(f"Cache creation tokens: {second.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {second.usage.cache_read_input_tokens}")
TypeScript import 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.8-max",
max_tokens: 1024,
system: [
{
type: "text",
text: longTextContent,
// テキストブロックに cache_control を追加して、キャッシュのブレークポイントをマークします。messages 配列のコンテンツブロックにも配置できます
cache_control: { type: "ephemeral" },
},
],
messages: [{ role: "user", content: userInput }],
});
}
// 最初のリクエスト:キャッシュを作成
const first = await getCompletion("このコードは何をしますか?");
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("このコードはどのように最適化できますか?");
console.log(`Cache creation tokens: ${second.usage.cache_creation_input_tokens}`);
console.log(`Cache read tokens: ${second.usage.cache_read_input_tokens}`);
curl curl -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.8-max",
"max_tokens": 1024,
"system": [
{
"type": "text",
"text": "<ここに少なくとも 1024 トークンのキャッシュ可能なコンテンツを配置します>",
"cache_control": {"type": "ephemeral"}
}
],
"messages": [
{"role": "user", "content": "このコードは何をしますか?"}
]
}'
構造化出力
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="deepseek-v4-pro",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "次のメールから重要な情報を抽出してください:田中一郎 (tanaka@example.com) は Enterprise プランに興味があり、来週火曜日の午後2時にデモを希望しています。"
}
],
extra_body={
"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
}
}
}
},
)
# deepseek-v4-pro は thinking ブロックを返すため、text コンテンツブロックを見つけます
text_block = next(block for block in message.content if block.type == "text")
print(text_block.text)
TypeScript import 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() {
// output_config は Model Studio プラットフォームの拡張パラメーターであり、ボディ経由で渡します
const message = await (anthropic.messages.create as Function)({
model: "deepseek-v4-pro",
max_tokens: 1024,
messages: [{
role: "user",
content: "次のメールから重要な情報を抽出してください:田中一郎 (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
}
}
}
});
// deepseek-v4-pro は thinking ブロックを返すため、text コンテンツブロックを見つけます
const textBlock = message.content.find(
(block: { type: string }) => block.type === "text"
);
console.log(textBlock?.text);
}
main().catch(console.error);
curl curl -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": "次のメールから重要な情報を抽出してください:田中一郎 (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
}
}
}
}'