1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

目的

AWSから提供されているStrands Agentsについて、使い勝手や特徴を理解するために触ってみました。初学者のため誤った情報など含まれているかと思います、その際はご指摘いただけますと幸いです。

参考にしたのは、こちらの公式ドキュメントです。
https://strandsagents.com/1.x/documentation/docs/

準備

  • Pythonの導入
  • AWS環境へのログイン(aws configure または aws login)
  • Strands Agentsの導入
    • pip install strands-agents
    • pip install strands-agents-tools

実践

1. まずは動かしてみる

※デフォルトでは「Claude 4 Sonnet inference model」が使用されるため、アカウントで該当モデルが使用できる状態になっている必要があります。初めてAnthoropicモデルを使用する場合には、Bedrockの「Model Catalog」ページからユースケースを提出する必要があります。

agent.py
from strands import Agent

# Create an agent with default settings
agent = Agent()

# Ask the agent a question
agent("Agentic AIについて教えて")

出力:

Agentic AIは、単なる質問応答を超えて、**自律的に目標に向かって行動・判断できるAIシステム**です。従来のAIが「指示を受けて反応する」のに対し、Agentic AIは「自ら計画を立てて実行する」特徴がありま す。

## 主な特徴

### 1. **自律性(Autonomy)**
- 最小限の人間の介入で動作
- 状況に応じて独自の判断を下す

### 2. **目標指向性**
- 設定された目標に向かって継続的に行動
- 複数のタスクを統合的に処理

### 3. **環境への適応**
- 外部環境の変化を認識
- 新しい情報に基づいて行動を修正

### 4. **ツール活用**
- 外部APIやデータベースにアクセス
- 必要に応じて他のシステムと連携

## 活用例

- **業務自動化**: スケジュール管理、メール対応
- **研究支援**: 文献調査、データ分析の自動実行  
- **カスタマーサポート**: 問題解決まで一貫して対応
- **コンテンツ制作**: 企画から執筆まで自律的に実行

## 従来AIとの違い

| 従来のAI | Agentic AI |
|---------|------------|
| 単発の応答 | 継続的な行動 |
| 受動的 | 能動的 |
| タスク特化 | 複合タスク対応 |

現在、この分野は急速に発展しており、今後様々な領域での実用化が期待されています。

3行程度のコードで、エージェントを動かせることがわかります!

2. 組み込みのツールを使ってみる

エージェントを定義する際に、組み込みのツールであるcalculatorとcurrent_timeを指定します。プロンプトには、それらのツールを使う質問を入れています。

agent-tools.py
from strands import Agent, tool
from strands_tools import calculator, current_time

# Create an agent with tools from the community-driven strands-tools package
agent = Agent(tools=[calculator, current_time])

# Ask the agent a question that uses the available tools
message = """
リクエストが2つあります:

1. 今の時間は?
2. 3111696 / 74088 を計算してください
"""
agent(message)

出力:

2つのリクエストにお答えします。
Tool #1: current_time

Tool #2: calculator
1. **現在時刻**: 2026年1月4日 6時1分1秒(UTC時間)です。

2. **計算結果**: 3,111,696 ÷ 74,088 = **42** です。

きれいに割り切れる数字ですね!

エージェントがツールを使用していることがわかります!

3. カスタムツールを定義してみる

pythonのlen関数を使った機能を実装しています。
また、ツールの冒頭部分にはLLMがどのようなときにツールを使うか判断できるようツールの説明と必要な引数を書いています。

agent-customtools.py
from strands import Agent, tool
from strands_tools import calculator, current_time

# Define a custom tool as a Python function using the @tool decorator
@tool
def letter_counter(word: str, letter: str) -> int:
    """
    Count occurrences of a specific letter in a word.

    Args:
        word (str): The input word to search in
        letter (str): The specific letter to count

    Returns:
        int: The number of occurrences of the letter in the word
    """
    if not isinstance(word, str) or not isinstance(letter, str):
        return 0

    if len(letter) != 1:
        raise ValueError("The 'letter' parameter must be a single character")

    return word.lower().count(letter.lower())

# Create an agent with tools from the community-driven strands-tools package
agent = Agent(tools=[calculator, current_time, letter_counter])

# Ask the agent a question that uses the available tools
message = """
リクエストが3つあります:

1. 今の時間は?
2. 3111696 / 74088 を計算してください
3. "strawberry" に含まれる文字 "r" の数を教えてください
"""
agent(message)

出力:

3つのリクエストにお答えします。
Tool #1: current_time

Tool #2: calculator

Tool #3: letter_counter
お答えします:

1. **現在時刻**: 2026年1月4日 6時13分46秒 (UTC)

2. **計算結果**: 3,111,696 ÷ 74,088 = 42

3. **文字数**: "strawberry" に含まれる文字 "r" は **3個** です

カスタムツールも、適切に使用することができています!

まとめ

想像していたよりも簡単にエージェントを実装できることがわかりました。
実際のエージェント開発の際には外部連携など多様な要素を含める必要があるため、追加の検証など実施してみようと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?