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
相关推荐
卡索(CASO)汽车调查1 分钟前
卡索(CASO)汽车调查:数据智能时代,汽车产业竞争格局与战略升维路径探析
大数据·人工智能·汽车·神秘顾客·汽车密采·神秘人·汽车研究
笨鸟笃行5 分钟前
人工智能备考——2.1.1-2.1.5总结
人工智能·学习
晨非辰8 分钟前
【数据结构】排序详解:从快速排序分区逻辑,到携手冒泡排序的算法效率深度评测
运维·数据结构·c++·人工智能·后端·深度学习·排序算法
能来帮帮蒟蒻吗16 分钟前
深度学习(4)—— Pytorch快速上手!从零搭建神经网络
人工智能·pytorch·深度学习
Blossom.11822 分钟前
大模型知识蒸馏实战:从Qwen-72B到Qwen-7B的压缩艺术
大数据·人工智能·python·深度学习·算法·机器学习·pygame
AA陈超24 分钟前
ASC学习笔记0012:查找现有的属性集,如果不存在则断言
笔记·学习
pingao14137837 分钟前
零启动风速+多参数集成:金属超声波传感器的技术突破
人工智能·科技
wshzd1 小时前
LLM之Agent(二十八)|AI音视频转笔记方法揭秘
人工智能·笔记
IT_陈寒1 小时前
Python 3.12新特性实战:5个让你的代码效率翻倍的隐藏技巧!
前端·人工智能·后端
The_Second_Coming1 小时前
Python 学习笔记:基础篇
运维·笔记·python·学习