Agno でツールを使用して AI エージェントを構築する方法を学習します。エージェントはツールを使用してアクションを実行し、外部システムとやり取りをします。ツールは、エージェントがタスクを達成するために実行できる関数です。例えば: web 検索、SQL 実行、電子メールbの送信、あるいは API 呼び出し。
Agno : ユーザガイド : コンセプト : エージェント – ツール
作成 : クラスキャット・セールスインフォメーション
作成日時 : 07/19/2025
バージョン : Agno 1.7.4
* 本記事は docs.agno.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
Agno ユーザガイド : コンセプト : エージェント – ツール
Agno でツールを使用して AI エージェントを構築する方法を学習します。
エージェントはツールを使用してアクションを実行し、外部システムとやり取りをします。
ツールは、エージェントがタスクを達成するために実行できる関数です。例えば: web 検索、SQL 実行、電子メールの送信、あるいは API 呼び出し。任意の python 関数をツールとして使用したり、事前構築済みツールキットを使用することもできます。一般的な構文は :
from agno.agent import Agent
agent = Agent(
# Add functions or Toolkits
tools=[...],
# Show tool calls in the Agent response
show_tool_calls=True
)
ツールキットの使用
Agno は、エージェントに追加できる、事前構築済みツールキットを提供しています。例えば、DuckDuckGo ツールキットを使用して web を検索してみましょう。
Info : You can find more toolkits in the Toolkits guide.
- Web 検索エージェントを作成します。
ファイル web_search.py を作成します。
web_search.py
from agno.agent import Agent from agno.tools.duckduckgo import DuckDuckGoTools agent = Agent(tools=[DuckDuckGoTools()], show_tool_calls=True, markdown=True) agent.print_response("Whats happening in France?", stream=True)
- エージェントの実行
ライブラリのインストール
pip install openai duckduckgo-search agno
エージェントの実行
python web_search.py
独自のツールの作成
より制御するには、独自の python 関数を作成してそれらをツールとしてエージェントに追加します。例えば、get_top_hackernews_stories ツールをエージェントに追加する方法は以下になります。
hn_agent.py
import json
import httpx
from agno.agent import Agent
def get_top_hackernews_stories(num_stories: int = 10) -> str:
"""Use this function to get top stories from Hacker News.
Args:
num_stories (int): Number of stories to return. Defaults to 10.
Returns:
str: JSON string of top stories.
"""
# Fetch top story IDs
response = httpx.get('https://hacker-news.firebaseio.com/v0/topstories.json')
story_ids = response.json()
# Fetch story details
stories = []
for story_id in story_ids[:num_stories]:
story_response = httpx.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')
story = story_response.json()
if "text" in story:
story.pop("text", None)
stories.append(story)
return json.dumps(stories)
agent = Agent(tools=[get_top_hackernews_stories], show_tool_calls=True, markdown=True)
agent.print_response("Summarize the top 5 stories on hackernews?", stream=True)
以上