更新时间:2024-12-16 GMT+08:00
分享

通过Function Calling扩展大语言模型对外部环境的理解

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

操作步骤

  1. 设置Maas的api key和模型服务地址。
    import requests
    from openai import OpenAI
    client = OpenAI(
        api_key="您的 APIKEY", # 从MaaS控制台鉴权管理处获取。
        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)

相关文档