Skip to main content

カスタムアプリケーションパラメーターのパススルー

Alibaba Cloud Model Studio の エージェント および ワークフロー (エージェントオーケストレーションアプリケーションに代わるアプリケーション)を呼び出す際に、主にカスタムプラグインおよびカスタムノード向けにカスタムパラメーターを渡します。

API キーの作成およびAPI キーを環境変数としてエクスポートします。SDK を使用して呼び出しを行う場合は、DashScope SDK をインストールしてください。

カスタムプラグインパラメーター

この記事では、寮規則照会ツールを例に、API を使用してカスタムプラグインにパラメーターを渡す方法を説明します。
カスタムプラグインのパラメーターは、関連付けられた エージェント または ワークフロー の プラグインノード経由で渡されます。
以下の例では、エージェント のカスタムプラグインパラメーターを渡す方法を示します。

使用方法

ステップ 1:カスタムプラグインの作成

すでにプラグインタールを作成済みまたはプラグインをインポート済みの場合は、このステップをスキップしてください。
  1. カスタムプラグインを作成します。Model Studio コンソールの プラグイン ページにアクセスし、Add Custom Plug-in をクリックします。プラグイン情報を入力します。認証が必要な場合は、認証の有効化 スイッチをオンにして設定を行います。
    プラグインの説明例: 寮規則を照会し、数値インデックスで特定の項目を検索します。
    プラグイン URL の例: https://domitorgreement-plugin-example-icohrkdjxy.cn-beijing.fcapp.run
    image
    注: Plug-in Description は、プラグインの目的を簡潔にまとめたものです。これにより、大規模言語モデルがタスクに対してプラグインを呼び出す必要があるかどうかを判断できます。自然言語で記述してください。
  2. ツールを作成します。ツール情報を入力し、入力および出力パラメーターを設定します。注:
    1. Tool Description は、大規模言語モデルがツールの機能とユースケースをよりよく理解するために役立ちます。自然言語を使用し、可能であれば例を記載してください。
    2. わかりやすい Parameter Name は、大規模言語モデルがその情報が何を表しているかを理解するのに役立ちます。
    3. Parameter Description は、入力パラメーターの機能を説明するものです。簡潔かつ正確に記述し、大規模言語モデルが正しい値を決定できるようにしてください。
    4. 入力パラメーターの Passing Method として、必ず 業務パススルー を選択してください。
    この例では、寮規則コンテンツのインデックス(article_index)をパススルーパラメーターとして設定します。
    image
  3. Test Tool をクリックします。テストが成功したら、Publish します。

ステップ 2:プラグインとエージェントの関連付け

プラグインは、同一ワークスペース内の エージェント のみに関連付けることができます。
  1. 公開済みプラグインのカードで、Add to Agent をクリックし、エージェントアプリケーションを選択します。
  2. または、アプリケーション内で + プラグイン をクリックしてカスタムプラグインを関連付けます。
  3. 最後に、Publish します。

ステップ 3:API 呼び出し

  • 認証不要の場合:API 経由でカスタムプラグインを呼び出すには、biz_params の user_defined_params パラメーターを使用してカスタムプラグイン情報を渡します。your_plugin_code は実際のプラグイン ID に置き換え、プラグインで設定した入力パラメーターのキーと値のペアを渡します。
    プラグイン ID はプラグインカード上で確認できます。
    この例では、article_index パラメーターに値 2 を渡すことで、2 番目の寮規則の内容を照会し、正しい結果を返します。
    • Python
    • Java
    • HTTP
    リクエストサンプル
    import os
    import dashscope
    # DashScope SDK のバージョンは 2.0.1 以降である必要があります。
    from dashscope.v2 import DashScopeError
    
    # 以下はシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。
    client = dashscope.Client(
            # 環境変数が設定されていない場合は、api_key = "sk-xxx" に置き換えてください。本番環境では API キーの漏洩を防ぐため、環境変数の使用を推奨します。
            api_key=os.getenv("DASHSCOPE_API_KEY"),
            workspace="{WorkspaceId}",
            region="ap-southeast-1")
    biz_params = {
        # エージェントアプリケーションのカスタムプラグインの入力パラメーターを渡します。`your_plugin_code` をプラグイン ID に置き換えてください。
        "user_defined_params": {
            "your_plugin_code": {
                "article_index": 2}}}
    try:
        response = client.apps.completions.create(
                app_id='YOUR_APP_ID',
                prompt='Dormitory convention content',
                biz_params=biz_params)
        print('%s\n' % (response.text))  # テキストのみを処理して出力します。
        # print('%s\n' % (response.usage))
    except DashScopeError as e:
        print(f'request_id={e.request_id}')
        print(f'code={e.code}')
        print(f'message={e.message}')
        print(f'See the documentation: https://www.alibabacloud.com/help/ja/model-studio/developer-reference/error-code')
    
    応答サンプル
    The second article of the dormitory convention is as follows:
    
    "Roommates must help, care for, and learn from each other to achieve mutual improvement. They must be tolerant, forgiving, respectful, and sincere."
    
    This suggests that roommates should foster a positive living and learning environment by supporting and helping each other, while also learning to understand and respect others. If you want to learn about other articles of the convention, just ask!
    
  • 認証が必要な場合:このシナリオでは、認証 が有効化され、ユーザレベル認証 が設定されたカスタムプラグインを呼び出す方法を説明します。
    プラグイン ID はプラグインカード上で確認できます。
    • biz_params フィールドの user_defined_params を使用してカスタムプラグイン情報を渡します。your_plugin_code はご利用のプラグイン ID に置き換え、入力パラメーターをキーと値のペアで指定します。
    • biz_params フィールドの user_defined_tokens を使用して認証情報を渡します。your_plugin_code はご利用のプラグイン ID に置き換え、user_token の値を認証トークン(例:DashScope API キー)に設定します。
    • 認証が成功すると、サービスは提供されたインデックスパラメーターに基づいて指定された項目を照会し、正しい結果を返します。
    この例では、article_index パラメーターに 2 を設定し、user_token の値である YOUR_TOKEN を実際の DASHSCOPE_API_KEY に置き換えます。認証が成功すると、2 番目の寮規則の内容が照会され、正しい結果が返されます。
    • Python
    • Java
    • HTTP
    リクエストサンプル
    import os
    import dashscope
    # DashScope SDK のバージョンは 2.0.1 以降である必要があります。
    from dashscope.v2 import DashScopeError
    
    # 以下はシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。
    client = dashscope.Client(
                # 環境変数が設定されていない場合は、api_key = "sk-xxx" に置き換えてください。ただし、キーの漏洩リスクを低減するため、本番環境で API キーをハードコードすることは推奨されません。
                api_key=os.getenv("DASHSCOPE_API_KEY"),
                workspace="{WorkspaceId}",
                region="ap-southeast-1")
    biz_params = {
        # エージェントアプリケーションのカスタムプラグイン用の認証情報を渡します。
        # your_plugin_code をプラグイン ID に、YOUR_TOKEN を API キーなどの認証トークンに置き換えます。
        "user_defined_params": {
            "your_plugin_code": {
                "article_index": 2}},
        "user_defined_tokens": {
            "your_plugin_code": {
                "user_token": "YOUR_TOKEN"}}}
    try:
        response = client.apps.completions.create(
                    app_id='YOUR_APP_ID',
                    prompt='Dormitory convention content',
                    biz_params=biz_params)
        print('%s\n' % (response.text))  # テキストのみを処理して出力します。
        # print('%s\n' % (response.usage))
    except DashScopeError as e:
        print(f'request_id={e.request_id}')
        print(f'code={e.code}')
        print(f'message={e.message}')
        print(f'See the documentation: https://www.alibabacloud.com/help/ja/model-studio/developer-reference/error-code')
    
    応答サンプル
    The second article of the dormitory convention is as follows:
    
    Roommates must help, care for, and learn from each other to achieve mutual improvement. They must be tolerant, forgiving, respectful, and sincere.
    
    If you need to know about other articles of the convention, please let me know.
    

カスタムノードパラメーターの渡し方

このトピックでは、都市名による行政区画の照会の例を用いて、API 呼び出しを通じてアプリケーションにカスタムノードパラメーターを渡す方法を説明します。
アプリケーションのカスタムノードパラメーターは、ワークフロー の開始ノードを通じて渡されます。
以下の例では、ワークフロー の開始ノードにカスタムパラメーターを渡す方法を示します。

使用方法

ステップ 1:カスタムノードパラメーターの設定

Model Studio コンソールの マイアプリケーション ページにアクセスします。ワークフロー を選択し、その 開始ノード にカスタムパラメーターを定義します。この例では、city という名前の文字列型変数を作成します。次に、プロンプト に city および query 変数を挿入し、Publish します。

ステップ 2:API 呼び出し

API 呼び出しを行う際、biz_params フィールドに city の値を、prompt フィールドに query の値を渡します。
  • Python
  • Java
  • HTTP
リクエストサンプル
import os
import dashscope
from dashscope.v2 import DashScopeError

# 以下はシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。
client = dashscope.Client(
    # 環境変数が設定されていない場合は、api_key = "sk-xxx" に置き換えてください。ただし、本番環境で API キーを直接コードにハードコードするのは、漏洩リスクを高めるため推奨されません。
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)
# ワークフローアプリケーションのカスタムパラメーターを渡します。
biz_params = {"city": "Hangzhou"}
try:
    response = client.apps.completions.create(
        app_id='YOUR_APP_ID',  # 実際のアプリケーション ID に置き換えてください。
        prompt='Query the administrative divisions of this city',
        biz_params=biz_params  # ビジネスパラメーターを渡します。
    )
    print(f'{response.text}')  # テキストのみの応答を出力します。
except DashScopeError as e:
    print(f'request_id={e.request_id}')
    print(f'code={e.code}')
    print(f'message={e.message}')
    print(f'See the documentation: https://www.alibabacloud.com/help/ja/model-studio/developer-reference/error-code')
応答サンプル
Hangzhou, the capital city of Zhejiang Province, is divided into 10 municipal districts: Shangcheng, Gongshu, Xihu, Binjiang, Xiaoshan, Yuhang, Linping, Qiantang, Fuyang, and Lin'an. Each district has its own unique features and development priorities.

- Shangcheng District: Located in the center of Hangzhou, it is one of the city's political, economic, and cultural hubs.
- Gongshu District: Known for its canal culture and numerous historical and cultural heritage sites.
- Xihu District: Home to the famous West Lake scenic area, a major tourist destination.
- Binjiang District: A hub for high-tech industries, where well-known companies such as Alibaba are located.
- Xiaoshan District: An administrative district in the southeast known for its rapid economic development, especially in manufacturing.
- Yuhang District: Has developed rapidly in recent years, especially in the internet economy. Alibaba's headquarters is also located here (Note: Alibaba's headquarters is actually in Binjiang District).
- Linping District: A newly established administrative district aimed at promoting comprehensive economic and social development in the area.
- Qiantang District: Also a result of recent administrative division adjustments, it emphasizes both innovative development and ecological protection.
- Fuyang District: Located to the southwest of Hangzhou, it is known for its rich natural landscapes and long history and culture.
- Lin'an District: Situated in the western part of Hangzhou, it is famous for its beautiful ecology and profound cultural heritage.

Note that specific urban planning may change over time. Refer to the latest official information.

関連ドキュメント

カスタムプラグイン:カスタムプラグインの作成方法について学習します。 アプリケーションの呼び出し:アプリケーションの呼び出し方法とその他のユースケースについて学習します。 アプリケーション呼び出し API:完全なパラメーターリストと呼び出し例を参照します。