Function Calling学习

Function Calling第一篇

  • Agent:AI 主动提要求
  • Function Calling:AI 要求执行某个函数
  • 场景举例:明天上班是否要带伞?AI反过来问你,明天天气怎么样?
    在这里插入图片描述

Function Calling 的基本流程

在这里插入图片描述

Function Calling 完整的官方接口文档:https://platform.openai.com/docs/guides/function-calling
接口里的 tools,最初版本叫 functions

定义函数tools

# 定义function
def get_completion(messages, model="gpt-3.5-turbo"):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0,  # 模型输出的随机性,0 表示随机性最小
        tools=[{  # 用 JSON 描述函数。可以定义多个。由大模型决定调用谁。也可能都不调用
            "type": "function",
            "function": {
                "name": "sum",
                "description": "只能用来计算加法,计算一组数的和",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "numbers": {
                            "type": "array",
                            "items": {
                                "type": "number"
                            }
                        }
                    }
                }
            }
        }],
    )
    return response.choices[0].message

执行sum函数

方法中的get_completion方法可以查看之前的文章prompt工程那篇

#prompt = "Tell me the sum of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10."
prompt = "桌上有 2 个苹果,四个桃子和 3 本书,一共有几个水果?"
#prompt = "1+2+3...+99+100"
#prompt = "1024 乘以 1024 是多少?"   # Tools 里没有定义乘法,会怎样?
#prompt = "太阳从哪边升起?"           # 不需要算加法,会怎样?

messages = [
    {"role": "system", "content": "你是一个数学家"},
    {"role": "user", "content": prompt}
]
response = get_completion(messages)

# 把大模型的回复加入到对话历史中
messages.append(response)

print("=====GPT回复=====")
print_json(response)

# 如果返回的是函数调用结果,则打印出来
if (response.tool_calls is not None):
    # 是否要调用 sum
    tool_call = response.tool_calls[0]
    if (tool_call.function.name == "sum"):
        # 调用 sum
        args = json.loads(tool_call.function.arguments)
        result = sum(args["numbers"])
        print("=====函数返回=====")
        print(result)

        # 把函数调用结果加入到对话历史中
        messages.append(
            {
                "tool_call_id": tool_call.id,  # 用于标识函数调用的 ID
                "role": "tool",
                "name": "sum",
                "content": str(result)  # 数值 result 必须转成字符串
            }
        )

        # 再次调用大模型
        print("=====最终回复=====")
        print(get_completion(messages).content)

划重点:

  1. OpenAI并不会主动调用函数,只会根据prompt中提供的函数判断是否要调用函数
  2. Function Calling 中的函数与参数的描述也是一种 Prompt
  3. 这种 Prompt 也需要调优,否则会影响函数的召回、参数的准确性,甚至让 GPT 产生幻觉

发现问题:

大家可以试一下问他1024乘以1024等于多次,会发现OpenAI将乘以也识别成了sum的函数,得出的结果是2048。我尝试了很多种prompt,都没能解决这个问题。最终我又创建了一个tools用来计算乘法,这时候OpenAI就能识别乘法了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值