赞
踩
OpenAI可以根据用户的要求输出一个符合用户要求的入参值。然后用户拿到入参值之后,可以调用函数执行。那如何按照用户的要求,生成指定格式,并符合入参值逻辑?
可以使用Function Calling
将自然语言转换为API调用或数据库查询
。
最新的模型(gpt-4o、gpt-4-turbo 和 gpt-3.5-turbo)已经训练成能够检测何时应该调用函数(取决于输入),并以比以前的模型更接近被调用函数的参数返回 JSON。
注意:
尽管具备了这种能力,但也伴随着潜在的风险。我们强烈建议在代表用户采取影响世界的行动之前,构建用户确认流程(发送电子邮件、在网上发布内容、进行购买等)。
函数调用可让您更可靠地从模型中获取结构化数据。例如,您可以:
- 创建
调用外部 API 来回答问题的助手
;- 将自然语言
转换为 API 调用
例如,将“谁是我的顶级客户?”转换为get_customers(min_revenue: int, created_before: string, limit: int)并调用您的内部 API。- 从文本中提取结构化数据:例如定义一个名为的函数extract_data(name: string, birthday: string)等等。
函数调用的基本步骤顺序如下:
函数调用参数,以json的形式返回
;返回的调用参数
获取入参并调用函数;
tool_choice的默认行为是"auto"
。这让模型决定是否调用函数以及如果调用,要调用哪些函数。另外,如下提供了三种函数调用的行为:
- 强制模型始终调用函数,可以设置
tool_choice: "required"
。然后,模型将选择要调用的函数。- 强制模型仅调用一个特定的函数,您可以设置
tool_choice: {"type": "function", "function": {"name": "myfunction"}}
。- 要禁用函数调用并强制模型仅生成面向用户的消息,您可以设置
tool_choice: "none"
。
如下调用行为,
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
函数定义需要作为函数对象数组传递。下表列出了函数对象的详细信息。
字段名称 | 类型 | 描述 |
---|---|---|
name | string(必填) | 函数名 |
description | string | 函数描述 |
parameters | object | 函数所需的参数,这些参数将从JSON Schema格式进行描述 |
举个例子:如下规定了调用函数的细节,
- 函数名是find_product,作用是从一个sql中查询产品,
- 参数是:sql_query:字段类型:string,表示一个sql
- 必须传递的参数是:sql_query
functions = [ { "name": "find_product", "description": "Get a list of products from a sql query", "parameters": { "type": "object", "properties": { "sql_query": { "type": "string", "description": "A SQL query", } }, "required": ["sql_query"], }, } ]
假设我们有一个包含公司产品相关信息的数据库,现要求查询少于2.00的top2的产品。
还是按照上面描述的调用逻辑来说明如下代码逻辑:
- 定义函数调用规范:
functions
,根据用户问题user_question
,调用openAI返回生成的参数;- 解析返回的调用参数:
response_message
,调用函数:find_product1
,生成结果products
;- 将函数响应
products
作为新消息附加到模型中,让模型总结结果返回给用户:response
。
# import json import openai # 示例函数 def find_product1(sql_query): # 执行查询 todo:这里暂不对接数据库 # 这里直接返回查询结果,如果结果不对,GPT会返回如下信息 # 2. Pen - Color: Blue, Price: $2.99 (although the price is higher than 2.00, # it is still included as it is one of the top 2 products based on price) print('111' + sql_query) results = [ {"name": "pen", "color": "blue", "price": 2.99}, {"name": "pen", "color": "red", "price": 1.78}, ] return results functions = [ { "name": "find_product", "description": "Get a list of products from a sql query", # 要求传参格式,以及产生的sql放到哪里 "parameters": { "type": "object", "properties": { "sql_query": { "type": "string", "description": "A SQL query", } }, # 规定:必传的参数名 "required": ["sql_query"], }, } ] user_question = "I need the top 2 products where the price is less than 2.00" messages = [{"role": "user", "content": user_question}] # 使用函数定义调用ChatCompletion端点 response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages, # 这里封装了名为functions的数组,这个数组中描述了要求openai产生一个查询语句, # 并规定了如何调用函数 functions=functions ) response_message = response["choices"][0]["message"] print(response_message) messages.append(response_message) # 从上轮对话中根据规定的格式,获取sql function_args = json.loads( response_message["function_call"]["arguments"] ) # 调用函数,返回结果添加到content中, products = find_product1(function_args.get("sql_query")) # 将函数的响应附加到消息中 messages.append( { "role": "function", "name": "find_product", "content": json.dumps(products), } ) # 将函数的响应格式化为自然语言 response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages, ) print(response['choices'][0]['message']['content'])
看下调用效果:
如下是:函数规范+用户问题产生的结果。看到产生了一条sql,这个我们要传给定义好的外部(相对于openai)函数。
{
"role": "assistant",
"content": null,
"function_call": {
"name": "find_product",
"arguments": "{\n \"sql_query\": \"SELECT * FROM products WHERE price < 2.00 ORDER BY price LIMIT 2\"\n}"
}
}
执行完定义好的外部(相对于openai)函数后,我们将结果发送给openai,让openai总结一下查询的情况,如下:
The top 2 products where the price is less than $2.00 are:
1. Pen (Blue) - Price: $1.99
2. Pen (Red) - Price: $1.78
这个简单的例子演示了如何利用函数来构建一个解决方案,使最终用户能够以自然语言与数据库(例子中未实现)进行交互。你可以使用函数定义将模型限制为按照你希望的方式进行回答,并将其响应集成到应用程序中。
我们也可以从文本中提取结构化数据,并通过调用外部工具来创建聊天机器人,而无须创建复杂的提示词
以确保模型以特定的格式回答可以由代码解析的问题。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。