Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Strands Agents SDK で AIエージェント作成 を試してみた / 202505...

Strands Agents SDK で AIエージェント作成 を試してみた / 20250525strands-agents

2025/05/25 (日) AI CRAFT Hacks Niigata #2 で発表した資料。

イベントページ:
https://ai-craft-hacks-niigata.connpass.com/event/354127/

Avatar for kasacchiful

kasacchiful

May 25, 2025
Tweet

More Decks by kasacchiful

Other Decks in Programming

Transcript

  1. 笠原 宏 (@kasacchiful) クラスメソッド株式会社データ事業本部ソリューションアーキテクト 新潟市在住 AWS Community Builder (Serverless since

    2022 / 無事更新) JAWS-UG新潟支部/Python機械学習勉強会in新潟/JaSST新潟/ASTER/ SWANII/Cloudflare Meetup Niigata/AI CRAFT Hacks Niigata 自己紹介
  2. LangGraph https://github.com/langchain- ai/langgraph メジャー LangChainのツール群 Mastra https://github.com/mastra- ai/mastra TypeScriptで書ける OpenAI

    Agents SDK https://github.com/openai/openai- agents-python OpenAI製 Agent Development Kit (ADK) https://github.com/google/adk- python Google製 Strands Agents SDK https://github.com/strands- agents/sdk-python AWS製 AIエージェント作成SDK
  3. A model-driven approach to building AI agents in just a

    few lines of code. Install pip install strands-agents strands-agents-tools デフォルトでは、AWSオレゴンリージョン (us-west-2) の Amazon Bedrock の Anthropic Claude 3.7 Sonnet の基盤モデルを利用して推論が行われます。 (事前にAWS CLIの設定をしておく必要があります) Strands Agents
  4. Quick Start from strands import Agent from strands_tools import calculator

    agent = Agent(tools=[calculator]) agent("1764の平方根は何ですか?") python quickstart.py 1764の平方根を計算したいですね。これは計算機能を使って求めることができます。 Tool #1: calculator 1764の平方根は「42」です。 数学的には、平方根とはある数を2乗(自分自身を掛ける)するともとの数になる値のことです。この場合、42 × 42 = 1764 となります。**%** Strands Agents
  5. from strands import Agent, tool from strands_tools import current_time, python_repl,

    file_write @tool def letter_count(word: str, letter: str) -> int: return word.lower().count(letter.lower()) agent = Agent(tools=[current_time, python_repl, letter_count, file_write]) message = ''' いくつか質問がありますので、適宜回答してください。 1. 今の日本は何時ですか?日本時間でお答えください。また、ミリ秒単位以下は無視してください。 2. その時刻表示の中に「1」はいくつありますか? 3. ここまで話した内容をPythonのコードにしてください。テストした後にコード内容を示してください。 4. そのコードをファイル名「generated_code.py」としてファイルに出力してください。 ''' agent(message) もう少し色々してみる
  6. from datetime import datetime import pytz def get_japan_time(): # 日本のタイムゾーンで現在時刻を取得

    japan_timezone = pytz.timezone('Asia/Tokyo') current_time = datetime.now(japan_timezone) # ミリ秒以下を無視したフォーマット formatted_time = current_time.strftime("%Y-%m-%dT%H:%M:%S%z") return formatted_time def count_digit_in_string(text, digit): # 文字列内の特定の数字の出現回数をカウント return text.count(str(digit)) # 日本時間を取得 japan_time = get_japan_time() print(f"日本の現在時刻: {japan_time}") # 時刻表示の中で「1」がいくつあるかカウント count_of_ones = count_digit_in_string(japan_time, 1) print(f"時刻表示の中の「1」の数: {count_of_ones}個") # テスト実行 print("\n--- テスト実行結果 ---") test_time = "2025-05-22T21:15:03+09:00" test_count = count_digit_in_string(test_time, 1) print(f"テスト時刻: {test_time}") print(f"テストでの「1」の数: {test_count}個") 作成されたコードの例
  7. from strands import Agent from strands.tools.mcp import MCPClient from mcp

    import stdio_client, StdioServerParameters ## ローカルMCPサーバ定義 (Standard I/O) stdio_mcp_client = MCPClient(lambda: stdio_client( StdioServerParameters( command="docker", args=[ "run", "--rm", "--interactive", "--env", "FASTMCP_LOG_LEVEL=ERROR", "awslabs/aws-documentation-mcp-server:latest", ], ) )) ## 利用可能なツールを読み込んでエージェント設定 with stdio_mcp_client: agent = Agent(tools=stdio_mcp_client.list_tools_sync()) agent("BedrockのClaude 3.7 SonnetのモデルIDは何ですか?") 今回はローカルで起動しているMCPサーバにアクセスしてますが、リモートMCPサー バの利用ももちろん可能です。 MCPも使える
  8. Strands Agents Toolsで用意されている機能 File Operations Shell Integration Mem0 Memory HTTP

    Client Python Execution Mathematical Tools AWS Integration Image Processing Environment Management Journaling Advanced Reasoning Swarm Intelligence ツール
  9. AWSが発表したAIエージェント構築SDK「Strands Agents」を5分だけ触ってみよ う! #bedrock - Qiita Welcome - Strands Agents

    SDK Model Context Protocol (MCP) - Strands Agents SDK strands-agents/sdk-python: A model-driven approach to building AI agents in just a few lines of code. 参考