Closed10

Strands Agents を触ってみる

u1u1

ユーザガイドも見つつ記事のサンプルコードを書いてみましたが上手く動きませんでした

from strands import Agent

agent = Agent()
agent("Strandsってどういう意味?")
u1u1

エラーの内容としては、指定したモデルIDにアクセスできないといったものでした

An error occurred (AccessDeniedException) when calling the ConverseStream operation: You don't have access to the model with the specified model ID.
u1u1

そこで自分の場合は us-east-1 でBedrockのモデルアクセス許可をとっているので以下のような感じに書き換えてみましたがエラーが解消しませんでした

# Bedrock
bedrock_model = BedrockModel(
    model_id="anthropic.claude-3-5-sonnet-20240620-v1:0",
    temperature=0.3,
    region="us-east-1",
)

agent = Agent(
    model=bedrock_model,
)
agent("Strandsってどういう意味?")
u1u1

上手く動いた最終形態はこれ
˙.aws/configregion = ap-northeast-1` が設定されていたため接続先のリージョンを設定してやる必要がありましたとさ。
わかってみれば当たり前ですけども。

from strands import Agent
from strands.models import BedrockModel
import boto3


# Bedrock
bedrock_model = BedrockModel(
    model_id="anthropic.claude-3-5-sonnet-20240620-v1:0",
    # model_id="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
    # model_id="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
    temperature=0.3,
    boto_session=boto3.Session(profile_name="default", region_name="us-east-1"),
    # region="us-east-1",
)

agent = Agent(
    model=bedrock_model,
)

try:
    response = agent("Strandsってどういう意味?")
    print(response)
except Exception as e:
    print(e)
u1u1

calculatorツールを使ってみる
https://github.com/strands-agents/tools

agent = Agent(
    model=bedrock_model,
    tools=[
        calculator,
    ],
)

response = agent("2 x 9 + 2 は?")
print(response)

以下のような回答が返却されました

この計算を行うために、calculatorツールを使用します。式は "2 * 9 + 2" となります。
Tool #1: calculator


計算結果は以下の通りです:

2 × 9 + 2 = 20

したがって、「2 x 9 + 2」の答えは20です。

計算結果は以下の通りです:

2 × 9 + 2 = 20

したがって、「2 x 9 + 2」の答えは20です。
u1u1

http_request ツール良いですね

agent = Agent(
    model=bedrock_model,
    tools=[
        calculator,
        http_request,
    ],
)

response = agent("https://qiita.com/minorun365/items/dd05a3e4938482ac6055 の内容を教えて")
print(response)

回答:

この URL の内容を確認するために、HTTP リクエストを使用して Web ページの内容を取得する必要があります。`http_request` 関数を使用してこのタスクを実行しましょう。
Tool #1: http_request


この記事の内容を要約します:

1. AWSが新しいAIエージェント構築SDK「Strands Agents」を発表しました。

2. AIエージェントSDK開発の競争が激しくなっており、LangChain、Mastra、OpenAI、Google等も参入しています。

3. Strands Agentsは非常にシンプルで、数行のコードでAIエージェントを作成できます。

4. 記事では、Strands Agentsを使ったAIエージェントの作成方法が詳しく説明されています。

5. Strands AgentsはModel Context Protocol (MCP)にも対応しており、他の開発者が作成したツールも利用できます。

6. AWS BedrockユーザーにとってStrands Agentsは魅力的なツールとなる可能性があります。

7. 著者はMCPに関する入門書を出版予定であることも紹介されています。

この記事は、AWSの新しいAIエージェント開発ツールの紹介と、その基本的な使用方法を解説しています。AIエージェント開発に興味のある開発者にとって有用な情報が含まれています。
このスクラップは4ヶ月前にクローズされました