Amazon Bedrock APIの使い方
参考記事
AWS入門ブログリレー2024〜Amazon Bedrock編〜 | DevelopersIO
Amazon Bedrockとは
既存のAI企業などが開発したChatGPTやClaudeなどさまざまなモデルを共通のインターフェースを経由して使うことができるAWSサービス。
前回までのコンテキストを保存しないテキスト形式でのやり取り、以前までのコンテキストを持ったまま会話するチャット形式などで利用できる。
概要
Amazon Bedrockには大きく4つのAPIがあり、以下どれかのAPIをアプリケーションから叩くことで、AmazonBedRockをAPIから実行できる。
- InvokeModel
- InvokeModelWithResponseStream
- Converse
- ConverseStream
上記は大まかに、InvokeModel APIとConverse APIで分かれている。
現状はConverse APIの方が、同じリクエスト内容で異なる基盤モデルを呼び出せる点で汎用性に優れているとのことで推奨されている。
Converse APIを呼び出す場合のCLI例
を使用して Amazon Bedrock APIリクエストの例を実行する AWS Command Line Interface - Amazon Bedrock
使用可能な基盤モデルを一覧表示
$ aws bedrock list-foundation-models --profile <profile名> | jq ".modelSummaries[] | [.modelArn, .modelId, .modelName]"
...省略
[
"arn:aws:bedrock:ap-northeast-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0:200k",
"anthropic.claude-3-sonnet-20240229-v1:0:200k",
"Claude 3 Sonnet"
]
[
"arn:aws:bedrock:ap-northeast-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
"Claude 3 Sonnet"
]
...省略
テキストプロンプトを送信し、Converse APIでテキストレスポンスを生成する
$ aws bedrock-runtime converse \
--model-id amazon.titan-text-express-v1 \
--messages '[{"role": "user", "content": [{"text": "Describe the purpose of a \"hello world\" program in one line."}]}]' \
--inference-config '{"maxTokens": 512, "temperature": 0.5, "topP": 0.9}' | jq .
{
"output": {
"message": {
"role": "assistant",
"content": [
{
"text": "\n\nA \"hello world\" program is a basic computer program that outputs \"hello world\" to the console or terminal. It is often used as a starting point for learning programming languages and demonstrates the fundamental structure of a program."
}
]
}
},
"stopReason": "end_turn",
"usage": {
"inputTokens": 18,
"outputTokens": 49,
"totalTokens": 67
},
"metrics": {
"latencyMs": 2437
}
}
Converse APIの仕様
Request Syntax
POST /model/modelId/converse HTTP/1.1
Content-type: application/json
Request Body
-
messages
-
content: (以下制限に注意)
- 画像は20枚まで添付できます。 各画像のサイズ、高さ、幅はそれぞれ3.75MB、8000px、8000px以下でなければなりません。
- 最大5つのdocumentを含めることができます。 各ドキュメントのサイズは4.5MB以下でなければなりません。(document=pdf,wordといったテキストファイルのこと)
-
type: document
を持つ ContentBlock を配列に含める場合は、 type: textを持つ ContentBlock も含める必要があります。 - imageやdocumentを含めることができるのは、ロールがuserの場合のみです。
- role(user | assistant): messageの役割
-
content: (以下制限に注意)
-
URLでテキストファイルを指定するbodyの例
{ "messages": [ { "role": "user", "content": [ { "type": "text", "text": "以下のドキュメントを要約してください。" }, { "type": "document", "document": { "url": "https://example.com/sample.pdf", "title": "Sample Document" } } ] } ] }
詳細は以下参照
API料金
使用するモデルによって異なる。ただ全リージョンで料金は同じ(参照)
ここでは、Claudeのモデルの一つである Claude 3 Sonnet だけ整理する。(参照)
Claude 3 Sonnet
[
"arn:aws:bedrock:ap-northeast-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
"Claude 3 Sonnet"
]
Anthropic モデル | 入力トークン 1,000 個あたりの価格 | 1,000 出力トークンあたりの料金 | 1,000 入力トークンあたりの料金 (バッチ) | 1,000 出力トークンあたりの料金 (バッチ) | 1,000 入力トークンあたりの料金 (キャッシュ書き込み) | 1,000 入力トークンあたりの料金 (キャッシュ読み取り) |
---|---|---|---|---|---|---|
Claude 3.5 Sonnet | 0.003 USD | 0.015 USD | 0.0015 USD | 0.0075 USD | 0.00375 USD | 0.0003 USD |
入力トークン、出力トークンとは?
トークン数とはテキストを細かく分割した単位のこと。英語だと短い単語がほぼ1トークンとなるが、日本語中国語は漢字などが多いと1文字1トークンとなったりしてコスパは良くなかったりする。
またモデルによってどこまでを1トークンとするかは変わるため要注意(以下例はあくまで1単語がほとんど1トークンのようになった場合の例である)
具体例
-
入力が「 Explain quantum computing in simple terms. 」の場合。
- トークン化:
["Explain", "quantum", "computing", "in", "simple", "terms", "."]
- トークン数: 7 個
- トークン化:
-
出力が「 Quantum computing uses quantum mechanics to perform calculations much faster than classical computers. 」
- トークン化の例:
["Quantum", "computing", "uses", "quantum", "mechanics", "to", "perform", "calculations", "much", "faster", "than", "classical", "computers", "."]
- トークン数: 14 個
- トークン化の例:
バッチとは?
複数リクエストをまとめて送信するリクエストのこと。
通常のリクエストより割安になる。(Claude 3.5 Sonnetなら半額)
キャッシュ書き込み/読み取りとは?
キャッシュの用途と利点
- 高速化: 一度計算済みの結果を再利用することで応答時間を短縮。
- コスト削減: 同じリクエストに対して再計算を行わないため、トークン利用量が減少。
-
適用例:
- よく似たクエリの多いシステム。
- ユーザーが同じ入力を繰り返し送信するチャットアプリケーション。
Discussion