0
0

Amazon Titan Text ExpressをCloudShellで実行する

Posted at

概要

Amazon Titan Text ExpressをCloudShellで実行する手順を残します
CloudShellとAmazonが提供しているテキスト生成モデル Amazon Titan Text の紹介のみとなります
コードはAmazon公式サイトの物をほぼ流用させて頂きました

Amazon Titan Text G1 - Expressとは

  • Amazon Titanテキストモデルの一つです
  • テキスト生成用の大規模言語モデルであり、検索拡張生成 (RAG) をサポートするだけでなく、自由形式のテキスト生成や会話型チャットなどに対応可能です
  • 現時点(2024.8.31)では英語のみ対応の模様です
  • サポートするユースケース
    • 検索拡張生成
    • 自由形式のテキスト生成
    • ブレインストーミング
    • 要約
    • コード生成
    • 表作成
    • データフォーマット
    • 書き換え
    • 抽出
    • Q&A
    • チャット

CloudShell

  • ブラウザベースで利用可能
  • AWSマネジメントコンソールから直接起動可能
  • 事前にシェル構築済み
    image.png

CloudShellでAmazon Titan Text ExpressのAPI実行

CloudShellにてpythonコマンド実行

$ python
Python 3.9.16 (main, Jul  5 2024, 00:00:00) 
[GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.

API実行

Pythonで Amazon Titan Text ExpressのAPI 実行します。
prompt = "Write the code for the Hello World program in Vue.js"
で、プロンプトを定義しています。
英語で「Vue.jsでHello Worldプログラムのコードを書いて」と定義

import boto3
import json

from botocore.exceptions import ClientError

# Create an Amazon Bedrock Runtime client.
brt = boto3.client("bedrock-runtime")

# Set the model ID, e.g., Amazon Titan Text G1 - Express.
model_id = "amazon.titan-text-express-v1"

# Define the prompt for the model.
prompt = "Write the code for the Hello World program in Vue.js"

# Format the request payload using the model's native structure.
native_request = {
    "inputText": prompt,
    "textGenerationConfig": {
        "maxTokenCount": 512,
        "temperature": 0.5,
        "topP": 0.9
    },
}

# Convert the native request to JSON.
request = json.dumps(native_request)

response = brt.invoke_model(modelId=model_id, body=request)

# Decode the response body.
model_response = json.loads(response["body"].read())

# Extract and print the response text.
response_text = model_response["results"][0]["outputText"]
print(response_text)

実行結果

image.png

  • コードは以下です
  • ちょっと物足りないですが、ちゃんと書いてくれました。
// Define a Vue component named 'HelloWorld'
Vue.component('hello-world', {
  // Define the template for the component
  template: '<div>Hello, Vue!</div>'
});
// Create a new instance of the 'HelloWorld' component
new Vue({
  el: '#app'
});

#参考サイト
Amazon Bedrock での Amazon Titan
Amazon Titan Text G1 - Express
Amazon Bedrock ユーザーガイド
AWS CloudShell とは

0
0
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
0
0