Function calling
所有文档
menu

千帆大模型服务与开发平台ModelBuilder

Function calling

能力介绍

Function call是能将大模型与外部工具或代码相连的功能,可通过该功能增强大模型在实时性、数据计算等应用场景的推理效果或进行其他外部操作,包括信息检索、数据库操作、图谱搜索与处理等工具调用场景。

tools是模型服务API中的可选参数,用于向模型提供函数定义。通过此参数,模型能够生成符合用户所提供规范的函数参数。请注意,模型服务API实际上并不会执行任何函数调用,仅返回是否调用函数,以及调用的函数名与调用函数所需要的参数。开发者可以利用模型输出的参数在系统中进一步执行函数调用。

支持模型范围

  • ernie-4.0-turbo-128k
  • ernie-4.0-turbo-8k
  • ernie-3.5-128k
  • ernie-3.5-8k
  • ernie-speed-pro-128k
  • ernie-lite-pro-128k
  • qwq-32b
  • deepseek-v3

快速开始

调用步骤说明

  1. 使用JSON Schema格式定义函数;
  2. 通过tools参数将定义好的函数提交给支持function call的大模型,可一次提交多个函数;
  3. 大模型将根据当前聊天上下文,决定使用哪个函数,也可能不使用函数;
  4. 若大模型决定使用函数,大模型会将调用函数所需要的参数和信息通过JSON格式返回;
  5. 使用大模型输出的参数,执行对应的函数,并将此函数执行结果提交给大模型;
  6. 大模型将根据函数执行结果,给予用户回复。

SDK调用示例

from openai import OpenAI

client = OpenAI(
    api_key=api_key  ## 
    base_url="https://qianfan.baidubce.com/v2",  ## 
)

tools = [
  {
    "type": "function",
    "function": {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA",
          },
          "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["location"],
      },
    }
  }
]
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
completion = client.chat.completions.create(
  model="ernie-3.5-8k",
  messages=messages,
  tools=tools,
  tool_choice="auto"
)

print(completion)

典型案例

查询天气

请求示例
curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer bce-v3/ALTAK-IpGwCWpMaKK9kfz44OK2A/' \
--header 'Cookie: BAIDUID=6FC98A94EA43253A9B06FB79783DA833:FG=1' \
--data '{
    "model": "ernie-lite-pro-128k",
    "messages": [
        {
            "role": "user",
            "content": "查一下上海和北京现在的天气"
        }
    ],
    "tools": [{
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "天气查询工具",
            "parameters": {
                "properties": {
                    "location": {
                        "description": "地理位置,精确到区县级别",
                        "type": "string"
                    },
                    "time": {
                        "description": "时间,格式为YYYY-MM-DD",
                        "type": "string"
                    }
                },
                "type": "object"
            }
        }

    }],
    "stream": false,
    "stream_options": {
        "include_usage": true
    },
    "tool_choice" : "auto",
    "tool_options" : {"thoughts_output" : true}
}'
返回示例
{
    "id": "as-7jqstcp9ce",
    "object": "chat.completion",
    "created": 1732885634,
    "model": "ernie-lite-pro-128k",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "",
                "tool_calls": [
                    {
                        "id": "call_tmvd94n0t8",
                        "type": "function",
                        "function": {
                            "name": "get_current_weather",
                            "arguments": "{\"location\":\"上海\"}"
                        }
                    }
                ]
            },
            "finish_reason": "tool_calls",
            "flag": 0
        }
    ],
    "usage": {
        "prompt_tokens": 5,
        "completion_tokens": 49,
        "total_tokens": 54
    }
}

询问车票

请求示例
curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer bce-v3/ALTAK-IpGwCWpMaKK9kfz44OK2A/' \
--header 'Cookie: BAIDUID=6FC98A94EA43253A9B06FB79783DA833:FG=1' \
--data '{
    "model": "ernie-3.5-8k",
    "messages": [
        {
            "role": "system",
            "content": "你是一个智能助手"
        },
        {
            "role": "user",
            "content": "你能帮我查询2024年1月1日从北京南站到上海的火车票吗?"
        }
    ],
    "stream": false,
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "query_train_info",
                "description": "根据用户提供的信息,查询对应的车次",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "departure": {
                            "type": "string",
                            "description": "出发城市或车站"
                        },
                        "destination": {
                            "type": "string",
                            "description": "目的地城市或车站"
                        },
                        "date": {
                            "type": "string",
                            "description": "要查询的车次日期"
                        }
                    },
                    "required": [
                        "departure",
                        "destination",
                        "date"
                    ]
                }
            }
        }
    ]
}'
返回示例
{
    "id": "as-ubsiihj8d5",
    "object": "chat.completion",
    "created": 1732887366,
    "model": "ernie-3.5-8k",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "",
                "tool_calls": [
                    {
                        "id": "19d8e571660dd000",
                        "type": "function",
                        "function": {
                            "name": "query_train_info",
                            "arguments": "{\"departure\": \"北京南站\", \"destination\": \"上海\", \"date\": \"2024-01-01\"}"
                        }
                    }
                ]
            },
            "finish_reason": "tool_calls",
            "flag": 0
        }
    ],
    "usage": {
        "prompt_tokens": 19,
        "completion_tokens": 33,
        "total_tokens": 52
    }
}