OpenAI ChatGPT-4开发笔记2024-04:Chat之Tool之2:multiple functions

从程序员到ai Expert

  • [1 设定目标](#1 设定目标)
  • [2 自定义function,3个](#2 自定义function,3个)
  • [3 接口。自定义function--->ChatGPT](#3 接口。自定义function--->ChatGPT)
  • [4 define function to call ChatGPT](#4 define function to call ChatGPT)
  • [5 发起首次请求,告诉gpt要做什么,已经有哪些函数可以调动](#5 发起首次请求,告诉gpt要做什么,已经有哪些函数可以调动)
  • [6 大结局](#6 大结局)
  • [7 参考资料](#7 参考资料)

上一篇解决了调用一个函数的问题。这一篇扩展为调用3个。n个自行脑补。

1 设定目标

python 复制代码
#1.设定目标
what_i_want_to_know = [{"role": "user", "content": f"汇总3个function的aiXpert的结果"}]

2 自定义function,3个

python 复制代码
#2.自定义function,3个
def search_baidu(keyword):
    return f"{keyword}是一个技术博主"

def search_google(keyword):
    return f"{keyword}是一个后端工程师"

def search_bing(keyword):
    return f"{keyword}是一个Python爱好者"

3 接口。自定义function--->ChatGPT

采用Tool的标准写法:

python 复制代码
tools = [
    {
        "type": "function",
        "function": {
            "name": "search_baidu",
            "parameters": {
                "type": "object",
                "properties": {
                    "keyword": {
                        "type": "string",
                    }
                },
                "required": ["keyword"],
            },
        }
    },    
    {
        "type": "function",
        "function": {
            "name": "search_google",
            "parameters": {
                "type": "object",
                "properties": {
                    "keyword": {
                        "type": "string",
                    }
                },
                "required": ["keyword"],
            },
        }
    },        
    {
        "type": "function",
        "function": {
            "name": "search_bing",
            "parameters": {
                "type": "object",
                "properties": {
                    "keyword": {
                        "type": "string",
                    }
                },
                "required": ["keyword"],
            },
        }
    }
]

available_functions = { "search_baidu": search_baidu, "search_google": search_google, "search_bing": search_bing } 

4 define function to call ChatGPT

python 复制代码
def run_chat(messages):
    response = openai.chat.completions.create(
        model   ="gpt-3.5-turbo-1106",
        messages=messages,
        tools   =tools,
        tool_choice="auto", 
    )
    return response.choices[0].message

5 发起首次请求,告诉gpt要做什么,已经有哪些函数可以调动

python 复制代码
first_response = run_chat(what_i_want_to_know)
tool_calls = first_response.tool_calls

6 大结局

python 复制代码
# 检查是否需要调用函数
if tool_calls:
        # 解析所有需要调用的函数及参数
        what_i_want_to_know.append(first_response)  # 注意这里要将openai的回复也拼接到消息列表里
        # 将所有函数调用的结果拼接到消息列表里
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(**function_args)
            what_i_want_to_know.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            ) 
        print(run_chat(what_i_want_to_know))

7 参考资料

  1. OpenAI的多函数调用(Multiple Function Calling)简介
  2. OpenAI API
相关推荐
金融小师妹35 分钟前
多因子关联模型:黄金与铜价同步回落背后的金融变量及PPI传导分析
大数据·人工智能·金融
ZhengEnCi41 分钟前
中美AI竞赛之外其他国家在干什么:算力5%的欧洲、买卡的沙特与做场景的日韩
人工智能·gpu·nvidia
对你无可奈何8 小时前
给 ComfyUI 做了一个手机助手
人工智能
feasibility.10 小时前
英伟达把物理AI的3D模型“应用商店”开放了:2575个SimReady仿真资产,总计约472GB,可让agent装配机器人训练场景
人工智能·3d·机器人·仿真·建模·具身智能·英伟达
智联视频超融合平台10 小时前
消防宣传月过半,“AI+消防“给视频平台换了张考卷
人工智能·边缘计算·视频监控·安防·视频联网
冬奇Lab11 小时前
一天一个开源项目(第262篇):AG-UI —— 把 AI Agent 接进前端的开放协议,和 MCP、A2A 并列的第三块拼图
人工智能·开源
罗西的思考12 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
RobinDevNotes13 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
stormzhangV13 小时前
Coding Agent,通往 AGI 的重要一环
人工智能·agent·ai编程
Bowen_J14 小时前
Apple Foundation Models:Apple AI核心框架
人工智能·ai编程·apple