5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

BedrockのMantleにAnthropic互換APIが生えたみたいなので呼んでみた

5
Posted at

公式発表前っぽいのでご利用は計画的に

Claude Codeの2.1.94のリリースノートに謎の情報が。

image.png

Amazon Bedrock powered by Mantle

BedrockのMantleはこれです。次世代?のすごやつっぽい。

これまでは、OpenAI互換APIとしてChat completionsResponses APIが提供されていました。

これに加え、Anthropic互換APIのMessages APIが増えたようです。

呼び出してみた

IAM認証とBedrock APIキーのどちらでも呼べますが、Bedrock APIキーでやってみます。

Bedrock APIキーを発行して、環境変数AWS_BEARER_TOKEN_BEDROCKに登録しておきます。
そして、以下のコードを実行します。

MantleのMessages APIのエンドポイントはhttps://bedrock-mantle.{リージョン}.api.aws/anthropic/v1/messagesです。

現時点で、先ほどリリースされたOpus 4.7だけが対応しているようです。(Opus 4.6やSonnet 4.6はエラーになりました)

また、usglobalなどの推論プロファイルもエラーになりました。

import os

import httpx

response = httpx.post(
    "https://bedrock-mantle.us-east-1.api.aws/anthropic/v1/messages",
    headers={
        "Authorization": f"Bearer {os.environ['AWS_BEARER_TOKEN_BEDROCK']}",
        "Content-Type": "application/json",
        "anthropic-version": "2023-06-01",
    },
    json={
        "model": "anthropic.claude-opus-4-7",
        "max_tokens": 1024,
        "messages": [{"role": "user", "content": "Hello"}],
    },
    timeout=httpx.Timeout(connect=5.0, read=600, write=600, pool=600),
)

print(response.json())

呼び出し結果

{'model': 'claude-opus-4-7', 'id': 'msg_bdrk_ha2u4ucimy5dki7iouadxoedsjaf6hjtptxyizdp6n3xdwnh5ala', 'type': 'message', 'role': 'assistant', 'content': [{'type': 'text', 'text': 'Hello! How can I help you today?'}], 'stop_reason': 'end_turn', 'stop_sequence': None, 'stop_details': None, 'usage': {'input_tokens': 14, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'cache_creation': {'ephemeral_5m_input_tokens': 0, 'ephemeral_1h_input_tokens': 0}, 'output_tokens': 15, 'service_tier': 'standard'}}

どうして呼び方わかったの?

Claude Codeのリリースノートが出たのと同タイミングで、AnthropicのSDKにも同様の対応が入ってました。

このSDKを使うと、IAM認証、Bedrock APIキー認証、どちらも簡単に呼べました。

import logging
import os

from anthropic.lib.bedrock import AnthropicBedrockMantle

logging.basicConfig(level=logging.DEBUG)

client = AnthropicBedrockMantle(
    aws_region="us-east-1",
    # aws_access_key="",  # IAM認証の場合に使用
    # aws_secret_key="",  # IAM認証の場合に使用
    # aws_session_token="",  # IAM認証の場合に使用
    # aws_profile="",  # IAM認証の場合に使用
    api_key=os.environ["AWS_BEARER_TOKEN_BEDROCK"],
)

response = client.messages.create(
    model="anthropic.claude-opus-4-7",
    messages=[{"role": "user", "content": "Hello"}],
    max_tokens=1024,
)

print(response.content[0].text)

これで、AnthropicのAPIと同じ使用感で使えるようになりましたね!

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?