设定 functions

functions

设定可以被openai识别的 functions (上一个页面传入的参数)

const functions = [
    {
        name: 'getTravelProductList',
        description: 'Get a list of travel products based on the given location',
        parameters: {
            type: 'object',
            properties: {
                keyword: {
                    type: 'string',
                    description: 'The destination keyword, e.g. Shanghai, China',
                },
                travelDays: {
                    type: 'array',
                    items: {
                        type: 'number',
                    },
                    description: 'The number of days of the trip, e.g. [3, 5, 7]',
                },
                sortType: {
                    type: 'string',
                    enum: ['priceDesc', 'priceAsc', 'ratingDesc', 'default', 'sales'],
                    description: 'The sort type of product list show, e.g. priceDesc',
                },
            },
            required: ['keyword'],
        },
    },
]

这里我们需要描述清楚 function 的名称,以及入参类型和说明,这样才能正确地被识别并且从prompt中提取到合理的参数。

至于这里的 getTravelProductList 实际的内容:

在调用 openai api 时,我们需要判断当前返回的结果是否需要走自己的 function,openai 会在返回的内容里面出现 function_call

当返回的内容中有 function_call, 并且命中之前已有的 function 时,就可以走自己的方法去获取数据然后二次传给 openai 做语言整合。

最后更新于

这有帮助吗?