智健blog

文心一言大模型-function Calling的应用

"大模型的函数调用"(Large Model Function Calling)有两种含义:

  • 内部函数调用: 这指的是大型模型在其内部运行时执行的函数调用。这些函数调用是模型的一部分,用于处理输入,执行复杂的计算,生成预测等。这些内部函数是模型架构的一部分,对于最终用户通常是不可见的。

  • 外部API调用: 另一种情况是大型模型如 GPT-4 被设计为能够与外部系统交互,比如通过 API 调用执行某些任务。例如,一个大型模型可能被编程为在需要时调用外部数据库查询函数、调用图像处理服务或执行其他外部功能。

第二种情况下,也就是openai六月份开放的一个功能-FunctionCalling,大型模型的函数调用通常是指模型作为其生成过程的一部分调用外部服务或功能的能力。这种能力极大地扩展了模型的用途,使其能够在生成文本、做出决策或执行任务时,集成更广泛的信息和功能。

接下来本文将做个function calling 外部API调用的demo实例,采用的大模型是文心一言

1. 准备工作

安装qianfan

pip install qianfan

前往百度智能云官网https://console.bce.baidu.com/qianfan/chargemanage/list申请大模型app key和secret key

2. 需求概述

实现一个旅游景点推荐以及自动下单的功能

  • 用户给大模型输入:想去北京天安门附近的10公里内的旅游景点
  • 大模型理解输入,获取关键词position:天安门,expect_distance:10000,返回需要调用的api function name:attractionRecommend
  • 用户输入:就去天坛公园吧
  • 大模型理解输入,返回需要调用的api function name:attractionReservation

3. 代码实现

import qianfan
import re
from pprint import pprint
import json
with open('prompt.txt',encoding='utf-8') as f:  
    prompt = f.read()  

def attractionRecommend(arguments):
    return "天坛公园"
def attractionReservation(arguments):
    return "预约成功"
def function_calling(prompt):
    print("user-prompt:"+prompt)
    model = "ERNIE-Bot"
    print('=' * 30,'大模型-',model,' 输出 ', '='*30,"\n")
    response = chat_comp.do(
            model=model, 
            messages=[{
                "role": "user",
                "content": prompt
                }],
            temperature=0.000000001,
            functions=[
                {
                    "name": "attractionRecommend",
                    "description": "景点推荐",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "location": {
                                "type": "string",
                                "description": "地址信息,包括省市、街道、门牌号等"
                                },
                            "expect_distance": {
                                "type": "int",
                                "description": "距离"
                                }
                            },
                        "required": ["location"]
                        },
                    "responses": {
                        "type": "object",
                        "properties": {
                            "price": {
                                "type": "int",
                                "description": "景点距离"
                                },
                            "food": {
                                "type": "string",
                                "description": "景点名称"
                                },
                            },
                        },
                 },
                 {
                        "name": "attractionReservation",
                        "description": "景点介绍预约",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                
                                "attraction": {
                                    "type": "string",
                                    "description": "景点名称"
                                    },
                                },
                            "required": ["attraction"]
                            },
                        "responses": {
                            "type": "object",
                            "properties": {
                                "result": {
                                    "type": "string",
                                    "description": "回答完成"
                                    },
                                }
                            },
                   }
                 ]
            )


    pprint(response)
    return response
chat_comp = qianfan.ChatCompletion(ak="xx", sk="xx")


prompt_list = re.split(r"----", prompt)

for prompt in prompt_list:
    response = function_calling(prompt)
    function_name = response['body']['function_call']['name']
    arguments = response['body']['function_call']['arguments']
    eval(function_name)(arguments)
    
    #拿到response后,解析json,调用自定义的数据表api和下单api
    print("\n")
    print('=' * 30,"大模型响应结束","="*30)



posted @ 2024-01-01 16:16  智健  阅读(1152)  评论(0编辑  收藏  举报