> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 动态用户输入（Agentic）

> 本示例演示了如何使用 `UserControlFlowTools` 实现动态用户输入收集，使代理能够在执行过程中根据需要请求信息。

## 代码

```python cookbook/agent_concepts/user_control_flows/agentic_user_input.py theme={null}
from typing import List

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.function import UserInputField
from agno.tools.toolkit import Toolkit
from agno.tools.user_control_flow import UserControlFlowTools
from agno.utils import pprint

class EmailTools(Toolkit):
    def __init__(self, *args, **kwargs):
        super().__init__(
            name="EmailTools", tools=[self.send_email, self.get_emails], *args, **kwargs
        )

    def send_email(self, subject: str, body: str, to_address: str) -> str:
        """向给定地址发送带有给定主题和正文的电子邮件。

        Args:
            subject (str): 邮件主题。
            body (str): 邮件正文。
            to_address (str): 要发送邮件的地址。
        """
        return f"Sent email to {to_address} with subject {subject} and body {body}"

    def get_emails(self, date_from: str, date_to: str) -> str:
        """获取给定日期范围内的所有邮件。

        Args:
            date_from (str): 开始日期（格式为 YYYY-MM-DD）。
            date_to (str): 结束日期（格式为 YYYY-MM-DD）。
        """
        return [
            {
                "subject": "Hello",
                "body": "Hello, world!",
                "to_address": "test@test.com",
                "date": date_from,
            },
            {
                "subject": "Random other email",
                "body": "This is a random other email",
                "to_address": "john@doe.com",
                "date": date_to,
            },
        ]

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    tools=[EmailTools(), UserControlFlowTools()],
    markdown=True,
    debug_mode=True,
)

run_response = agent.run("Send an email with the body 'What is the weather in Tokyo?'")
# 我们使用一个 while 循环来持续运行，直到代理对用户输入感到满意为止
while run_response.is_paused:
    for tool in run_response.tools_requiring_user_input:
        input_schema: List[UserInputField] = tool.user_input_schema

        for field in input_schema:
            # 向用户显示字段信息
            print(f"\n字段: {field.name}")
            print(f"描述: {field.description}")
            print(f"类型: {field.field_type}")

            # 获取用户输入
            if field.value is None:
                user_value = input(f"请输入 {field.name} 的值: ")
            else:
                print(f"值: {field.value}")
                user_value = field.value

            # 更新字段值
            field.value = user_value

    run_response = agent.continue_run(run_response=run_response)
    if not run_response.is_paused:
        pprint.pprint_run_response(run_response)
        break
```

## 使用方法

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="设置您的 API 密钥">
    ```bash theme={null}
    export OPENAI_API_KEY=xxx
    ```
  </Step>

  <Step title="安装库">
    ```bash theme={null}
    pip install -U agno openai
    ```
  </Step>

  <Step title="运行示例">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/agent_concepts/user_control_flows/agentic_user_input.py
      ```

      ```bash Windows theme={null}
      python cookbook/agent_concepts/user_control_flows/agentic_user_input.py
      ```
    </CodeGroup>
  </Step>
</Steps>

## 主要特性

* 使用 `UserControlFlowTools` 进行动态用户输入收集
* 实现了一个包含多个可能需要用户输入的工具的工具集
* 处理多轮用户输入收集
* 演示了如何在每轮输入后继续代理执行

## 用例

* 动态表单式交互
