LoginSignup
12
1
生成AIに関する記事を書こう!
Qiita Engineer Festa20242024年7月17日まで開催中!

Bedrockが前処理なしにドキュメントを読めるようになったのでできることを整理しました

Last updated at Posted at 2024-06-28

今日のトップニュースはこちらです。

BedrockのConverse APIがドキュメントのインプットに対応しています!!

これまでは、Claude 3、Claude 3.5がマルチモーダル機能として、画像(JPEG、PNG、GIF、WebP)のインプットに対応していました。

BedrockのConverse APIがドキュメントに対応したことで、WordやExcel、PDFを事前の前処理なしにそのままBedrockに渡せるようになりました。

サポート状況

2024/6/28時点のサポート状況です。

「Document chat」欄がYESのものがドキュメントに対応しており、「Vision」がYESのものが画像に対応しています。

Claude 3 はドキュメントと画像に両対応していますが、Claude 3.5は画像のみの対応です。また、TitanやCommand R、Llama 2/3など、比較的多くのモデルがドキュメントは対応しています。(モデルそのものの機能ではなく、Bedrock内部で処理をした上でモデルにテキストを渡しているのかなと推測しています。)

image.png

ドキュメント:Supported models and model features

Converse APIの呼び出し

Claude 3 Haikuを使い、それぞれのバリエーションで呼び出してみました。

テキストのみ

一番シンプルな、テキストだけを渡す例です。

import boto3

client = boto3.client("bedrock-runtime")

response = client.converse(
    modelId="anthropic.claude-3-haiku-20240307-v1:0",
    messages=[{"role": "user", "content": [{"text": "こんにちは"}]}],
    inferenceConfig={"temperature": 0},
)

print(response["output"]["message"]["content"][0]["text"])

こんにちは!どうぞよろしくお願いいたします。AIのアシスタントなので、ご質問やお困りのことがあればお聞きしますので、遠慮なくお話しください。どのような内容でも、できる限りサポートさせていただきます。

テキスト+画像

テキストと画像を渡す例です。

以下の画像を渡しました。Webページを印刷機能でPDF化し、更に画像にしました。

(フォトアプリで開いた様子)
image.png

まず、ファイルをバイナリ形式で読み込みます。
ユーザーメッセージの「context」は配列で複数指定できるので、「text」と「image」を指定します。

Claudeのマルチモーダルのプロンプトエンジニアリングテクニックのひとつ、「画像に名前をつける」をやってみました。

参考:https://docs.anthropic.com/en/docs/build-with-claude/vision
Example: Multiple images

import boto3

client = boto3.client("bedrock-runtime")

with open("./document.png", "rb") as f:
    image = f.read()

response = client.converse(
    modelId="anthropic.claude-3-haiku-20240307-v1:0",
    messages=[
        {
            "role": "user",
            "content": [
                {"text": "Image1: "},
                {"image": {"format": "png", "source": {"bytes": image}}},
                {"text": "Image1の内容を要約して"},
                
            ],
        }
    ],
    inferenceConfig={"temperature": 0},
)

print(response["output"]["message"]["content"][0]["text"])

画像の内容を要約すると以下のようになります:

Anthropic社のClaude 3.5 Sonnetモデルが、Amazon Bedrock上で利用可能になったことが発表されています。このモデルは、Anthropic社の最も高度なAIモデルで、業界標準となる新しいインテリジェンスを提供します。

Claude 3.5 Sonnetは、他の生成型AIモデルよりも優れた性能を発揮し、コストも従来のClaude 3 Opusの1/5と低価格になっています。このモデルは、文脈に敏感な顧客サポート、複雑なワークフローの調整、高品質なコンテンツ作成など、幅広い用途に適しています。また、画像や図表の解釈、不完全なデータからの洞察抽出、コードの自動生成など、多様な機能を備えています。

全体として、Anthropic社のClaude 3.5 Sonnetモデルは、コスト対効果に優れ、高度な自然言語処理能力を発揮する新しいAIソリューションとして注目されています。

画像から上手にテキストを抽出できました。

テキスト+ドキュメント(PDF)

では、ドキュメントを渡しましょう。

ドキュメントは先ほどの画像を生成する前のPDFファイルです。

(Adobe Acrobatで開いた様子)
image.png

ドキュメントを渡す場合は、「content」の中に「document」として指定します。「image」とは違い、「name」の指定が必要です。

import boto3

client = boto3.client("bedrock-runtime")

with open("./document.pdf", "rb") as f:
    pdf = f.read()

response = client.converse(
    modelId="anthropic.claude-3-haiku-20240307-v1:0",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "document": {
                        "name": "PDF Document",
                        "format": "pdf",
                        "source": {"bytes": pdf},
                    }
                },
                {"text": "PDF Documentの内容を要約して"},
            ],
        }
    ],
)

print(response["output"]["message"]["content"][0]["text"])

以下のように要約できます。

• Anthropic社のClaude 3.5 Sonnetモデルが、Amazon Bedrock上で一般利用可能になった。

• Claude 3.5 Sonnetは、Anthropic社の最も知能的なモデルで、他の生成AI モデルを性能面で上回る。

• Claude 3.5 Sonnetは、コストはClaudeb3 Opusの1/5だが、性能はOpusを上回る。

• Claude 3.5 Sonnetは、顧客サポート、ワークフロー管理、コード翻訳、アプリケーション開発などの複雑なタスクに最適。

• Claude 3.5 Sonnetは、自然な言語理解と表現力に優れ、チャート・グラフの解釈、画像からのテキスト抽出などのビジョン機能も強化されている。

• 適切なツールと指示があれば、Claude 3.5 Sonnetは自立的にコーディングと問題解決も行える。

出力形式は異なりますが、ほぼ同じ内容を出力しています。

テキスト+画像+ドキュメント

実際の利用用途としてあるかはわかりませんが、画像とドキュメントは一度に渡すことも可能です。

まず、タスク1で画像から文字抽出を行います。次に、タスク2でPDFから文字抽出を行います。最後に、タスク3で両者を比較させてみました。(元データは同じなので、内容は完全一致するはずです)

※Claude 3 Haikuだといい結果が出なかったので、Claude 3 Sonnetを使用した場合の結果です。

response = client.converse(
    modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    messages=[
        {
            "role": "user",
            "content": [
                {"text": "Image1: "},
                {"image": {"format": "png", "source": {"bytes": png}}},
                {
                    "document": {
                        "name": "PDF Document",
                        "format": "pdf",
                        "source": {"bytes": pdf},
                    }
                },
                {"text": "以下のタスクを実施してください。"},
                {
                    "text": "タスク1:Image1から文字列をできるだけ忠実に抽出してください。"
                },
                {
                    "text": "タスク2:PDF Documentから文字列をできるだけ忠実に抽出してください。"
                },
                {
                    "text": "タスク3:タスク1とタスク2で抽出した文字列を比較して相違点を示してください。"
                },
            ],
        }
    ],
)

print(response["output"]["message"]["content"][0]["text"])

タスク1: Image1から抽出した文字列:

Anthropic's Claude 3.5 Sonnet model now available in Amazon Bedrock

Get Started for Free Contact Us

Posted on: Jun 20, 2024

Anthropic's Claude 3.5 Sonnet foundation model is now generally available in Amazon Bedrock. Anthropic's most intelligent model to date, Claude 3.5 Sonnet, sets a new industry standard for intelligence. The model outperforms other generative AI models in the industry as well as Anthropic's previously most intelligent model, Claude 3 Opus, on a wide range of evaluations, all while being one-fifth of the cost of Opus. You can now get intelligence better than Claude 3 Opus, at the same cost of Anthropic's original Claude 3 Sonnet model.

The frontier intelligence displayed by Claude 3.5 Sonnet combined with cost- effective pricing, makes this model ideal for complex tasks such as context- sensitive customer support, orchestrating multi-step workflows, streamlining code translations, and creating user-facing applications. Claude 3.5 Sonnet exhibits marked improvements in near-human levels of comprehension and fluency. The model represents a significant leap in understanding nuance, humor, and complex instructions. It is exceptional at writing high-quality content that feels more authentic with a natural and relatable tone. Claude 3.5 Sonnet is also Anthropic's strongest vision model, providing best-in-class vision capabilities. It can accurately interpret charts and graphs and transcribe text from imperfect images—a core capability for retail, logistics, and financial services, where AI may glean more insights from an image, graphic, or illustration than from text alone. Additionally, when instructed and provided with the relevant tools, Claude 3.5 Sonnet can independently write and edit code with sophisticated reasoning and troubleshooting capabilities.

タスク2: PDF Documentから抽出した文字列:

Anthropic's Claude 3.5 Sonnet model now available in Amazon Bedrock

Posted on: Jun 20, 2024

Anthropic's Claude 3.5 Sonnet foundation model is now generally available in Amazon Bedrock. Anthropic's most intelligent model to date, Claude 3.5 Sonnet, sets a new industry standard for intelligence. The model outperforms other generative AI models in the industry as well as Anthropic's previously most intelligent model, Claude 3 Opus, on a wide range of evaluations, all while being one-fifth of the cost of Opus. You can now get intelligence better than Claude 3 Opus, at the same cost of Anthropic's original Claude 3 Sonnet model.

The frontier intelligence displayed by Claude 3.5 Sonnet combined with cost- effective pricing, makes this model ideal for complex tasks such as context- sensitive customer support, orchestrating multi-step workflows, streamlining code translations, and creating user-facing applications. Claude 3.5 Sonnet exhibits marked improvements in near-human levels of comprehension and fluency. The model represents a significant leap in understanding nuance, humor, and complex instructions. It is exceptional at writing high-quality content that feels more authentic with a natural and relatable tone. Claude 3.5 Sonnet is also Anthropic's strongest vision model, providing best-in-class vision capabilities. It can accurately interpret charts and graphs and transcribe text from imperfect images—a core capability for retail, logistics, and financial services, where AI may glean more insights from an image, graphic, or illustration than from text alone. Additionally, when instructed and provided with the relevant tools, Claude 3.5 Sonnet can independently write and edit code with sophisticated reasoning and troubleshooting capabilities.

タスク3: 抽出した文字列を比較して相違点を示す:

Image1とPDF Documentから抽出した文字列は完全に一致しています。相違点は見つかりませんでした。

見事、完全一致しました🎉🎉🎉

まとめ

精度の良し悪しは確認する必要がありますが、前処理せずにPDFやWord、Excelを渡せるのはなかなかアツいアップデートだと思いました!

12
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
12
1