j9九游会登录/ 大模型即服务平台 maas/ 用户指南/ / 通过function calling扩展大语言模型对外部环境的理解
更新时间:2026-01-20 gmt 08:00

通过function calling扩展大语言模型对外部环境的理解-j9九游会登录

本示例将展示如何定义一个获取送货日期的函数,并通过llm来调用外部api来获取外部信息。

操作步骤

  1. 设置maas的api key和模型服务地址url。
    import requests
    from openai import openai
    client = openai(
        api_key="您的 apikey", # 从api key管理页面获取。
        base_url="https://infer-modelarts.cn-east-4.myhuaweicloud.com/v1/infers/xxxxxx/v1"  # maas模型服务的基础url,不包含尾部的chat/completions部分。
    )
  2. 自定义一个获取送货日期的函数。
    from datetime import datetime
    def get_delivery_date(order_id: int) -> datetime:
        if order_id == 1:
            return datetime.strptime("2024-09-01 18:30", "%y-%m-%d %h:%m")
        elif order_id == 2:
            return datetime.strptime("2024-10-20 12:00", "%y-%m-%d %h:%m")
        else:
            return f"cannot find order_id {order_id}"
    # example usage
    print(get_delivery_date(1))
  3. 编写函数的描述。
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_delivery_date",
                "description": "get the delivery date for a customer's order. call this whenever you need to know the delivery date, for example when a customer asks 'where is my package'",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "order_id": {
                            "type": "string",
                            "description": "the customer's order id.",
                        },
                    },
                    "required": ["order_id"],
                    "additionalproperties": false,
                },
            }
        }
    ]
  4. 与llm进行对话。
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_delivery_date",
                "description": "get the delivery date for a customer's order. call this whenever you need to know the delivery date, for example when a customer asks 'where is my package'",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "order_id": {
                            "type": "string",
                            "description": "the customer's order id.",
                        },
                    },
                    "required": ["order_id"],
                    "additionalproperties": false,
                },
            }
        }
    ]
     
    messages = [
        {"role": "system", "content": "you are a helpful customer support assistant. use the supplied tools to assist the user."},
        {"role": "user", "content": "hi, can you tell me the delivery date for my order?"}
    ]
    messages.append({"role": "assistant", "content": "hi there! i can help with that. can you please provide your order id?"})
    messages.append({"role": "user", "content": "i think it is 1"})
     
    response = client.chat.completions.create(
        model="qwen2.5-72b-32k",
        messages=messages,
        tools=tools,
    )
     
    print(response)

常见问题

在modelarts studio(maas) 创建api key后需要等待多久才能生效?

maas api key在创建后不会立即生效,通常需要等待几分钟才能生效。

相关文档

网站地图