当前位置:   article > 正文

【大模型应用开发极简入门】agent之前:Function Calling:将自然语言转换为API调用或数据库查询_function calling chatgpt

function calling chatgpt

一. 介绍

OpenAI可以根据用户的要求输出一个符合用户要求的入参值。然后用户拿到入参值之后,可以调用函数执行。那如何按照用户的要求,生成指定格式,并符合入参值逻辑?

可以使用Function Calling将自然语言转换为API调用或数据库查询

 

最新的模型(gpt-4o、gpt-4-turbo 和 gpt-3.5-turbo)已经训练成能够检测何时应该调用函数(取决于输入),并以比以前的模型更接近被调用函数的参数返回 JSON。

 
注意:

尽管具备了这种能力,但也伴随着潜在的风险。我们强烈建议在代表用户采取影响世界的行动之前,构建用户确认流程(发送电子邮件、在网上发布内容、进行购买等)。

 

二. 常见用例与Function Calling调用逻辑

函数调用可让您更可靠地从模型中获取结构化数据。例如,您可以:

  1. 创建调用外部 API 来回答问题的助手;
  2. 将自然语言转换为 API 调用 例如,将“谁是我的顶级客户?”转换为get_customers(min_revenue: int, created_before: string, limit: int)并调用您的内部 API。
  3. 从文本中提取结构化数据:例如定义一个名为的函数extract_data(name: string, birthday: string)等等。

 

函数调用的基本步骤顺序如下:

  1. 定义函数调用规范,调用openAI返回函数调用参数,以json的形式返回
  2. 解析返回的调用参数获取入参并调用函数;
  3. 将函数响应作为新消息附加到模型中,并让模型将结果总结回用户。

 

三. 调用细节

1. 调用行为:tool_choice

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
    )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

 

2. 调用规定:functions

函数定义需要作为函数对象数组传递。下表列出了函数对象的详细信息。

字段名称类型描述
namestring(必填)函数名
descriptionstring函数描述
parametersobject函数所需的参数,这些参数将从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"],
        },
    }
]


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

 

四. 实战:查询公司相关产品

假设我们有一个包含公司产品相关信息的数据库,现要求查询少于2.00的top2的产品。

还是按照上面描述的调用逻辑来说明如下代码逻辑:

  1. 定义函数调用规范:functions,根据用户问题user_question,调用openAI返回生成的参数;
  2. 解析返回的调用参数:response_message,调用函数:find_product1,生成结果products;
  3. 将函数响应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'])

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

 
看下调用效果:

如下是:函数规范+用户问题产生的结果。看到产生了一条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}"
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

 

执行完定义好的外部(相对于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

  • 1
  • 2
  • 3
  • 4
  • 5

 

这个简单的例子演示了如何利用函数来构建一个解决方案,使最终用户能够以自然语言与数据库(例子中未实现)进行交互。你可以使用函数定义将模型限制为按照你希望的方式进行回答,并将其响应集成到应用程序中。

 

我们也可以从文本中提取结构化数据,并通过调用外部工具来创建聊天机器人,而无须创建复杂的提示词以确保模型以特定的格式回答可以由代码解析的问题。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/988889
推荐阅读
相关标签
  

闽ICP备14008679号