[アップデート] Amazon Nova Lite, Pro で S3 バケットに保管されたオブジェクトを直接参照できるようになりました
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
What's New には載っていないのですが、 Amazon Nova Lite, Pro で S3 バケットに保管されたオブジェクトを直接参照できるようになりました。
Changes You can now reference images and documents stored in Amazon S3 when using InvokeModel and Converse APIs with Amazon Nova Lite and Nova Pro. This enables direct integration of S3-stored multimedia assets in your model requests without manual downloading or base64 encoding.
アップデート内容
今まで、各 LLM に対して推論 API を実行する場合、ドキュメントを保管元から API が実行できる環境に持ってきて、推論する必要がありました。
たとえば、S3 バケットに保管されたオブジェクトに基づいて、 Converse API を実施したいケースの場合、いったん GetObject を実行し API 実行元にオブジェクトをダウンロード、ダウンロードしたオブジェクトを使用して Converse API を実行するといったステップが必要でした。また、画像に関しては Base64 によるエンコードが必要で、ファイルタイプに応じて切り替えするのが少し手間ではありました。
今回、Amazon Nova Lite, Pro に関しては、S3 バケット内のオブジェクトを直接参照できるオプション(s3Location
)がサポートされました。
Supported models and model features をみると、Amazon S3 uploads
の列が今後このようなことをできるオプションのようです。
The location of a document object in an Amazon S3 bucket. To see which models support S3 uploads, see Supported models and features for Converse.
やってみる
実際に Converse API から、 S3 バケットのオブジェクトを利用してみます。S3 バケットとオブジェクトを用意します。
テキストの取得
テキストファイルを元にやりとりをしてみます。
タイトル:星降る夜の約束
昔々、静かな山あいの村に、リオという少年が住んでいました。リオは星を見るのが大好きで、毎晩、丘の上に登って夜空を眺めていました。
ある晩、リオがいつものように星を見ていると、ひときわ大きく輝く流れ星が空を横切りました。リオは「どうか、僕にも友達ができますように」と願いごとをしました。
すると、流れ星がリオの目の前に落ちてきて、まばゆい光の中から小さな女の子が現れました。彼女は「ステラ」と名乗り、星の国からやってきたと言います。
リオとステラはすぐに仲良くなり、毎晩一緒に星座を探したり、星の物語を語り合ったりしました。しかし、ステラは「夜明けとともに星の国に帰らなければならない」と言います。
最後の夜、リオとステラは「また会えるように」と約束し、ステラはリオに小さな星のペンダントを託して、空へと帰っていきました。
それからというもの、リオはひとりでも寂しくありませんでした。星空を見上げるたびに、いつかまたステラと会えることを信じて、毎日を大切に過ごしました。
そして、リオが大人になったある夜、ペンダントが淡く光り、空からステラの声が聞こえてきました。「リオ、約束はまだ覚えている?」
リオは微笑みながら、静かに星空に手を振りました。
おしまい。
Python のコードは以下のとおりです。「タイトルは何ですか?
」といったシンプルな質問をしてみます。
# Conversation APIを使ってAmazon Novaに日本語のテキストメッセージを送信します。
import boto3
from botocore.exceptions import ClientError
# 利用するAWSリージョンでBedrock Runtimeクライアントを作成
client = boto3.client("bedrock-runtime", region_name="us-east-1")
# モデルIDを指定(例:Amazon Nova Lite)
# model_id = "amazon.nova-lite-v1:0"
model_id = "amazon.nova-pro-v1:0"
# ユーザーからの日本語メッセージを設定
user_message = "タイトルは何ですか?"
document = {
'format': 'txt',
'name': 'story',
'source': {
's3Location': {
'uri': 's3://amazon-nova-s3-uploads-123456789012/story.txt'
}
}
}
conversation = [
{
"role": "user",
"content": [
{"text": user_message},
{"document": document}
],
}
]
try:
# モデルにメッセージを送信し、推論設定も指定
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 1, "topP": 0.9},
)
# 応答テキストを抽出して表示
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
except (ClientError, Exception) as e:
print(f"エラー: '{model_id}'の呼び出しに失敗しました。理由: {e}")
exit(1)
Invoke Amazon Nova on Amazon Bedrock using Bedrock's Converse API - Amazon Bedrock
どうやら、エンコーディングされた文字列で LLM に渡されているようです。
takakuni@ % python app.py
The text you've provided appears to be a mix of encoded or garbled characters, possibly due to an encoding issue or an attempt to obscure the original content. It includes a mix of what seems like Japanese text (based on the characters like "44K", "44OI", etc.), some structured data or code, and what looks like Base64 encoded segments.
To identify a title or meaningful content from this, we would need to decode or interpret the segments properly. Here’s a breakdown of potential steps:
1. **Identify Encoding**:
- The segments like “44K”, “44OI” might be encoding artifacts.
- The longer strings such as “44K/44Kk44OI44Or77ya5pif6ZmN44KL5aSc44Gu57SE5p2fCgrmmJTjgIXjgIHpnZnjgYvjgarlsbHjgYLjgYTjga7mnZHjgavjgIHjg6rjgqrjgajjgYTjgYblsJHlubTjgYzkvY” look like they could be Base64 encoded.
2. **Decode Base64 Segments**:
- Use a Base64 decoder to convert these segments into readable text.
3. **Interpret Japanese Characters**:
- The Japanese characters might need to be properly encoded (UTF-8) to be readable.
4. **Structured Data**:
- The segments that look like code or structured data (e.g., “jOOBvuWknOOAgeODquOCquOBqOOCueODhuODqeOBr+OAjOOBvuOBn+S8muOBiOOCi+OCiOOBhuOBq+OAjeOBqOe0hOadn+OBl+OAgeOCueODhuODqeOBr+ODquOCquOBq+Wwj+OBleOBquaYn+OBruODmuODs+ODgOODs+ODiOOCkuiol+OBl+OBpuOAgeepuuOBuOOBqOW4sOOBo+OBpuOBhOOBjeOBvuOBl+OBn+OAggoK”) might need further analysis or decoding.
### Example Decoding Step
Let’s try decoding a small Base64 segment:
44K/44K
では、文章を英語にしてみましょう。
Title: Promise on a Starry Night
Once upon a time, in a quiet mountain village, there lived a boy named Rio. Rio loved watching the stars and would climb up the hill every night to gaze at the night sky.
One night, as Rio was stargazing as usual, an especially bright shooting star streaked across the sky. Rio made a wish: "Please, let me have a friend."
Then, the shooting star fell right in front of Rio, and from the dazzling light appeared a little girl. She introduced herself as Stella and said she had come from the Land of Stars.
Rio and Stella quickly became friends. Every night, they searched for constellations together and shared stories about the stars. However, Stella said, "When dawn comes, I must return to the Land of Stars."
On their last night together, Rio and Stella promised that they would meet again someday. Stella entrusted Rio with a small star-shaped pendant and returned to the sky.
From then on, Rio was never lonely, even when he was alone. Every time he looked up at the starry sky, he believed that he would see Stella again someday and cherished each day.
Then, one night after Rio had grown up, the pendant glowed softly, and he heard Stella’s voice from the sky. "Rio, do you still remember our promise?"
Smiling, Rio quietly waved his hand toward the starry sky.
The End.
コード側も変更します。
# Conversation APIを使ってAmazon Novaに日本語のテキストメッセージを送信します。
import boto3
from botocore.exceptions import ClientError
# 利用するAWSリージョンでBedrock Runtimeクライアントを作成
client = boto3.client("bedrock-runtime", region_name="us-east-1")
# モデルIDを指定(例:Amazon Nova Lite)
# model_id = "amazon.nova-lite-v1:0"
model_id = "amazon.nova-pro-v1:0"
# ユーザーからの日本語メッセージを設定
user_message = "タイトルは何ですか?"
document = {
'format': 'txt',
'name': 'story',
'source': {
's3Location': {
- 'uri': 's3://amazon-nova-s3-uploads-123456789012/story.txt'
+ 'uri': 's3://amazon-nova-s3-uploads-123456789012/story_en.txt'
}
}
}
conversation = [
{
"role": "user",
"content": [
{"text": user_message},
{"document": document}
],
}
]
try:
# モデルにメッセージを送信し、推論設定も指定
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 1, "topP": 0.9},
)
# 応答テキストを抽出して表示
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
except (ClientError, Exception) as e:
print(f"エラー: '{model_id}'の呼び出しに失敗しました。理由: {e}")
exit(1)
うまく返答が返ってきていますね。(ありがたいのか、日本語訳して返ってきています)
takakuni@ % python app.py
タイトルは「星空の約束」です。
以下は、与えられたテキストの復号化と要約です:
---
**タイトル:星空の約束**
とある山村で、リオという少年がいました。リオは星空が大好きで、毎晩星空を眺めながら空を飛ぶことを夢見ていました。
ある夜、いつものように星を眺めていると、特に明るい星が一筋の光を放ち、リオの目の前に小さな女の子が現れました。彼女はステラと名乗り、星の国から来たと言いました。
リオとステラはすぐに友達になりました。毎晩、二人は星について話し、星空の下で冒険を共有しました。しかし、ステラは「夜明けが来ると、私は星の国に戻らなければならない」と言っていました。
最後の夜、二人は再び星空の下で会い、必ずまたいつか会おうと約束しました。ステラはリオに小さな星形のペンダントを渡して去っていきました。
それ以降、リオは一人ぼっちになることはありませんでした。星空を見るたびに、ステラとの約束を思い出し、彼女に会える日を心待ちにしていました。
そして、リオが大人になったある夜、星空からステラの声が聞こえてきました。「リオ、私たちの約束まだ覚えている?」
微笑みながら、リオは星空に向かって手を振りました。
**終わり。**
画像の取得
続いて画像の取得を行います。以下の画像を使ってみました。
コードは以下のとおりです。 image
の場合、 name
キーは不要になります。
# Conversation APIを使ってAmazon Novaに日本語のテキストメッセージを送信します。
import boto3
from botocore.exceptions import ClientError
# 利用するAWSリージョンでBedrock Runtimeクライアントを作成
client = boto3.client("bedrock-runtime", region_name="us-east-1")
# モデルIDを指定(例:Amazon Nova Lite)
# model_id = "amazon.nova-lite-v1:0"
model_id = "amazon.nova-pro-v1:0"
# ユーザーからの日本語メッセージを設定
user_message = "画像を説明してください"
image = {
'format': 'png',
'source': {
's3Location': {
'uri': 's3://amazon-nova-s3-uploads-123456789012/bird_fukurou_run.png'
}
}
}
conversation = [
{
"role": "user",
"content": [
{"text": user_message},
{"image": image}
],
}
]
try:
# モデルにメッセージを送信し、推論設定も指定
response = client.converse(
modelId=model_id,
messages=conversation,
inferenceConfig={"maxTokens": 512, "temperature": 1, "topP": 0.9},
)
# 応答テキストを抽出して表示
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
except (ClientError, Exception) as e:
print(f"エラー: '{model_id}'の呼び出しに失敗しました。理由: {e}")
exit(1)
うまく動いている様子です。Base64 のエンコーディングも済んでいるため便利ですね。
takakuni@ % python app.py
この画像は、ふわふわとした柔らかい感触を与える白いキャラクターのイラストです。キャラクターは丸みを帯びた形で、耳と目、口が特徴的なデザインです。特に目は大きくて丸い形で、その周囲には目立つ黄色い縁取りがあり、キャラクターの可愛らしさを強調しています。また、目には小さな黒い点が描かれており、表情に生気を与えています。口も小さくて丸く、少し上に曲がった形で、ほのかな笑顔を醸し出しています。背景は黒く、キャラクターが際立つようにデザインされています。全体的に、シンプルで可愛らしいデザインとなっています。
まとめ
以上、「Amazon Nova Lite, Pro で S3 バケットに保管されたオブジェクトを直接参照できるようになりました」でした。
とくに画像の方は言語の壁がないため、非常に便利そうな印象を受けました。今回は document, image のみでしたが、video も対応しているようです。
クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!