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

python 复制代码
# 定义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工程那篇

python 复制代码
#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就能识别乘法了。

相关推荐
m***记1 分钟前
Python 数据分析入门:Pandas vs NumPy 全方位对比
python·数据分析·pandas
MYX_3099 分钟前
第七章 完整的模型训练
pytorch·python·深度学习·学习
云起SAAS12 分钟前
ai周公解梦抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·ai周公解梦
新子y26 分钟前
【小白笔记】岛屿数量
笔记·python
CLubiy31 分钟前
【研究生随笔】Pytorch中的线性代数
pytorch·python·深度学习·线性代数·机器学习
reasonsummer34 分钟前
【办公类-115-02】20251018信息员每周通讯上传之文字稿整理(PDF转docx没有成功)
python·pdf
材料科学研究41 分钟前
深度学习物理神经网络(PINN)!
python·深度学习·神经网络·pinn
兰文彬1 小时前
Pytorch环境安装指南与建议
人工智能·pytorch·python
哦你看看1 小时前
学习Python 03
开发语言·windows·python
后端小张1 小时前
[AI 学习日记] 深入解析MCP —— 从基础配置到高级应用指南
人工智能·python·ai·开源协议·mcp·智能化转型·通用协议