strands agentをローカルで体験してみる

Ai

こんばんは!みなさんBedrock 使っていますか?最近Bedrock agentCore というものが出たらしく、早速使ってみたくなったので、使ってみます!!agent Core はまだプレビュー中なのと、東京リージョンでは使用できないので、今回はバージニア北部で使用していきます。

今回の内容

今回はローカルで実行して、POST したresult が返されるまでのブログとします。
次回のブログでconsole を使って実際にagent を登録していきます。

事前準備

uv でpython のバージョンを管理していきたいと思いますので、まずはuv のインストールから初めていきます。uv をインストールしている人は飛ばしてもらっても大丈夫です!

uv コマンドのインストール

uv をcurl で取得します。

curl -LsSf https://astral.sh/uv/install.sh | sh

uv のバージョン確認

uv --version
uv 0.8.14

Bedrock agent を使うためにプロジェクトを作成していきます。

uv init

こんな感じで色々ファイルができます。

strands agent などのインストール

python-version を見てみると3.8 になっていたので、ちょっと古いので、3.11 になおしておきます。

 uv add strands-agents bedrock-agentcore bedrock-agentcore-starter-toolkit

こんな感じでエラーが発生する場合は、python のバージョンが原因なので、pyenv でバージョンをあわせてあげて、

warning: Ignoring unsupported Python request `pypy3.11-7.3.19` in version file: /home/<your user>/pj/bedrock-agent/.python-version
  × No solution found when resolving dependencies for split (markers: python_full_version >= '3.8' and python_full_version
  │ < '3.10'):
  ╰─▶ Because the requested Python version (>=3.8) does not satisfy Python>=3.10 and all versions of
      bedrock-agentcore-starter-toolkit depend on Python>=3.10, we can conclude that all versions of
      bedrock-agentcore-starter-toolkit cannot be used.
      And because your project depends on bedrock-agentcore-starter-toolkit, we can conclude that your project's requirements
      are unsatisfiable.

      hint: The `requires-python` value (>=3.8) includes Python versions that are not supported by your dependencies (e.g., all
      versions of bedrock-agentcore-starter-toolkit only supports >=3.10). Consider using a more restrictive `requires-python`
      value (like >=3.10).

      hint: While the active Python version is 3.11, the resolution failed for other Python versions supported by your project.
      Consider limiting your project's supported Python versions using `requires-python`.
  help: If you want to add the package regardless of the failed resolution, provide the `--frozen` flag to skip locking and
        syncing.

toml ファイルでpython のバージョンも変更してあげます。

[project]
name = "bedrock-agent"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"  // 3.11 使うので3.10 以上に設定
dependencies = [
    "bedrock-agentcore>=0.1.2",
    "bedrock-agentcore-starter-toolkit>=0.1.8",
    "strands-agents>=1.7.0",
]

そうしたら、問題なくインストールが完了します!!これで事前準備自体はオッケーになります。
us-east-1 でモデルを有効化していない場合はこの手順で有効にしておいてください!

Bedrock をpython から叩く

ここまできたら、python でBedrock agentをたたきます。

main.py の内容

main.py です。

from strands import Agent
from strands.models import BedrockModel
from bedrock_agentcore.runtime import BedrockAgentCoreApp

app = BedrockAgentCoreApp()
bedrock_model = BedrockModel(
    region_name="us-east-1",
    model_id="anthropic.claude-sonnet-4-20250514-v1:0",
)
agent = Agent(
    model=bedrock_model,
    system_prompt="You are a helpful AI assistant"
 )

@app.entrypoint
def invoke(payload):
    """Process user input and return a response"""
    user_message = payload.get("prompt","Hello")
    result = agent(user_message)
    return {"result": result.message}

if __name__ == "__main__":
    app.run()

コードは下記のページからお借りしています。

Amazon Bedrock AgentCoreって何?StrandsAgentでLine Bot作ってデプロイしてみよう! - Qiita
吾輩は人間である 前書き 2025年07月17日、New Yorkで開催されたAWS Summitにおいて、Amazon Bedrock AgentCoreというサービスが発表されました。 このサービスは名前にAmazon Bedrockと...

python サーバー実行

仮想環境で、python を実行できるようにします。

uv run main.py 

実行されている状態で、POST でpayload を送ります。

 curl -X POST http://localhost:8080/invocations -H "Content-Type: application/json" -d '{"prompt": "こんにちは!"}'

# 結果
{"result": {"role": "assistant", "content": [{"text": "こんにちは!どうぞよろしくお願いします。何か質問やご要望がありましたらお聞かせください。できる限り丁寧にお答えさせていただきますので、お気軽にお話しください。"}]}

このように回答がかえってきたらオッケーです!

よくあるエラー

このエラーが発生する場合は、cross inference がサポートされているmodel を選択してください。

when calling the ConverseStream operation: Invocation of model ID anthropic.claude-3-5-sonnet-20241022-v2:0 with on-demand throughput isn’t supported.

ここから選択します!

次回のブログ

今回はローカルで実行するまでゴールでしたが、次回のブログでBedrock agent を登録して、実行していこうと思います。

参考サイト

Strands Agents
AI-powered agents for modern workflows

\ 最新情報をチェック /

コメント

PAGE TOP
タイトルとURLをコピーしました