调试文心大模型或chatgpt的function Calling函数应用场景

沉默了一段时间,最近都在研究AI大模型的产品落地应用,我觉得这个function calling出来后,对目前辅助办公有革命性的改变,可以它来做什么呢?我们先来调试看看,chatgpt和文心大模型的ERNIE Bot支持这个,chatgpt申请有一定门槛,先以文心的为例,账号的access_token申请我就不说了,直接去飞桨飞桨AI Studio星河社区-人工智能学习与实训注册申请。安装那些自己直接看官方文档,上代码:

#coding:utf-8
import erniebot,json

#记载模型
models = erniebot.Model.list()

#参数
erniebot.api_type = "aistudio"
erniebot.access_token = "你的token码"


messages = [
    {
        'role': 'user',
        'content': "搜索用户李大锤的信息",
    },
]
functions = [
    {
        'name': 'get_current_temperature',
        'description': "获取指定城市的气温",
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': "城市名称",
                },
                'time': {
                    'type': 'string',
                    'description': "时间",
                },
                'unit': {
                    'type': 'string',
                    'enum': [
                        '摄氏度',
                        '华氏度',
                    ],
                },
            },
            'required': [
                'location',
                'time',
                'unit',
            ],
        },
        'responses': {
            'type': 'object',
            'properties': {
                'temperature': {
                    'type': 'integer',
                    'description': "城市气温",
                },
                'time': {
                    'type': 'string',
                    'description': "时间",
                },
                'unit': {
                    'type': 'string',
                    'enum': [
                        '摄氏度',
                        '华氏度',
                    ],
                },
            },
        },
    },
{
        'name': 'get_user_info',
        'description': "获取用户信息",
        'parameters': {
            'type': 'object',
            'properties': {
                'username': {
                    'type': 'string',
                    'description': "用户名",
                },
            },
            'required': [
                'username',
            ],
        },
        'responses': {
            'type': 'object',
            'properties': {
                'username': {
                    'type': 'string',
                    'description': "用户名",
                },
                'birthday': {
                    'type': 'string',
                    'description': "用户生日",
                },
                'hobby': {
                    'type': 'string',
                    'description': "用户爱好",
                },
            },
        },
    },
]
response = erniebot.ChatCompletion.create(
    model='ernie-bot',
    messages=messages,
    functions=functions,
)
print("one:",response.get_result())

funcitons是一个数组,里面可以定义多组方法类的数据,上面我设置了2个方法,一个是查询天气的,一个是查询用户信息的,我们截取一个片段看看:

{
        'name': 'get_user_info',       #我们自己定义的函数名称
        'description': "获取用户信息",    #描述
        'parameters': {
            'type': 'object',
            'properties': {
                'username': {
                    'type': 'string',
                    'description': "用户名",
                },
            },
            'required': [
                'username',
            ],
        },
        'responses': {
            'type': 'object',
            'properties': {
                'username': {
                    'type': 'string',
                    'description': "用户名",
                },
                'birthday': {
                    'type': 'string',
                    'description': "用户生日",
                },
                'hobby': {
                    'type': 'string',
                    'description': "用户爱好",
                },
            },
        },
    },

这格式是固定的,parameters就是设置的参数,可定义参数的名称,类型,描述,responses就是返回的结果参数,这里主要就是定义好方法类及参数,这是执行的调用写法:

response = erniebot.ChatCompletion.create(
    model='ernie-bot',        
    messages=messages,       #消息
    functions=functions,     #匹配函数
)

执行上面的代码会返回:

大模型会根据你的问题智能匹配是否符合函数的关键词(我估计可能也会存在一定的误差,整体测试还行),匹配上后会返回以上信息,get_user_info是你方法类的名称,arguments里面就是你定义的参数,它智能匹配到了用户名叫李大锤的。

接下来就可以根据返回的信息去调用本地对应的方法函数:

#获取天气
def get_current_temperature(location,time,unit):
    #这里可以自定义执行方法,下面是演示返回结果
    return {"location":location,"temperature": 36, "time": time, "unit": "℃"}

#获取用户信息
def get_user_info(username):
    #这里可以自定义执行方法,下面是演示返回结果
    return {"username": username, "birthday": "1998-08-08", "hobby": "踢球,打游戏,吃火锅"}

#如果匹配成功则执行下面代码
if response.is_function_response:
    function_call = response.get_result()
    name2function = {'get_current_temperature': get_current_temperature,'get_user_info': get_user_info}
    func = name2function[function_call['name']]
    func_name=function_call['name']
    args = json.loads(function_call['arguments'])
    res=""
    #根据name判断调用本地那个方法类
    if func_name=='get_current_temperature':
        res = func(location=args['location'],time=args['time'],unit=args['unit'])
    elif func_name=='get_user_info':
        res = func(username=args['username'])
    print("res:", res)

上面执行一下,看看结果

到这里基本上都走通了,开始的问题:搜索用户李大锤的信息,匹配后就执行了你的get_user_info的方法返回了结果。

通过以上思考,这样的话是不是可以在自己的公司产品或者项目开发一个辅助工具,更有效便捷的帮助用户摆脱一些繁琐复杂的操作流程,这个想象空间我感觉很大。

得到json后我们可以再次通过模型美化一下:

#再次通过Ai执行
    messages.append(
        {
            "role": "assistant",
            "content": None,
            "function_call": function_call,
        }
    )
    messages.append(
        {
            "role": "function",
            "name": function_call["name"],
            "content": json.dumps(res, ensure_ascii=False),
        }
    )
    secendresponse = erniebot.ChatCompletion.create(
        model='ernie-bot',
        messages=messages,
        functions=functions,
    )
    print(secendresponse.result)

来执行看看:

这功能还是挺强大的,它的原理简单的可以理解为关键词匹配,有点类似小爱,小度智能控制的赶脚,然后在加上模型本身的文案编写,图片生成等功能,真的可以让自己的产品得到一个飞一般的提升,然后在想象一下,把它结合到软件工具中,是不是可以智能化的执行系统的命令,智能回复及应答,搜索等等,我目前第一步就是先集成到公司的产品中去,好了,记录就先到这里。

如果有需要的源代码的同学,请关注“小白一起学编程”公众号回复:functioncalling 下载源码

猜你喜欢

转载自blog.csdn.net/jylonger/article/details/134829918