弥合人类与机器之间的差距

实施OpenAI的新API功能-函数

ChatGPT中文站
AI-generated on Leonard.ai

OpenAI的API更新

自然语言处理模型例如ChatGPT在人类语言的复杂性方面表现出色。但是,当涉及到计算机语言的结构化特征时,它们也会遇到挑战。

我们在先前的文章中强调过,使用ChatGPT生成持续有效的JSON输出是繁琐的。

幸运的是,OpenAI最近的更新引入了许多令人兴奋的功能,包括函数-一种内置解决方案,可以确保可靠的JSON输出,可以被我们的软件消耗。

功能被作为参数传递到我们的OpenAI聊天完成调用中,并以对象作为参数。该功能配合最近发布的模型版本gpt-4-0613和gpt-3.5-turbo-0613使用。这些模型已经经过精细调节,以便接收功能并以JSON输出做出回应。

{
"name": "my_function",
"description": "Description of my openai function call.",
"parameters": {
"type": "object",
"properties": {
"my_first_required_property": {
"type": "string",
"description": "A require property of my request"
},
"my_second_optional_property": {
"type": "string",
"description": "An optional property of my request"
},
},
"required": ['my_first_required_property'],
}
}

实际应用

在这个例子中,我们向我们热情的冲浪用户提供每日潮汐详情!我们的流程非常简单,用户将为特定位置的潮汐信息提示我们的应用程序,而不是给用户拖动菜单和工业形式的负担,我们将利用ChatGPT来理解类人的提示。

ChatGPT中文站
AI-generated on Leonard.ai

当我们的用户请求特定位置的潮汐信息时,我们让ChatGPT从他们的句子中提取位置信息。从那里,我们使用ChatGPT的帮助从人类句子中提取位置。现在,我们可以获取该位置的潮汐数据,并将其反馈给ChatGPT以便它给我们提供类似于人的回复,并将其返回给用户。

ChatGPT中文站
Software flow

这个代码

首先,我们构建了我们的OpenAI调用,以从类似人类的句子中提取数据。

openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "user", "content": "What time is the high tide in Nelson today?"},
],
functions=[{
"name": "get_tide_info",
"description": "Get today's tide information.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "Location for tide information (i.e: Wellington)"
},
"high_tide_time": {
"type": "string",
"description": "High tide time"
},
"low_tide_time": {
"type": "string",
"description": "Low tide time"
},
},
"required": ['location'],
}
}]
)

这里我们声明了一个函数,它有三个参数,其中一个必需的参数是 location。现在我们应该接收到一个包含 JSON 输出的响应。

{
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_tide_info",
"arguments": "{ \"location\": \"Nelson\"}"
}
},
"finish_reason": "function_call"
}]
}

通过我们的位置,我们现在可以从潮汐API获取潮汐信息。

const getTideData = () => {
const response = await fetch(`http://someapi/tide?location=${location}`);
return await response.json();
}

console.log(`log: ${getTideData()}`);

// log: "{\"high_tide_time\": \"08:32+12:00\", \"low_tide_time\": \"14:54+12:00\"}"

从这里我们将响应连接到我们的OpenAI呼叫并获得可读的人类响应:

openai.ChatCompletion.create(
model="gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "What time is high tide in Nelson today?"},
{"role": "assistant", "content": null, "function_call": {"name": "get_tide_info", "arguments": "{ \"location\": \"Nelson\"}"}},
{"role": "function", "name": "get_tide_info", "content": "{\"high_tide_time\": \"08:32+12:00\", \"low_tide_time\": \"14:54+12:00\"}"}
],
functions: [
{
"name": "get_tide_info",
"description": "Get the day's tide information.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City for the tide information (i.e: Wellington)"
},
"high_tide_time": {
"type": "string",
"description": "Time of the day's high tide"
},
"low_tide_time": {
"type": "string",
"description": "Time of the day's low tide"
},
},
"required": ['location']
}
}
]
)

并且,完成了!

{
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "The high tide in Nelson today is at 08:32pm",
},
"finish_reason": "stop"
}]
}

我们现在可以通过接收和提供多个 API 调用驱动的人类响应来弥合人类和计算机语言之间的差距。

了解更多关于OpenAI的更新和其他发布的功能,请查看他们的文章。

喜欢这篇文章吗?在Twitter或LinkedIn上关注我们吧。

2023-10-20 17:03:16 AI中文站翻译自原文