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

相关推荐
Zonda要好好学习14 分钟前
Python入门Day4
java·网络·python
保持学习ing1 小时前
AI--提升效率、驱动创新的核心引擎
低代码·自动化·ai编程
用户3521802454751 小时前
MCP极简入门:@modelcontextprotocol/inspector 如何使用
ai编程·mcp
小龙在山东1 小时前
Python 包管理工具 uv
windows·python·uv
weixin_307779131 小时前
批量OCR的GitHub项目
python·github·ocr
孤狼warrior2 小时前
灰色预测模型
人工智能·python·算法·数学建模
神仙别闹2 小时前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心3 小时前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金3 小时前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP3 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python