智谱AI函数调用(Function calling)实测/ ai #32

zhipu.jpg

https://open.bigmodel.cn/dev/howuse/functioncall

智谱AI算是国内知名的大模型了,是紧跟时代潮流嘀。在OpenAI推出Function calling后不久,也推出了自己的函数调用(Function calling),实测下,效果还不错。以下,是实测代码和结果,代码为python版本。有些详细的介绍可以参考《函数调用(Function calling)详解》

pip install zhipuai

from zhipuai import ZhipuAI
import json


client = ZhipuAI(api_key="xxx")

def query_train_info(date, departure , destination):
    #此外是调用的API,这里做测试时直接给出结果
    print(6689, "query_train_info",date, departure, destination)
    return "北京-广州-9966"

def parse_function_call(model_response,messages):
    # 处理函数调用结果,根据模型返回参数,调用对应的函数。
    # 调用函数返回结果后构造tool message,再次调用模型,将函数结果输入模型
    # 模型会将函数调用结果以自然语言格式返回给用户。
    print(556, model_response.choices[0].message)
    if model_response.choices[0].message.tool_calls:
        tool_call = model_response.choices[0].message.tool_calls[0]
        args = tool_call.function.arguments
        function_result = {}
        if tool_call.function.name == "query_train_info":
            function_result = query_train_info(**json.loads(args))
        messages.append({
            "role": "tool",
            "content": f"{json.dumps(function_result)}",
            "tool_call_id":tool_call.id
        })
        response = client.chat.completions.create(
            model="glm-4",
            messages=messages
        )
        print(666, response.choices[0].message)
        # messages.append(response.choices[0].message.model_dump())


def test_func_call():
    messages = []   
    messages.append({"role": "system", "content": "不要假设或猜测传入函数的参数值。如果用户的描述不明确,请要求用户提供必要信息"})
    messages.append({"role": "user", "content": "帮我查询1月23日,北京到广州的航班"})
    tools = [
        {
            "type": "function",
            "function": {
                "name": "query_train_info",
                "description": "根据始发地、目的地和日期,查询对应日期的航班号",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "departure": {
                            "description": "出发地",
                            "type": "string"
                        },
                        "destination": {
                            "description": "目的地",
                            "type": "string"
                        },
                        "date": {
                            "description": "日期",
                            "type": "string",
                        }
                    },
                    "required": [ "departure", "destination", "date" ]
                },
            }
        }
    ]
     
    response = client.chat.completions.create(
        model="glm-4",
        messages=messages,
        tools=tools,
    )
    print(112, response.choices[0].message)
    messages.append(response.choices[0].message.model_dump())
    parse_function_call(response,messages)

if __name__ == "__main__":
    test_func_call()   

测试结果:根据您提供的信息,我查询到了1月23日从北京到广州的航班信息,航班号为“北京-广州-9966”。请注意,这是一个航班号,具体的航班时间、航空公司和其他详细信息需要您通过航空公司或航班查询服务进一步确认。如果您需要更多帮助,请告知我。 一套流程下来还是蛮顺利的,智谱AI也算是争了口气,没掉链子!

Sort:  

Congratulations @lemooljiang! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 47000 upvotes.
Your next target is to reach 48000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP