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"] } }
关键修正点说明
- 数组类型配置:
- 将
type从string改为array - 通过
items字段定义数组元素的类型(这里是string),并在items内添加enum约束可选值,确保模型返回符合预期的元素
- 将
- 对象类型配置:
- 将
type从string改为object - 使用
additionalProperties定义字典值的允许类型(可根据实际需求调整,比如只允许字符串);如果参数键是固定枚举,可改用properties字段逐一定义每个键的规则
- 将
- 格式稳定性优化:
- 在
example中补充完整的参数结构示例,给模型更明确的格式参考 - 新增
required字段标记必填参数,减少模型返回缺失参数的情况
- 在
内容的提问来源于stack exchange,提问作者Emil
相关产品推荐
相关产品推荐

