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

wxoでのpython toolの単体テスト時の出力の確認方法

0
Posted at

はじめに

2026年5月20日発表のADK v2.10.0 を前提にまとめています。
最新情報は適宜ご確認ください。

python toolのテストについて

watsonx Orchestrateではさまざまなツールが利用可能です。
代表的なものの一つにpython tool (またはpython toolkit)があります。

python toolを作成する際には、単体でも実行可能か確認することが推奨されています。
単体での実行を確認後、watsonx Orchestrateの実行環境へのimportを行なってください。

python toolの例

このドキュメントでは下記のpython toolを例として使用します。
企業情報を取得する機能です。

yfinance.pyファイル

import yfinance as yf
from pydantic import Field, BaseModel
from ibm_watsonx_orchestrate.agent_builder.tools import tool

class TickerInfo(BaseModel):
    """
    Represents the details of a company info.
    """
    longBusinessSummary: str = Field(description='business summary of the company')
    sector: str = Field(description='sector of the company')
    industry: str = Field(description='industry of the company')
    fullTimeEmployee: int = Field(description='number of the fulltime employee of the company')
    per: float = Field(description='price earning ratio of the company')
    pbr: float = Field(description='price book ratio of the company')
    analyst_price_targets: dict = Field(description='analyst_price_targets')


@tool
def get_company_info(symbol:str) -> TickerInfo:

    """
    企業のTickerSymbolを用いて企業情報を取得します。
    Args:
        symbol: Symbol of the company.
    Returns:
        TickerInfo: company info of the company.
    """
    try:
        ticker=yf.Ticker(symbol) 
        # print(ticker.actions)
        return TickerInfo(analyst_price_targets=ticker.analyst_price_targets,longBusinessSummary=ticker.info['longBusinessSummary'],fullTimeEmployee=ticker.info['fullTimeEmployees'],sector=ticker.info['sector'],industry=ticker.info['industry'],per=ticker.info['trailingPE'],pbr=ticker.info['priceToBook'])
    except Exception as e:
        return e

python toolの出力の確認方法

@toolで定義される関数は、ibm_watsonx_orchestrate.agent_builder.tools._internal.tool_response.ToolResponseクラスで出力されます。例のようにクラスを定義していても単純なstrでも同様です。

ローカルでのテスト時には、下記のようにvars()などで出力を確認することが必要です。

if __name__ == '__main__':
    info = get_company_info('IBM')
    print(vars(info))

上記のコードをpython toolのソースの最後に付与し、python yfinance.py などのようなコマンドで実行して確認ができます。

この時、確認できる出力例は下記の通りです。

{
  "content": {
    "longBusinessSummary": "International Business Machines Corporation, ...中略....",
    "sector": "Technology",
    "industry": "Information Technology Services",
    "fullTimeEmployee": 264300,
    "per": 22.366932,
    "pbr": 7.2106147,
    "analyst_price_targets": {
      "current": 252.97,
      "high": 335.0,
      "low": 195.0,
      "mean": 277.68,
      "median": 282.5
    }
  },
  "context_updates": {},
  "structuredContent": null
}

contentの中身が出力内容になるので、下記のようにcontent部分のみ出力することで確認が簡単になります。

if __name__ == '__main__':
    info = get_company_info('IBM')
    print(vars(info)["content"])

まとめ

このように、python toolの単体テストを行っておくことで、watsonx Orchestrateの実行環境でのテストや開発ではエージェントの振る舞いの確認や改善に注力することができます。

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