次の方法で共有


Python ツール

Python ツールを使用すると、カスタマイズされたコード スニペットをプロンプト フローの自己完結型実行可能ノードとして提供できます。 Python ツールの作成、コードの編集、結果の確認を簡単に行うことができます。

入力

Name タイプ 内容 必須
コード string Python のコード スニペット はい
入力 - ツール関数のパラメーターとその割り当ての一覧 -

種類

Type Python の例 説明
int param: int 整数型
[bool] param: bool ブール型
string param: str 文字列の種類
倍精度浮動小数点 param: float 倍精度型
list param: list または param: List[T] リスト タイプ
object param: dict または param: Dict[K, V] オブジェクトの種類
接続 param: CustomConnection 接続の種類は特別に処理されます

型注釈を Connection 持つパラメーターは、接続入力として扱われます。つまり、次のことを意味します。

  • プロンプト フロー拡張機能には、接続を選択するセレクターが表示されます。
  • 実行時に、プロンプト フローは、渡されたパラメーター値から同じ名前の接続を検索しようとします。

Note

型注釈はUnion[...]、接続の種類に対してのみサポートされます。たとえば、 param: Union[CustomConnection, OpenAIConnection]

出力

出力は Python ツール関数の戻り値です。

Python ツールを使用した書き込み

Python ツールを使用して記述するには、次のガイドラインを使用します。

ガイドライン

  • Python ツール コードは、必要なモジュールのインポートを含む完全な Python コードで構成されている必要があります。

  • Python ツール コードには、実行のエントリ ポイントとして機能する (ツール関数) で修飾された @tool 関数が含まれている必要があります。 スニペット内でデコレーターを @tool 1 回だけ適用します。

    次のセクションのサンプルでは、@tool.my_python_tool.

  • このセクションでは、Python ツール関数パラメーターを Inputs 割り当てる必要があります。

    次のセクションのサンプルでは、入力 message を定義して割り当てます world

  • Python ツール関数には戻り値があります。

    次のセクションのサンプルでは、連結された文字列が返されます。

コード

次のスニペットは、ツール関数の基本的な構造を示しています。 プロンプト フローは、関数を読み取り、関数パラメーターと型注釈から入力を抽出します。

from promptflow import tool
from promptflow.connections import CustomConnection

# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need
@tool
def my_python_tool(message: str, my_conn: CustomConnection) -> str:
    my_conn_dict = dict(my_conn)
    # Do some function call with my_conn_dict...
    return 'hello ' + message

入力

Name Type フロー YAML のサンプル値 関数に渡される値
message string world world
my_conn CustomConnection my_conn CustomConnection オブジェクト

プロンプト フローは、実行時に名前が付けられた my_conn 接続の検索を試みます。

出力

"hello world"

Python ツールでのカスタム接続

認証を使用して外部サービスを呼び出す必要がある Python ツールを開発している場合は、プロンプト フローでカスタム接続を使用します。 これを使用して、アクセス キーを安全に格納し、Python コードで取得できます。

カスタム接続を作成する

大規模言語モデル API キーまたはその他の必要な資格情報をすべて格納するカスタム接続を作成します。

  1. ワークスペースのプロンプト フローに移動し、[接続] タブに移動します。

  2. [カスタムの作成]>を選択します。

    Screenshot that shows flows on the Connections tab highlighting the Custom button in the drop-down menu.

  3. 右側のウィンドウで、接続名を定義できます。 [キーと値のペアの追加] を選択すると、複数のキーと値のペアを追加して資格情報とキーを格納できます。

    Screenshot that shows adding a custom connection point and the Add key-value pairs button.

Note

1 つのキーと値のペアをシークレットとして設定するには、is secret チェック ボックスを選択します。 このオプションは、キー値を暗号化して格納します。 少なくとも 1 つのキーと値のペアがシークレットとして設定されていることを確認します。 それ以外の場合、接続は正常に作成されません。

Python でカスタム接続を使用する

Python コードでカスタム接続を使用するには:

  1. Python ノードのコード セクションで、カスタム接続ライブラリをインポートします from promptflow.connections import CustomConnection。 ツール関数で型 CustomConnection の入力パラメーターを定義します。

    Screenshot that shows the doc search chain node highlighting the custom connection.

  2. 入力セクションへの入力を解析し、[値] ドロップダウンでターゲット のカスタム接続を選択します。

    Screenshot that shows the chain node highlighting the connection.

次に例を示します。

from promptflow import tool
from promptflow.connections import CustomConnection

@tool
def my_python_tool(message: str, myconn: CustomConnection) -> str:
    # Get authentication key-values from the custom connection
    connection_key1_value = myconn.key1
    connection_key2_value = myconn.key2