✅什么是Function Call? 因为大模型自身存在着局限性,无法获取一些最新的信息,无法获取私域知识等,那么,我们就可以通过function calling(也称为tool calling)的方式使大模型可以和外部交互,从而访问训练数据之外的数据。
比如我问大模型今天的天气如何,模型肯定是没有这部分数据训练过的,根据前面讲过的大模型的原理,他的输出结果一定是错的。而如果我们提供一个函数(工具)给他,那么就可以通过函数(工具)调用的方式获取今天的天气信息。
后面我们的介绍,为了方便,不管是Function,还是Tool,还是说函数,还是工具,都是一回事儿。
但是一定要注意,函数调用听上去好像是模型自己会调用工具,但其实并不是。在Function Calling中,大模型并不负责函数的调用和执行 ,大模型的作用是根据用户的问题,理解用户的需求,然后根据用户需求确定具体需要调用哪个函数以及函数所需要的参数是什么。
function call过程 如下面这张图,其实是OpenAI给出的函数调用的过程,可以看到,最关键的函数的执行调用,其实是靠开发者来进行的,也是需要借助我们的应用,即你的python代码或者java代码。
所以,根据上面的交互图,我们总结下,通过大模型做Function call的过程是:
向模型发送包含其可调用工具的请求
从模型接收一个工具调用结果(包括具体的工具和参数)
在应用端执行代码,使用工具调用的输入
使用工具输出向模型发起第二次请求,带有工具调用结果
接收模型返回的最终响应(或更多工具调用)
函数定义 想要通过模型做Function Call,就需要定义函数(工具),在Open AI的规范中,定义一个函数需要包含以下内容:
Field
Value
字段
描述
type
固定值:function
name
函数的名称(例如:get_weather )
description
关于何时以及如何使用该函数的详细信息
parameters
定义函数输入参数的 JSON 模式
strict
是否对函数调用执行严格模式
比如官方的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 { "type" : "function" , "name" : "get_weather" , "description" : "Retrieves current weather for the given location." , "parameters" : { "type" : "object" , "properties" : { "location" : { "type" : "string" , "description" : "City and country e.g. Bogotá, Colombia" } , "units" : { "type" : "string" , "enum" : [ "celsius" , "fahrenheit" ] , "description" : "Units the temperature will be returned in." } } , "required" : [ "location" , "units" ] , "additionalProperties" : false } , "strict" : true }
当在函数定义中设置strict: true时,将开启结构化输出功能,可以确保模型为Function Calling生成的参数与在函数定义中提供的JSON Schema完全匹配。(https://openai.com/index/introducing-structured-outputs-in-the-api/ )
向模型发送请求 有了函数定义之后,就可以向模型发送请求了,我们可以看下开源的qwen是如何做的,如何把工具告知模型的。
在huggingface上的开源模型都能看到chat_template,这里面定义了用户和模型之间的输入输出的模板,如Qwen/Qwen3-235B-A22B (https://huggingface.co/Qwen/Qwen3-235B-A22B?chat\_template=default )
大致能看懂一些,在最开始就会判断是否有工具,如果有工具的话,会组装出一些和工具调用有关的prompt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 { %-iftools% } { { -'<|im_start|>system\n' } } { %-ifmessages [ 0 ] .role=='system'% } { { -messages [ 0 ] .content+'\n\n' } } { %-endif% } { { -"# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" } } { %-fortoolintools% } { { -"\n" } } { { -tool|tojson } } { %-endfor% } { { -"\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" } } { %-else% } { %-ifmessages [ 0 ] .role=='system'% } { { -'<|im_start|>system\n'+messages [ 0 ] .content+'<|im_end|>\n' } } { %-endif% } { %-endif% } { %-setns=namespace(multi_step_tool=true , last_query_index=messages|length-1 )% } { %-formessageinmessages [ : : -1 ] % } { %-setindex=(messages|length-1 )-loop.index0% } { %-ifns.multi_step_toolandmessage.role=="user" andmessage.contentisstringandnot(message.content.startswith('<tool_response>')andmessage.content.endswith('</tool_response>'))% } { %-setns.multi_step_tool=false % } { %-setns.last_query_index=index% } { %-endif% } { %-endfor% } { %-formessageinmessages% } { %-ifmessage.contentisstring% } { %-setcontent=message.content% } { %-else% } { %-setcontent=''% } { %-endif% } { %-if(message.role=="user" )or(message.role=="system" andnotloop.first)% } { { -'<|im_start|>'+message.role+'\n'+content+'<|im_end|>'+'\n' } } { %-elifmessage.role=="assistant" % } { %-setreasoning_content=''% } { %-ifmessage.reasoning_contentisstring% } { %-setreasoning_content=message.reasoning_content% } { %-else% } { %-if'</think>'incontent% } { %-setreasoning_content=content.split('</think>') [ 0 ] .rstrip('\n').split('<think>') [ -1 ] .lstrip('\n')% } { %-setcontent=content.split('</think>') [ -1 ] .lstrip('\n')% } { %-endif% } { %-endif% } { %-ifloop.index0>ns.last_query_index% } { %-ifloop.lastor(notloop.lastandreasoning_content)% } { { -'<|im_start|>'+message.role+'\n<think>\n'+reasoning_content.strip('\n')+'\n</think>\n\n'+content.lstrip('\n') } } { %-else% } { { -'<|im_start|>'+message.role+'\n'+content } } { %-endif% } { %-else% } { { -'<|im_start|>'+message.role+'\n'+content } } { %-endif% } { %-ifmessage.tool_calls% } { %-fortool_callinmessage.tool_calls% } { %-if(loop.firstandcontent)or(notloop.first)% } { { -'\n' } } { %-endif% } { %-iftool_call.function% } { %-settool_call=tool_call.function% } { %-endif% } { { -'<tool_call>\n { "name" : "' }} {{- tool_call.name }} {{- '" , "arguments" : ' } } { %-iftool_call.argumentsisstring% } { { -tool_call.arguments } } { %-else% } { { -tool_call.arguments|tojson } } { %-endif% } { { -' } \n</tool_call>' } } { %-endfor% } { %-endif% } { { -'<|im_end|>\n' } } { %-elifmessage.role=="tool" % } { %-ifloop.firstor(messages [ loop.index0-1 ] .role!="tool" )% } { { -'<|im_start|>user' } } { %-endif% } { { -'\n<tool_response>\n' } } { { -content } } { { -'\n</tool_response>' } } { %-ifloop.lastor(messages [ loop.index0+1 ] .role!="tool" )% } { { -'<|im_end|>\n' } } { %-endif% } { %-endif% } { %-endfor% } { %-ifadd_generation_prompt% } { { -'<|im_start|>assistant\n' } } { %-ifenable_thinkingisdefinedandenable_thinkingisfalse% } { { -'<think>\n\n</think>\n\n' } } { %-endif% } { %-endif% }
大致能看懂一些,在最开始就会判断是否有工具,如果有工具的话,会组装出一些和工具调用有关的prompt,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 { "messages" : [ { "role" : "system" , "content" : "You are a helpful assistant. " } , { "role" : "user" , "content" : "What is my horoscope? I am an Aquarius." } ] , "tools" : [ { "type" : "function" , "name" : "get_horoscope" , "description" : "Get today's horoscope for an astrological sign." , "parameters" : { "type" : "object" , "properties" : { "sign" : { "type" : "string" , "description" : "An astrological sign like Taurus or Aquarius" , } , } , "required" : [ "sign" ] , } , } , ] }
最终大模型得到的提示词就是:
1 2 3 4 5 6 <|im_start|>systemYou are a helpful assistant. # Tools You may call one or more functions to assist with the user query.You are provided with function signatures within <tools></tools> XML tags:<tools>{"type": "function", "name": "get_horoscope", "description": "Get today's horoscope for an astrological sign.", "parameters": {"type": "object", "properties": {"sign": {"type": "string", "description": "An astrological sign like Taurus or Aquarius"}}, "required": ["sign"]}}</ tools>For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:<tool_call>{"name": <function-name>, "arguments": <args-json-object>}</ tool_call><|im_end|><|im_start|>userWhat is my horoscope? I am an Aquarius .<|im_end|><|im_start|>assistant
这里面的<|im_start|>、<|im_end|> 叫做special tokens,可以理解为一线特殊标记,方便模型区分段落。
把这段话直接贴给大模型,得到内容:
第一次返回就是一个function call的结果,他告诉我们要调用哪个函数,以及参数信息。然后我给出工具调用结果后,他在组装到之前的任务中,告诉我星座运势是Everything is good!