作用:
Function Calling可以根据用户的输入自行判断何时需要调用哪些函数,并且可以根据目标函数的描述生成符合要求的请求参数。开发人员可以使用函数调用能力,通过GPT实现:
在进行自然语言交流时,通过调用外部工具回答问题(类似于ChatGPT插件);
将自然语言转换为调用API时使用的参数,或者查询数据库时使用的条件;
假设我们要创建一个具备查询航班功能的聊天机器人。我们定义如下两个外部函数供模型选择调用:
-
查询两地之间某日航班号函数:get_flight_number(departure: str, destination: str, date: str)
-
查询航班号以及日期查询票价函数:get_ticket_price(flight_number: str, date: str)
一、定义本地函数
为了向模型描述外部函数库,需要向 tools 字段传入可以调用的函数列表。参数如下表:
| 参数名称 | 类型 | 是否必填 | 参数说明 |
|---|---|---|---|
| type | String | 是 | 设置为function |
| function | Object | 是 | |
| name | String | 是 | 函数名称 |
| description | String | 是 | 用于描述函数功能。模型会根据这段描述决定函数调用方式 |
| parameters | Object | 是 | parameters Object是数所接受的参数。若调用函数时不需要传入参数,省略该参数即可 |
代码
tools = [
{
"type": "function",
"function": {
"name": "get_flight_number",
"description": "根据始发地、目的地和日期,查询对应日期的航班号",
"parameters": {
"type": "object",
"properties": {
"departure": {
"description": "出发地",
"type": "string"
},
"destination": {
"description": "目的地",
"type": "string"
},
"date": {
"description": "日期",
"type": "string",
}
},
"required": [ "departure", "destination", "date" ]
},
}
},
{
"type": "function",
"function": {
"name": "get_ticket_price",
"description": "查询某航班在某日的票价",
"parameters": {
"type": "object",
"properties": {
"flight_number": {
"description": "航班号",
"type": "string"
},
"date": {
"description"

&spm=1001.2101.3001.5002&articleId=138127686&d=1&t=3&u=cb40f870ad9f4a28a9bbfe4ee3814799)
223

被折叠的 条评论
为什么被折叠?



