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

# 为角色添加工具

工具调用使角色能够使用你的应用能力。模型请求执行某项操作；你的应用负责验证请求、执行操作并返回结果。

### 1. 定义可用操作

`change_outfit` 是由应用定义的函数。以下示例提供粉色半身裙和碎花连衣裙两种选择。将函数定义添加到会话请求的 `conversation.tools` 中。

```json wrap theme={null}
{
  "conversation": {
    "tools": [
      {
        "type": "function",
        "name": "change_outfit",
        "description": "Switch to a prepared outfit image when the viewer explicitly requests an available outfit. Ask which outfit they mean if the choice is unclear. Do not switch after a casual mention or a request not to change.",
        "parameters": {
          "type": "object",
          "properties": {
            "outfit": {
              "type": "string",
              "enum": [
                "pink_skirt",
                "floral_dress"
              ]
            }
          },
          "required": [
            "outfit"
          ],
          "additionalProperties": false
        }
      }
    ]
  }
}
```

### 2. 接收并执行调用

处理 `response.output_item.done` 中的 `function_call`。解析 `arguments` 中的 JSON 字符串，验证工具名称和允许的取值，然后执行应用的业务函数。使用会话 ID 和 `call_id` 进行去重。

```json theme={null}
{
  "type": "response.output_item.done",
  "response_id": "resp_outfit_01",
  "output_index": 0,
  "item": {
    "type": "function_call",
    "name": "change_outfit",
    "call_id": "call_outfit_01",
    "arguments": "{\"outfit\":\"floral_dress\"}"
  }
}
```

在本例中，将服装映射到一张已注册的图片，然后调用图片变更 API。注册和更新步骤请参阅[更换图片](https://docs.vivix.ai/cn/streaming-avatar/interaction/outfits-and-scenes.md)。工具调用不会自动执行业务操作。

### 3. 返回实际结果

使用原始 `call_id`，以 `function_call_output` 的形式返回字符串结果。对于图片变更，应等待对应的 `session.update.done` 状态；`session.updated` 只表示请求已被接受。失败、取消和未确认的超时都应按其实际状态返回。

```json wrap theme={null}
{
  "event_id": "tool_result_01",
  "type": "conversation.item.create",
  "item": {
    "id": "outfit_result_01",
    "type": "function_call_output",
    "call_id": "call_outfit_01",
    "output": "{\"status\":\"completed\",\"outfit\":\"floral_dress\",\"source_image_id\":\"look_floral_dress\"}"
  }
}
```

### 4. 继续生成回复

等待原始 `response.done`，并确认该响应中每个工具结果都收到对应的 `conversation.item.created` 事件，然后只发送一次 `response.create`。使用 `response_id` 跟踪后续生成，不要在每个工具完成后分别继续。

```json theme={null}
{
  "event_id": "continue_outfit_01",
  "type": "response.create",
  "response": {
    "scheduling_policy": "after_current_response"
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.