创建级联工具输入#

当一个输入字段的值决定显示哪些后续输入时,级联输入设置非常有用。这使输入过程更加简化、用户友好且无错误。本指南将引导您如何为工具创建级联输入。

先决条件#

请确保您已安装最新版本的适用于 VS Code 的 Prompt flow(v1.2.0+)。

创建具有级联输入的工具#

我们将构建一个示例工具来展示级联输入的工作原理。student_idteacher_id 输入将由为 user_type 输入选择的值控制。以下是如何在工具代码和 YAML 中配置此功能。

  1. 按照级联输入示例开发工具函数。要点:

    • 使用 @tool 装饰器将函数标记为工具。

    • UserType 定义为枚举类,因为在此示例中它只接受一组特定的固定值。

    • 根据 user_type 在工具逻辑中有条件地使用输入。

from enum import Enum

from promptflow.core import tool


class UserType(str, Enum):
    STUDENT = "student"
    TEACHER = "teacher"


@tool
def my_tool(user_type: Enum, student_id: str = "", teacher_id: str = "") -> str:
    """This is a dummy function to support cascading inputs.

    :param user_type: user type, student or teacher.
    :param student_id: student id.
    :param teacher_id: teacher id.
    :return: id of the user.
    If user_type is student, return student_id.
    If user_type is teacher, return teacher_id.
    """
    if user_type == UserType.STUDENT:
        return student_id
    elif user_type == UserType.TEACHER:
        return teacher_id
    else:
        raise Exception("Invalid user.")
  1. 根据工具包指南为您的工具生成一个初始 YAML,然后更新它以启用级联。

    添加 enabled_byenabled_by_value 以控制依赖输入的可见性。请参阅示例 YAML 以供参考。

    • enabled_by 属性指定输入字段,该字段必须是枚举类型,它控制依赖输入字段的可见性。

    • enabled_by_value 属性定义来自 enabled_by 字段的接受的枚举值,这些值将使此依赖输入字段可见。

    注意:enabled_by_value 接受一个列表,允许多个值来启用一个输入。

my_tool_package.tools.tool_with_cascading_inputs.my_tool:
  function: my_tool
  inputs:
    user_type:
      type:
      - string
      enum:
        - student
        - teacher
    student_id:
      type:
      - string
      # This input is enabled by the input "user_type".
      enabled_by: user_type
      # This input is enabled when "user_type" is "student".
      enabled_by_value: [student]
    teacher_id:
      type:
        - string
      enabled_by: user_type
      enabled_by_value: [teacher]
  module: my_tool_package.tools.tool_with_cascading_inputs
  name: My Tool with Cascading Inputs
  description: This is my tool with cascading inputs
  type: python

在 VS Code 中使用工具#

一旦您打包并共享您的工具,您可以根据工具包指南在 VS Code 中使用它。我们有一个演示流您可以尝试。

在选择 user_type 之前,student_idteacher_id 输入是隐藏的。一旦您选择 user_type,相应的输入就会出现。before_user_type_selected.png after_user_type_selected_with_student.png after_user_type_selected_with_teacher.png

常见问题#

如何创建多层级联输入?#

如果您正在处理多个级别的级联输入,您可以通过使用 enabled_byenabled_by_value 属性来有效管理它们之间的依赖关系。例如:

my_tool_package.tools.tool_with_multi_layer_cascading_inputs.my_tool:
  function: my_tool
  inputs:
    event_type:
      type:
      - string
      enum:
        - corporate
        - private
    corporate_theme:
      type:
      - string
      # This input is enabled by the input "event_type".
      enabled_by: event_type
      # This input is enabled when "event_type" is "corporate".
      enabled_by_value: [corporate]
      enum:
        - seminar
        - team_building
    seminar_location:
      type:
      - string
      # This input is enabled by the input "corporate_theme".
      enabled_by: corporate_theme
      # This input is enabled when "corporate_theme" is "seminar".
      enabled_by_value: [seminar]
    private_theme:
      type:
        - string
      # This input is enabled by the input "event_type".
      enabled_by: event_type
      # This input is enabled when "event_type" is "private".
      enabled_by_value: [private]
  module: my_tool_package.tools.tool_with_multi_layer_cascading_inputs
  name: My Tool with Multi-Layer Cascading Inputs
  description: This is my tool with multi-layer cascading inputs
  type: python

输入将根据选择以级联方式启用。