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

# 函数调用

> 使用模型生成结构化工具调用参数

函数调用适合让模型决定什么时候调用业务工具，例如查询订单、检索知识库、创建工单、调用搜索或执行内部动作。

<Info>
  AGICTO API 使用 OpenAI 兼容格式时，你可以沿用现有工具调用设计。具体可用参数取决于所选模型能力。
</Info>

## 工作方式

<Steps>
  <Step title="定义工具">
    你向模型提供工具名称、说明和 JSON 参数结构。
  </Step>

  <Step title="模型选择工具">
    模型根据用户问题生成工具调用参数。
  </Step>

  <Step title="你的系统执行工具">
    业务系统调用数据库、搜索、CRM 或内部 API。
  </Step>

  <Step title="把结果交回模型">
    模型根据工具结果生成最终回复。
  </Step>
</Steps>

## 请求示例

```bash theme={null}
curl https://api.agicto.cn/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_NAME",
    "messages": [
      {
        "role": "user",
        "content": "查询订单 A1001 的物流状态。"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_order_status",
          "description": "查询订单物流状态",
          "parameters": {
            "type": "object",
            "properties": {
              "order_id": {
                "type": "string",
                "description": "订单编号"
              }
            },
            "required": ["order_id"]
          }
        }
      }
    ]
  }'
```

## 设计建议

* 工具名称使用稳定的英文标识，例如 `get_order_status`。
* 参数结构尽量小，避免把业务文档塞进工具定义。
* 工具说明写清楚“什么时候用”，不要只写“查询工具”。
* 对模型生成的参数做服务端校验。
* 工具执行失败时，将失败原因作为工具结果返回给模型。

## 常见工具类型

| 工具       | 作用                   |
| -------- | -------------------- |
| Search   | 检索外部网页或内部知识库。        |
| Database | 查询订单、用户、库存和账单。       |
| Ticket   | 创建、更新或查询工单。          |
| Action   | 执行业务动作，例如退款、发券、发送通知。 |

<Card title="OpenAI 兼容格式" icon="code" href="/guides/openai-compatible">
  查看 SDK 迁移和兼容格式说明。
</Card>
