Amazon Titan基盤モデルのTitan Text G1 Lite/Express を試してみた
2023-11-29
azblob://2023/11/29/eyecatch/2023-11-29-re-invent-2023-titan-text-g1-lite-and-express-000.png

こんにちは、株式会社FIXERの村上です。

ラスベガスで開催されるAWSの最大イベント「AWS re:Invent」の季節ということでブログ書きます!
 

11月29日 1:00~3:00(日本時間)、AWS CEOのAdam Selipsky氏からKeynoteの講演があり、その中でAmazon製の基盤モデルである「Titan」のテキスト生成がパブリックプレビューされることが決まりました🎉

実は、Keynote が始まる数時間前にバージニア北部(us-east-1)とオレゴン(us-west-2)でプレビューを確認していました笑

2023年11月28日に、パプリックプレビューになったのは以下の2モデルです。

  • Titan Text G1 - Lite
  • Titan Text G1 - Express

さっそく 試してみたいので いつもの「Manage model access」から利用できるように申請しました。

すぐに利用可能になりました!! 私はバージニア北部(us-east-1)で利用しています。

▼ 事前準備(省略OK)

前回の「Amazon BedrockでAmazon Titan Embeddingsを試してみる」と同様、ローカルPC(私はMac)からBedrock APIを実行してみます。

1. Bedrock API を操作するための準備

Amazon Bedrock のAPI操作をローカルPCから行うため、以下のIAMポリシー(BedrockFullAccessPolicy)を作成します。

IAMポリシーの中身はこちらです。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "BedrockFullAccess",
            "Effect": "Allow",
            "Action": [
                "bedrock:*"
            ],
            "Resource": "*"
        }
    ]
}

IAMのアクセスキーとシークレットを発行し、作成したIAMポリシーを割り当てます。

アクセスキーとシークレットを aws configure コマンドを使用して、認証情報を指定します。
私はdefaultプロファイルを使用します。

 

2. Bedrock API を実行するための準備

以下のコマンドを実行して、ローカルPCの環境設定を行います。

# 本ブログでは、下記のバージョンを使用します。 

> python3 --version 

Python 3.12.0

# AWS SDK for Python (Boto3) ライブラリの最新バージョンをインストールします。 

> pip3 install -q -U boto3 

Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 23.2.1 Uninstalling pip-23.2.1: Successfully uninstalled pip-23.2.1 Successfully installed pip-23.3.1

# 本ブログでは、下記のバージョンを使用します。 

> pip3 show boto3 | grep "Version:"

Version: 1.33.2

# LangChain をインストールします。 

> pip3 install --quiet langchain

# 本ブログでは、下記のバージョンを使用します。 

> pip3 show langchain | grep "Version:" 

Version: 0.0.342

今回は、AWSが提供する Amazon Bedrock Workshop を使用します。

> git clone https://github.com/aws-samples/amazon-bedrock-workshop.git

> cd amazon-bedrock-workshop

VSCodeなどのエディタで開きます。

▼ Titan Text G1 - Lite を試してみた

amazon-bedrock-workshop/ 配下に at_text_lite_bedrock_api.py と名付けたPythonファイルを新規用意します。

Pythonのソースコードはこちらです。

Pythonimport json
import os
import sys

module_path = "./utils"
sys.path.append(os.path.abspath(module_path))
from utils import bedrock, print_ww

bedrock_runtime = bedrock.get_bedrock_client(
    assumed_role=os.environ.get("BEDROCK_ASSUME_ROLE", None),
    region=os.environ.get("AWS_DEFAULT_REGION", None)
)

prompt_data = """
Command: We are the world's most cloud-native IT vendor.
"""

response = bedrock_runtime.invoke_model(
    body=json.dumps({
        "inputText": prompt_data
    }).encode(),
    modelId="amazon.titan-text-lite-v1",
    accept="*/*",
    contentType="application/json"
) 

response_body = json.loads(response.get('body').read())
outputText = response_body.get('results')[0].get('outputText')

print_ww(outputText)

出力結果 → 翻訳します!

We provide innovative solutions to help businesses and Government agencies modernize their IT infrastructure and drive digital transformation.

Our portfolio includes everything from cloud computing and data analytics to cybersecurity and artificial intelligence.

With a global presence and a strong focus on customer success, we're here to help you navigate the complexities of the IT landscape and achieve your goals.

At our core, we believe in the power of technology to drive positive change. We're committed to delivering solutions that empower our customers to innovate, grow, and succeed in an ever-changing world.

We're the world's most cloud-native IT vendor.

Google翻訳してみました。

企業や政府機関のITインフラを近代化し、デジタルトランスフォーメーションを推進するための革新的なソリューションを提供しています。

当社のポートフォリオには、クラウド・コンピューティングやデータ分析からサイバーセキュリティや人工知能まで、あらゆるものが含まれています。

グローバルに展開し、お客様の成功に重点を置く当社は、複雑化するIT環境をナビゲートし、お客様の目標達成を支援します。

私たちは、ポジティブな変化をもたらすテクノロジーの力を信じています。私たちは、お客様が革新し、成長し、変化し続ける世界で成功できるようなソリューションを提供することをお約束します。

当社は、世界で最もクラウドネイティブなITベンダーです。

ぱっと見、GPT-3 に近い文章ですね。

▼ Titan Text G1 - Express を試してみた

amazon-bedrock-workshop/ 配下に at_text_express_bedrock_api.py と名付けたPythonファイルを新規用意します。

Pythonのソースコードはこちらです。

Pythonimport json
import os
import sys

module_path = "./utils"
sys.path.append(os.path.abspath(module_path))
from utils import bedrock, print_ww

bedrock_runtime = bedrock.get_bedrock_client(
    assumed_role=os.environ.get("BEDROCK_ASSUME_ROLE", None),
    region=os.environ.get("AWS_DEFAULT_REGION", None)
)

prompt_data = """
Command: We are the world's most cloud-native IT vendor.
"""

response = bedrock_runtime.invoke_model(
    body=json.dumps({
        "inputText": prompt_data
    }).encode(),
    modelId="amazon.titan-text-express-v1",
    accept="*/*",
    contentType="application/json"
) 

response_body = json.loads(response.get('body').read())
outputText = response_body.get('results')[0].get('outputText')

print_ww(outputText)

出力結果 → 翻訳します!

PythonOur mission is to help our customers modernize their IT infrastructure and applications to achieve greater agility, scalability, and cost savings. 

As a Senior DevOps Engineer, you will be responsible for designing, implementing, and managing infrastructure and applications in the cloud. 

You will work closely with development teams to automate deployment, monitoring, and management processes, and ensure that applications are delivered with high availability and reliability. 

Responsibilities: 
1. Design and implement infrastructure and applications in the cloud, including AWS, Azure, and GCP. 
2. Automate deployment, monitoring, and management processes using tools such as Terraform, Ansible・・・

Google翻訳してみました。

Python私たちのミッションは、お客様のITインフラとアプリケーションの近代化を支援し、より高い俊敏性、拡張性、コスト削減を実現することです。 

シニアDevOpsエンジニアとして、クラウドにおけるインフラストラクチャとアプリケーションの設計、実装、管理を担当します。

開発チームと密接に連携し、デプロイ、監視、管理プロセスを自動化し、高い可用性と信頼性でアプリケーションを提供できるようにします。 

職務内容 
1. AWS、Azure、GCPを含むクラウドのインフラとアプリケーションの設計と実装。 
2. Terraform、Ansibleなどのツールを使用したデプロイ、監視、管理プロセスの自動化

▼【おまけ】 Titan Text G1 - Express に日本語で聞いてみた

執筆時点では、Amazon Titan Text は 英語のみサポートしています。
ただ、Titan Text G1 - Express をよく見てみると、Preview のワードを発見したので試しに日本語で聞いてみました。

プロンプトの内容はこちらを使ってみました。

Python#命令 あなたはIT企業で勤務するシステムエンジニアです。
以下の[#条件]に基づき、アジャイル開発の魅力を提案してください。

#条件
・ウォータフォール開発と比較すること
・アジャイルの最も効果の出しやすい事例をあげること
・否定的な文章は作成しないこと

出力結果(英語のみだよ、と表示されました)

PythonSorry, this model is only accessible for English only applications. 
Please consider revising your content to be in English.

やはり、まだ 英語以外の言語はリクエストを受け付けないようです(残念)。

▼ 料金

Amazon Titanは私がAmazon Bedrockサービスの中で一番好きなFM(基盤モデル)なのですが、その理由は価格です。
ラベルなしの学習済みデータを条件に他のFMと比較しても一番安いです。
回答精度を高めるためのRAGや基盤モデルなどをカスタムするAgent for Amazon Bedrockなどを併用稼働して、設計することが多いため、テキスト生成の料金だけでも安いことは正義になります。

https://aws.amazon.com/jp/bedrock/pricing/

▼ 最後に

最後まで読んでいただき、ありがとうございます!
まだ、英語のみをサポートするAmazon Titan Text モデルではありますが、近いうちに日本語をサポートすると思います。
今後のアップデートに期待して、事前に設計することもアリですね👍

▼ 余談

Adam氏のKeynote期間中にオレゴン(us-west-2)にて、Titan Text G1 - AgileLlama 2 Chat 70Bの情報がサイレント公開されていました!
現在は、AWS管理コンソール上から消えています!!!