function calling 仍需要寫程式與LLM互相配合
分享Azure OpenAI叫用 function 的小技巧, 此方法可在沒有MCP架構的情況下使用.
原本在prompt內文用文字描述列出所有可用function讓LLM自動選擇要使用哪個,
再收到LLM 回應要用的Function及input參數後, 程式再去執行,
在Azure OpenAI內也有嚴謹的寫法, 讓程式更方便與LLM溝通,
叫用的系統能提供function或db table欄位或view, 讓AI決定取用哪個或直接組sql, 以利串接整個流程:
ex. 第一次呼叫ai時, 列出所有function如下tools內容:
{
"model": "GPT-5.1",
"input": [
{
"role": "user",
"content": "客戶的帳戶為123456789,並查詢其可買賣的目前美元匯率。"
}
],
"tools": [
{
"type": "function",
"name": "get_customer_role",
"description": "客戶優惠匯率的角色",
"parameters": {
"type": "object",
"properties": {
"account_id": {
"type": "string",
"description": "客戶的存款帳戶號碼"
}
},
"required": ["account_id"]
}
},
{
"type": "function",
"name": "get_exchange_rate",
"description": "查詢指定貨幣對新台幣的最新匯率。",
"parameters": {
"type": "object",
"properties": {
"currency_code": {
"type": "string",
"description": "貨幣代碼,例如 USD"
},
"account_role": {
"type": "string",
"description": "客戶優惠匯率的角色"
}
},
"required": ["currency_code"]
}
}
],
"tool_choice": "auto"
}第一次回傳的結果如下:
{
...略...
"output": [
{
"id": "fc_0ba32631ac2c749e006a41e5e0a33c8193b69769f889ffb2c3",
"type": "function_call",
"status": "completed",
"arguments": "{\"account_id\":\"123456789\"}",
"call_id": "call_NyKpLYpdasevTCHgStp7vG1O",
"name": "get_customer_role"
}
],
...略...
}程式依以上回覆指定的function和參數去呼叫後, 再將結果插入json如下黃底再次呼叫ai:
{
"model": "GPT-5.1",
"input": [
{
"role": "user",
"content":"客戶的帳戶為123456789,並查詢其可買賣的目前美元匯率。"
},
{
"type": "function_call",
"call_id": "call_NyKpLYpdasevTCHgStp7vG1O",
"name": "get_customer_role",
"arguments": "{\"account_id\":\"123456789\"}"
},
{
"type": "function_call_output",
"call_id": "call_NyKpLYpdasevTCHgStp7vG1O",
"output": "{\"account_role\":\"VIP\"}"
}
],
"tools": [
{
"type": "function",
"name": "get_customer_role",
"description": "客戶優惠匯率的角色",
"parameters": {
"type": "object",
"properties": {
"account_id": {
"type": "string"
}
},
"required": ["account_id"]
}
},
{
"type": "function",
"name": "get_exchange_rate",
"description": "查詢指定貨幣對新台幣的最新匯率。",
"parameters": {
"type": "object",
"properties": {
"currency_code": {
"type": "string"
},
"account_role": {
"type": "string"
}
},
"required": ["currency_code", "account_role"]
}
}
]
}第2次reponse如下:
{
...略...
"output": [
{
"id": "fc_072fc6ce6633715c006a41ea9ebddc8193aa3c4f6f26bb41a7",
"type": "function_call",
"status": "completed",
"arguments": "{\"currency_code\":\"USD\",\"account_role\":\"VIP\"}",
"call_id": "call_2RFdDB1lXcjuYbTcSpLLuEqm",
"name": "get_exchange_rate"
}
],
...略...
}同樣再呼叫function後, 再上傳ai, 重覆循環, 直到取得最後結果
Taiwan is a country. 臺灣是我的國家