You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAI API函数调用:能否将参数类型设为列表与字典?

ChatGPT函数调用:列表与字典类型参数的配置方案

核心结论

完全可以将to_card_list、component_list设为**数组(array)类型,parameter_dict设为对象(object)**类型。之前修改类型触发报错,是因为参数定义不符合OpenAI函数调用的JSON Schema规范。

正确的参数定义示例

按照OpenAI规范调整后的函数定义如下(修正类型并补充必要的结构约束):

{
  "name": "set_absolute_parameter_values_for_component",
  "example": "Change the margin to 'comfortable' for 'ProfileCard' and 'SettingsCard': {\"to_card_list\": [\"ProfileCard\", \"SettingsCard\"], \"component_list\": [\"Content\"], \"parameter_dict\": {\"margin\": \"comfortable\"}}",
  "description": "Set specific values for parameters of a single component in the specified card(s). This function applies absolute changes. Note: the 'Content' component cannot contain text, but 'Info' can hold text. Call this function if both a relative and an absolute term are used.",
  "parameters": {
    "type": "object",
    "properties": {
      "to_card_list": {
        "type": "array",
        "description": "List of card names that the user wants to update",
        "items": {
          "type": "string",
          "enum": CARD_INFO['cards']
        }
      },
      "component_list": {
        "type": "array",
        "description": "List of components that the user wants to update. Note: the 'Content' component can NOT contain text",
        "items": {
          "type": "string",
          "enum": CARD_INFO['component_list']
        }
      },
      "parameter_dict": {
        "type": "object",
        "description": "Dictionary mapping parameters to their desired values",
        "additionalProperties": {
          "type": ["string", "number", "boolean"]
        }
      }
    },
    "required": ["to_card_list", "component_list", "parameter_dict"]
  }
}

关键修正点说明

  1. 数组类型配置:
    • 将type从string改为array
    • 通过items字段定义数组元素的类型(这里是string),并在items内添加enum约束可选值,确保模型返回符合预期的元素
  2. 对象类型配置:
    • 将type从string改为object
    • 使用additionalProperties定义字典值的允许类型(可根据实际需求调整,比如只允许字符串);如果参数键是固定枚举,可改用properties字段逐一定义每个键的规则
  3. 格式稳定性优化:
    • 在example中补充完整的参数结构示例,给模型更明确的格式参考
    • 新增required字段标记必填参数,减少模型返回缺失参数的情况

内容的提问来源于stack exchange,提问作者Emil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:30:04