如何在 OpenAI 函数调用中使用数组?

问题描述 投票:0回答:1

我正在使用 OpenAI 的函数调用从预设的项目列表中获取字符串列表。我希望 OpenAI 返回仅属于预设列表的值,而不返回其他值。 JSON schema 指出我应该使用

{"items": false}
并包含一个“prefixItems”对象。然而,OpenAI 的回应是这样的:

400 {'error':{'message':[“函数'get_something_work_lated_that_is_redacted'的模式无效:在上下文=('properties','things')中,数组模式项不是对象”],'type':' invalid_request_error','param':无,'code':无}}

我尝试了下面提到的不同方法,但仍然没有效果。

这是代码:

functions = [
        {
            "name": "get_something_work_related_that_is_redacted",
            "description": "Blah blah blah blah. The output must be from the enum of things given only and not self-generated.",
            "parameters": {
                "type": "object",
                "properties": {
                    "things": {
                        "type": "array",
                        "items": {
                            "type": "string",
                            "enum": [
                                "A",
                                "B",
                                "C",
                                "D",
                                "E",
                            ]
                        },
                    }
                },
                "required": ["datasets"]
            }
        }
    ]

有时我会得到[“A”,“C”]或基于其他参数的东西。但有时我会得到[“A”,“F”]。有时,我什至得到[“G”,“H”]

有人可以帮我解决这个问题吗?

python jsonschema openai-api json-schema-validator
1个回答
0
投票

我发现没有办法强制在函数中使用枚举。该模型有一半的时间只会做它想做的事。您唯一能做的就是验证响应。

我还发现,当您使用数组类型时,它会根据需要获取所有数组参数。那个

对于无效的模式,试试这个:

functions = [
 {
  "name": "get_something_work_related_that_is_redacted",
  "description": "Blah blah blah blah. The output must be from the enum of things given only and not self-generated.",
  "parameters": {
    "type": "object",
    "properties": {
      "things": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "letters": {
              "type": "string",
              "enum": ["A", "B", "C", "D", "E"]
            }
          }
        }
      }
    },
    "required": ["datasets"]
  }
 }
]
© www.soinside.com 2019 - 2024. All rights reserved.