OpenAI 新的Function Calling 网页链接 的一种用法:让它返回格式化的JSON数据。
以前GPT 3.5让它稳定的输出JSON格式是有点麻烦的,首先是Prompt比较长,可能还要Few-Shot,现在有了Function Calling,就可以要求它在返回结果之前去调用指定的Function,将结果作为参数传给Function,那么它就能返回一个标准的JSON格式,然后你实际上可以不去调用函数,只需要使用这个JSON数据就足够了。
举一个应用场景,字幕翻译,比如我解析完字幕,会把字幕拆分成一个个的字符串数组,然后把字符串数组作为输入传入GPT帮我翻译,但我希望翻译后是一个新的JSON格式的数组。
现在借助Function Calling,我们可以构造一个假的函数,比如名字叫“formatResult”,然后这个函数的输入是一个字符串数组。
按照Function Calling的API生成Function的描述,说明调用这个函数使参数必须是一个字符串数组,并且必须传入。
{
"name": "formatTranslation",
"description": "Format the result of the translation",
"parameters": {
"type": "object",
"properties": {
"translated_subtitle": {
"type": "array",
"description": "The translated subtitle",
"items": {
"type": "string",
"description": "A translated subtitle item"
}
}
},
"required": [
"translated"
]
}
}
在Prompt里面加一句话:“Call formatTranslation function before returning the result.”
然后它的返回结果就会如截图所示,是一个JSON格式,包含翻译好的JSON数组。
但这种方式也有一些缺陷:
1. 它并非总是有效,参考图2,有时候会莫名其妙的名为“python”的函数,给你生成一段Python代码,所以你的代码中要有纠错机制,如果没有调用你指定的函数需要重试。
2. 适合单次调用,不适合类似于Chat要保留历史会话的那种情况,因为并没有真的调用这个函数,只是借助Function Calling让GPT返回指定的JSON格式。
附上完整的示例请求消息:
——–
{
"model": "gpt-3.5-turbo-0613",
"messages": [
{
"role": "system",
"content": "You are a highly sophisticated translation AI, specifically trained to translate subtitles from English into Simplified Chinese. Your responsibility is to output the translated text based on the given English input. Call formatTranslation function before returning the result."
},
{
"role": "user",
"content": "[\"In the case of a RNN decoder it does it in steps, decoding the output one token at a\",\"time using the current state and what has been decoded so far.\",\"Okay, now that we have a high level understanding of the encoder-decoder architecture, how do\",\"we train it?\",\"That’s the training phase.\",\"To train a model, you need a dataset, that is a collection of input/ouput pairs that\",\"you want your model to imitate.\",\"You can then feed this dataset to the model, which will correct its weights during training\",\"on the basis of the error it produces on a given input in the dataset.\",\"This error is essentially the difference between what the neural network generates given an\",\"input sequence and the true output sequence you have in your dataset.\"]"
}
],
"functions": [
{
"name": "formatTranslation",
"description": "Format the result of the translation",
"parameters": {
"type": "object",
"properties": {
"translated_subtitle": {
"type": "array",
"description": "The translated subtitle",
"items": {
"type": "string",
"description": "A translated subtitle item"
}
}
},
"required": [
"translated"
]
}
}
]
}
——–
更新:谢谢@kkakakaa 提醒,其实可以直接使用 function_call 参数直接调用,而不需要使用Prompt。
参考图三,格式如下:
“function_call”:{“name”:”formatTranslation”},
来源:宝玉XP