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就能识别乘法了。

相关推荐
2601_962300812 分钟前
用PyTorch Lightning简化空间分析:Python和机器学习的力量
python·深度学习·机器学习·空间分析·pytorchlightning
全栈技术负责人14 分钟前
AI 效能工具建设:Agent 可观测性技能技术方案与实践
前端·ai·ai编程
jayson.h33 分钟前
python-可视化提取表格-通用
开发语言·python
零依赖极客1 小时前
《30 天手搓 ARM 架构零依赖纯 C 推理引擎》课程介绍与大纲
c语言·开发语言·arm开发·人工智能·嵌入式硬件·ai编程
悟天特斯1 小时前
AI驱动的楼宇节能:从粗放管控到精准降碳的实践路径
开发语言·人工智能·python·物联网
名字还没想好☜1 小时前
Python 用 tempfile 安全创建临时文件:NamedTemporaryFile、TemporaryDirectory 与别自己拼 /tmp 的坑
开发语言·后端·python·安全·编程语言
Behaviour2 小时前
DeepSeek V4.1 Flash 发布开源,Harness v0.1.5同日适配
人工智能·语言模型·开源·aigc·ai编程
北城bot2 小时前
把 Python 脚本打包成 exe
python
Bigger2 小时前
我把苹果发布会里的折叠屏,真的用网页做出来了
前端·ai编程·交互设计
孟健2 小时前
实测DeepSeek V4.1 Flash:调用费不到1元拿到补丁
ai编程