{"id":20513,"date":"2025-01-16T14:26:31","date_gmt":"2025-01-16T06:26:31","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=20513"},"modified":"2025-02-08T21:14:35","modified_gmt":"2025-02-08T13:14:35","slug":"hanshudiaoyongfunct","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/hanshudiaoyongfunct\/","title":{"rendered":"OpenAI \u51fd\u6570\u8c03\u7528\uff08Function calling\uff09"},"content":{"rendered":"<h2>OpenAI Function calling V2 \u7279\u70b9<\/h2>\n<p>Function calling V2 \u7684\u6838\u5fc3\u76ee\u6807\u662f\u8d4b\u4e88 OpenAI \u6a21\u578b\u4e0e\u5916\u90e8\u4e16\u754c\u4ea4\u4e92\u7684\u80fd\u529b\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u6838\u5fc3\u529f\u80fd\uff1a<\/p>\n<ol>\n<li><strong>\u6570\u636e\u83b7\u53d6 (Fetching Data) &#8211; RAG \u7684\u51fd\u6570\u8c03\u7528\u5b9e\u73b0:<\/strong>\n<ul>\n<li><strong>\u672c\u8d28\u662f RAG (\u68c0\u7d22\u589e\u5f3a\u751f\u6210):<\/strong>\u00a0Function calling \u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\u6765\u5b9e\u73b0\u68c0\u7d22\u589e\u5f3a\u751f\u6210 (<a href=\"https:\/\/www.kdjingpai.com\/de\/rag\/\">RAG<\/a>)\u3002\u6a21\u578b\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u9884\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u4ece\u5916\u90e8\u6570\u636e\u6e90\uff08\u5982\u77e5\u8bc6\u5e93\u3001API \u7b49\uff09\u68c0\u7d22\u6700\u65b0\u7684\u3001\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u878d\u5165\u5230\u81ea\u8eab\u7684\u751f\u6210\u56de\u590d\u4e2d\u3002<\/li>\n<li><strong>\u89e3\u51b3\u4fe1\u606f\u6ede\u540e\u548c\u77e5\u8bc6\u9650\u5236:<\/strong>\u00a0\u4f20\u7edf\u7684\u5927\u8bed\u8a00\u6a21\u578b\u77e5\u8bc6\u662f\u9759\u6001\u7684\uff0cFunction calling \u5f25\u8865\u4e86\u8fd9\u4e00\u7f3a\u9677\uff0c\u4f7f\u6a21\u578b\u80fd\u591f\u83b7\u53d6\u5b9e\u65f6\u4fe1\u606f\u6216\u7279\u5b9a\u9886\u57df\u77e5\u8bc6\uff0c\u4ece\u800c\u751f\u6210\u66f4\u51c6\u786e\u3001\u66f4\u5168\u9762\u7684\u56de\u590d\u3002<\/li>\n<li><strong>\u7528\u4f8b\u793a\u4f8b:<\/strong>\n<ul>\n<li><strong>\u5b9e\u65f6\u4fe1\u606f\u67e5\u8be2:<\/strong>\u00a0\u4f8b\u5982\uff0c\u83b7\u53d6\u5f53\u524d\u5929\u6c14\u6570\u636e\u3001\u6700\u65b0\u7684\u65b0\u95fb\u8d44\u8baf\u3001\u80a1\u7968\u4ef7\u683c\u7b49\u3002<\/li>\n<li><strong>\u77e5\u8bc6\u5e93\u641c\u7d22:<\/strong>\u00a0\u4f8b\u5982\uff0c\u67e5\u8be2\u4f01\u4e1a\u5185\u90e8\u77e5\u8bc6\u5e93\u3001\u4ea7\u54c1\u6587\u6863\u3001FAQ \u7b49\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u4e13\u4e1a\u7684\u89e3\u7b54\u3002<\/li>\n<li><strong>API \u6570\u636e\u96c6\u6210:<\/strong>\u00a0\u4f8b\u5982\uff0c\u4ece\u5916\u90e8 API \u83b7\u53d6\u5546\u54c1\u4fe1\u606f\u3001\u822a\u73ed\u52a8\u6001\u3001\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\u7b49\uff0c\u4e30\u5bcc\u5bf9\u8bdd\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f:<\/strong>\u00a0\u5f00\u53d1\u8005\u5b9a\u4e49\u7528\u4e8e\u6570\u636e\u68c0\u7d22\u7684\u51fd\u6570 (\u4f8b\u5982\u00a0<code>get_weather<\/code>,\u00a0<code>search_knowledge_base<\/code>)\uff0c\u5e76\u5728\u00a0<code>tools<\/code>\u00a0\u53c2\u6570\u4e2d\u63d0\u4f9b\u7ed9\u6a21\u578b\u3002\u5f53\u6a21\u578b\u5224\u65ad\u9700\u8981\u5916\u90e8\u4fe1\u606f\u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u5e76\u83b7\u53d6\u7ed3\u679c\uff0c\u518d\u5c06\u7ed3\u679c\u6574\u5408\u5230\u6700\u7ec8\u56de\u590d\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u6267\u884c\u52a8\u4f5c (Taking Action) &#8211; \u6a21\u578b\u9a71\u52a8\u7684\u81ea\u52a8\u5316:<\/strong>\n<ul>\n<li><strong>\u8d85\u8d8a\u4fe1\u606f\u63d0\u4f9b\uff0c\u9a71\u52a8\u5b9e\u9645\u64cd\u4f5c:<\/strong>\u00a0Function calling \u4e0d\u4ec5\u9650\u4e8e\u4fe1\u606f\u68c0\u7d22\uff0c\u66f4\u8fdb\u4e00\u6b65\uff0c\u5b83\u5141\u8bb8\u6a21\u578b\u9a71\u52a8\u5916\u90e8\u7cfb\u7edf\u6267\u884c\u5b9e\u9645\u7684\u64cd\u4f5c\uff0c\u5b9e\u73b0\u66f4\u6df1\u5c42\u6b21\u7684\u81ea\u52a8\u5316\u548c\u5e94\u7528\u573a\u666f\u3002<\/li>\n<li><strong>\u589e\u5f3a\u6a21\u578b\u7684\u5b9e\u7528\u6027\u548c\u5e94\u7528\u8303\u56f4:<\/strong>\u00a0\u8fd9\u4f7f\u5f97\u6a21\u578b\u53ef\u4ee5\u4e0d\u4ec5\u4ec5\u662f\u5bf9\u8bdd\u4f19\u4f34\uff0c\u66f4\u53ef\u4ee5\u6210\u4e3a\u667a\u80fd\u52a9\u624b\uff0c\u5e2e\u52a9\u7528\u6237\u5b8c\u6210\u5404\u79cd\u4efb\u52a1\u3002<\/li>\n<li><strong>\u7528\u4f8b\u793a\u4f8b:<\/strong>\n<ul>\n<li><strong>\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41:<\/strong>\u00a0\u4f8b\u5982\uff0c\u63d0\u4ea4\u8868\u5355\u3001\u521b\u5efa\u65e5\u5386\u4e8b\u4ef6\u3001\u53d1\u9001\u90ae\u4ef6\u3001\u9884\u8ba2\u673a\u7968\u9152\u5e97\u7b49\u3002<\/li>\n<li><strong>\u5e94\u7528\u7a0b\u5e8f\u63a7\u5236:<\/strong>\u00a0\u4f8b\u5982\uff0c\u4fee\u6539\u5e94\u7528\u7a0b\u5e8f\u72b6\u6001 (UI\/\u524d\u7aef\u6216\u540e\u7aef)\u3001\u63a7\u5236\u667a\u80fd\u5bb6\u5c45\u8bbe\u5907\u7b49\u3002<\/li>\n<li><strong>\u4ee3\u7406\u5de5\u4f5c\u6d41 (Agentic Workflow):<\/strong>\u00a0\u4f8b\u5982\uff0c\u6839\u636e\u5bf9\u8bdd\u5185\u5bb9\uff0c\u5c06\u5bf9\u8bdd\u79fb\u4ea4\u7ed9\u66f4\u4e13\u4e1a\u7684\u5ba2\u670d\u7cfb\u7edf\uff0c\u6216\u8005\u89e6\u53d1\u66f4\u590d\u6742\u7684\u81ea\u52a8\u5316\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b9e\u73b0\u65b9\u5f0f:<\/strong>\u00a0\u5f00\u53d1\u8005\u5b9a\u4e49\u7528\u4e8e\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u7684\u51fd\u6570 (\u4f8b\u5982\u00a0<code>send_email<\/code>,\u00a0<code>create_calendar_event<\/code>)\uff0c\u5e76\u5728\u00a0<code>tools<\/code>\u00a0\u53c2\u6570\u4e2d\u63d0\u4f9b\u7ed9\u6a21\u578b\u3002\u6a21\u578b\u53ef\u4ee5\u6839\u636e\u7528\u6237\u610f\u56fe\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u76f8\u5e94\u7684\u53c2\u6570\u6765\u89e6\u53d1\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>Function calling V2 \u7684\u5176\u4ed6\u5173\u952e\u7279\u6027 (\u652f\u6301\u6570\u636e\u83b7\u53d6\u548c\u6267\u884c\u52a8\u4f5c):<\/strong><\/p>\n<ul>\n<li><strong><code>tools<\/code>\u00a0\u53c2\u6570\u548c\u51fd\u6570\u6a21\u5f0f (Function Schema):<\/strong>\u00a0\u63d0\u4f9b\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u548c\u7ba1\u7406\u6a21\u578b\u53ef\u4ee5\u8c03\u7528\u7684\u51fd\u6570\uff0c\u5305\u62ec\u51fd\u6570\u540d\u3001\u63cf\u8ff0\u548c\u53c2\u6570\u5b9a\u4e49\uff0c\u786e\u4fdd\u6a21\u578b\u51c6\u786e\u7406\u89e3\u548c\u8c03\u7528\u51fd\u6570\u3002<\/li>\n<li><strong>\u4e25\u683c\u6a21\u5f0f (Strict Mode):<\/strong>\u00a0\u63d0\u9ad8\u51fd\u6570\u8c03\u7528\u7684\u53ef\u9760\u6027\u548c\u51c6\u786e\u6027\uff0c\u786e\u4fdd\u6a21\u578b\u8f93\u51fa\u7684\u51fd\u6570\u8c03\u7528\u4e25\u683c\u7b26\u5408\u9884\u5b9a\u4e49\u7684\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u5de5\u5177\u9009\u62e9 (Tool Choice) \u548c\u5e76\u884c\u51fd\u6570\u8c03\u7528\u63a7\u5236:<\/strong>\u00a0\u63d0\u4f9b\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u5e94\u7528\u573a\u666f\u8c03\u6574\u6a21\u578b\u8c03\u7528\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u5f3a\u5236\u8c03\u7528\u7279\u5b9a\u51fd\u6570\u6216\u9650\u5236\u5e76\u884c\u8c03\u7528\u6570\u91cf\u3002<\/li>\n<li><strong>\u6d41\u5f0f\u4f20\u8f93 (Streaming):<\/strong>\u00a0\u6539\u5584\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5b9e\u65f6\u5c55\u793a\u51fd\u6570\u53c2\u6570\u7684\u586b\u5145\u8fc7\u7a0b\uff0c\u8ba9\u7528\u6237\u66f4\u76f4\u89c2\u5730\u4e86\u89e3\u6a21\u578b\u7684\u601d\u8003\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p><strong>\u603b\u7ed3:<\/strong><\/p>\n<p>Function calling V2 \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u901a\u8fc7\u00a0<strong>\u6570\u636e\u83b7\u53d6 (RAG \u5b9e\u73b0)<\/strong>\u00a0\u548c\u00a0<strong>\u6267\u884c\u52a8\u4f5c<\/strong>\u00a0\u8fd9\u4e24\u5927\u529f\u80fd\uff0c\u6781\u5927\u5730\u62d3\u5c55\u4e86\u5927\u8bed\u8a00\u6a21\u578b\u7684\u5e94\u7528\u8fb9\u754c\u3002 \u5b83\u4e0d\u4ec5\u4f7f\u6a21\u578b\u80fd\u591f\u8bbf\u95ee\u548c\u5229\u7528\u5916\u90e8\u4fe1\u606f\uff0c\u751f\u6210\u66f4\u667a\u80fd\u3001\u66f4\u5b9e\u7528\u7684\u56de\u590d\uff0c\u66f4\u80fd\u9a71\u52a8\u5916\u90e8\u7cfb\u7edf\u6267\u884c\u64cd\u4f5c\uff0c\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u522b\u7684\u81ea\u52a8\u5316\uff0c\u4e3a\u6784\u5efa\u66f4\u5f3a\u5927\u7684 AI \u5e94\u7528\u5960\u5b9a\u4e86\u57fa\u7840\u3002 \u6570\u636e\u83b7\u53d6\u4f5c\u4e3a RAG \u7684\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u662f Function calling V2 \u5728\u77e5\u8bc6\u5bc6\u96c6\u578b\u5e94\u7528\u4e2d\u7684\u5173\u952e\u80fd\u529b\u4f53\u73b0\u3002<\/p>\n<hr \/>\n<p>\u4ee5\u4e0b\u662f OpenAI \u5b98\u65b9 <a href=\"https:\/\/www.kdjingpai.com\/de\/damoxingjiegouhuaai\/\">\u51fd\u6570\u8c03\u7528<\/a>\uff08Function calling\uff09\u65b0\u7248\u8bf4\u660e\uff0c\u4f7f\u6a21\u578b\u80fd\u591f\u83b7\u53d6\u6570\u636e\u5e76\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u51fd\u6570\u8c03\u7528<\/strong>\u00a0\u4e3a OpenAI \u6a21\u578b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u4f7f\u5176\u80fd\u591f\u4e0e\u4f60\u7684\u4ee3\u7801\u6216\u5916\u90e8\u670d\u52a1\u4ea4\u4e92\uff0c\u5e76\u4e14\u4e3b\u8981\u6709\u4e24\u4e2a\u7528\u4f8b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u83b7\u53d6\u6570\u636e<\/td>\n<td>\u68c0\u7d22\u6700\u65b0\u7684\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u6574\u5408\u5230\u6a21\u578b\u7684\u56de\u590d\u4e2d (RAG)\u3002 \u8fd9\u5bf9\u4e8e\u641c\u7d22\u77e5\u8bc6\u5e93\u4ee5\u53ca\u4ece API \u83b7\u53d6\u7279\u5b9a\u6570\u636e\uff08\u4f8b\u5982\uff0c\u5f53\u524d\u5929\u6c14\u6570\u636e\uff09\u975e\u5e38\u6709\u7528\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u6267\u884c\u64cd\u4f5c<\/td>\n<td>\u6267\u884c\u8bf8\u5982\u63d0\u4ea4\u8868\u5355\u3001\u8c03\u7528 API\u3001\u4fee\u6539\u5e94\u7528\u7a0b\u5e8f\u72b6\u6001\uff08UI\/\u524d\u7aef\u6216\u540e\u7aef\uff09\u6216\u91c7\u53d6\u4ee3\u7406\u5de5\u4f5c\u6d41\u64cd\u4f5c\uff08\u5982\u79fb\u4ea4\u5bf9\u8bdd\uff09\u7b49\u64cd\u4f5c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u4f60\u53ea\u60f3\u8ba9<strong>\u6a21\u578b\u751f\u6210 JSON<\/strong>\uff0c\u8bf7\u53c2\u9605 OpenAI \u5173\u4e8e structured outputs \u7684\u6587\u6863\uff0c\u786e\u4fdd\u6a21\u578b\u59cb\u7ec8\u751f\u6210\u7b26\u5408\u60a8\u63d0\u4f9b\u7684 <a href=\"https:\/\/www.kdjingpai.com\/dify-zhongde-jsonarra\/\">JSON Schema<\/a> \u7684\u54cd\u5e94\u3002<\/p>\n<p><strong>\u83b7\u53d6\u5929\u6c14<\/strong><\/p>\n<p>\u4f7f\u7528 get_weather \u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u793a\u4f8b<\/p>\n<pre><code>from openai import OpenAI\r\nclient = OpenAI()\r\ntools = [{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u83b7\u53d6\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u5f53\u524d\u6e29\u5ea6\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"location\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n}\r\n},\r\n\"required\": [\r\n\"location\"\r\n],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}]\r\ncompletion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=[{\"role\": \"user\", \"content\": \"\u4eca\u5929\u5df4\u9ece\u7684\u5929\u6c14\u600e\u4e48\u6837\uff1f\"}],\r\ntools=tools\r\n)\r\nprint(completion.choices[0].message.tool_calls)\r\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre><code>[{\r\n\"id\": \"call_12345xyz\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"arguments\": \"{\\\"location\\\":\\\"Paris, France\\\"}\"\r\n}\r\n}]\r\n<\/code><\/pre>\n<p><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528 send_email \u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u793a\u4f8b<\/p>\n<pre><code>from openai import OpenAI\r\nclient = OpenAI()\r\ntools = [{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"send_email\",\r\n\"description\": \"\u5411\u7ed9\u5b9a\u7684\u6536\u4ef6\u4eba\u53d1\u9001\u5305\u542b\u4e3b\u9898\u548c\u6d88\u606f\u7684\u7535\u5b50\u90ae\u4ef6\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"to\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u6536\u4ef6\u4eba\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\"\r\n},\r\n\"subject\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u7535\u5b50\u90ae\u4ef6\u4e3b\u9898\u884c\u3002\"\r\n},\r\n\"body\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u7535\u5b50\u90ae\u4ef6\u6d88\u606f\u6b63\u6587\u3002\"\r\n}\r\n},\r\n\"required\": [\r\n\"to\",\r\n\"subject\",\r\n\"body\"\r\n],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}]\r\ncompletion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=[{\"role\": \"user\", \"content\": \"\u4f60\u80fd\u7ed9 ilan@example.com \u548c katia@example.com \u53d1\u9001\u90ae\u4ef6\u8bf4\u201chi\u201d\u5417\uff1f\"}],\r\ntools=tools\r\n)\r\nprint(completion.choices[0].message.tool_calls)\r\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre><code>[\r\n{\r\n\"id\": \"call_9876abc\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"send_email\",\r\n\"arguments\": \"{\\\"to\\\":\\\"ilan@example.com\\\",\\\"subject\\\":\\\"Hello!\\\",\\\"body\\\":\\\"Just wanted to say hi\\\"}\"\r\n}\r\n},\r\n{\r\n\"id\": \"call_9876abc\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"send_email\",\r\n\"arguments\": \"{\\\"to\\\":\\\"katia@example.com\\\",\\\"subject\\\":\\\"Hello!\\\",\\\"body\\\":\\\"Just wanted to say hi\\\"}\"\r\n}\r\n}\r\n]\r\n<\/code><\/pre>\n<p><strong>\u641c\u7d22\u77e5\u8bc6\u5e93<\/strong><\/p>\n<p>\u4f7f\u7528 search_knowledge_base \u51fd\u6570\u7684\u51fd\u6570\u8c03\u7528\u793a\u4f8b<\/p>\n<pre><code>from openai import OpenAI\r\nclient = OpenAI()\r\ntools = [{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"search_knowledge_base\",\r\n\"description\": \"\u67e5\u8be2\u77e5\u8bc6\u5e93\u4ee5\u68c0\u7d22\u5173\u4e8e\u67d0\u4e2a\u4e3b\u9898\u7684\u76f8\u5173\u4fe1\u606f\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"query\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u7528\u6237\u95ee\u9898\u6216\u641c\u7d22\u67e5\u8be2\u3002\"\r\n},\r\n\"options\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"num_results\": {\r\n\"type\": \"number\",\r\n\"description\": \"\u8981\u8fd4\u56de\u7684\u6392\u540d\u9760\u524d\u7684\u7ed3\u679c\u6570\u91cf\u3002\"\r\n},\r\n\"domain_filter\": {\r\n\"type\": [\r\n\"string\",\r\n\"null\"\r\n],\r\n\"description\": \"\u53ef\u9009\u7684\u57df\uff0c\u7528\u4e8e\u7f29\u5c0f\u641c\u7d22\u8303\u56f4\uff08\u4f8b\u5982\uff0c\u201cfinance\u201d\uff0c\u201cmedical\u201d\uff09\u3002 \u5982\u679c\u4e0d\u9700\u8981\uff0c\u5219\u4f20\u9012 null\u3002\"\r\n},\r\n\"sort_by\": {\r\n\"type\": [\r\n\"string\",\r\n\"null\"\r\n],\r\n\"enum\": [\r\n\"<a href=\"https:\/\/www.kdjingpai.com\/de\/relevance-ai\/\">relevance<\/a>\",\r\n\"date\",\r\n\"popularity\",\r\n\"alphabetical\"\r\n],\r\n\"description\": \"\u5982\u4f55\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002 \u5982\u679c\u4e0d\u9700\u8981\uff0c\u5219\u4f20\u9012 null\u3002\"\r\n}\r\n},\r\n\"required\": [\r\n\"num_results\",\r\n\"domain_filter\",\r\n\"sort_by\"\r\n],\r\n\"additionalProperties\": False\r\n}\r\n},\r\n\"required\": [\r\n\"query\",\r\n\"options\"\r\n],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}]\r\ncompletion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=[{\"role\": \"user\", \"content\": \"\u4f60\u80fd\u5728 AI \u77e5\u8bc6\u5e93\u4e2d\u627e\u5230\u5173\u4e8e <a href=\"https:\/\/www.kdjingpai.com\/de\/chatgpt-6\/\">ChatGPT<\/a> \u7684\u4fe1\u606f\u5417\uff1f\"}],\r\ntools=tools\r\n)\r\nprint(completion.choices[0].message.tool_calls)\r\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre><code>[{\r\n\"id\": \"call_4567xyz\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"search_knowledge_base\",\r\n\"arguments\": \"{\\\"query\\\":\\\"What is ChatGPT?\\\",\\\"options\\\":{\\\"num_results\\\":3,\\\"domain_filter\\\":null,\\\"sort_by\\\":\\\"relevance\\\"}}\"\r\n}\r\n}]\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u6982\u8ff0<\/h2>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8ba9 OpenAI \u6a21\u578b\u8bbf\u95ee\u00a0<strong>tools<\/strong>\u00a0\u6765\u6269\u5c55\u5176\u529f\u80fd\uff0c<strong>tools<\/strong>\u00a0\u53ef\u4ee5\u6709\u4e24\u79cd\u5f62\u5f0f\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u51fd\u6570\u8c03\u7528<\/strong><\/td>\n<td><strong>\u5f00\u53d1\u8005\u5b9a\u4e49\u7684\u4ee3\u7801\u3002<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>\u6258\u7ba1\u5de5\u5177<\/strong><\/td>\n<td><strong>OpenAI \u6784\u5efa\u7684\u5de5\u5177\u3002\uff08\u4f8b\u5982\uff0c\u6587\u4ef6\u641c\u7d22\u3001\u4ee3\u7801\u89e3\u91ca\u5668\uff09\u4ec5\u5728 Assistants API \u4e2d\u53ef\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u672c\u6307\u5357\u5c06\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u00a0<strong>\u51fd\u6570\u8c03\u7528<\/strong>\u00a0\u8ba9\u6a21\u578b\u8bbf\u95ee\u4f60\u81ea\u5df1\u7684\u51fd\u6570\u3002 \u57fa\u4e8e\u7cfb\u7edf\u63d0\u793a\u548c\u6d88\u606f\uff0c\u6a21\u578b\u53ef\u80fd\u4f1a\u51b3\u5b9a\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u2014\u2014<strong>\u800c\u4e0d\u662f\uff08\u6216\u9664\u4e86\uff09\u751f\u6210\u6587\u672c\u6216\u97f3\u9891<\/strong>\u3002<\/p>\n<p><strong>\u4e4b\u540e\uff0c\u4f60\u9700\u8981\u6267\u884c\u51fd\u6570\u4ee3\u7801\uff0c\u5c06\u7ed3\u679c\u53d1\u56de\uff0c\u6a21\u578b\u4f1a\u5c06\u7ed3\u679c\u6574\u5408\u5230\u5176\u6700\u7ec8\u56de\u590d\u4e2d\u3002<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20515\" title=\"\u51fd\u6570\u8c03\u7528\uff08Function calling\uff09-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34.png\" alt=\"\u51fd\u6570\u8c03\u7528\uff08Function calling\uff09-1\" width=\"758\" height=\"1091\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34.png 3024w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34-768x1105.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34-1067x1536.png 1067w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34-1423x2048.png 1423w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/c7a911da2ba7b34-8x12.png 8w\" sizes=\"auto, (max-width: 758px) 100vw, 758px\" \/><\/p>\n<h3>\u793a\u4f8b\u51fd\u6570<\/h3>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u5141\u8bb8\u6a21\u578b\u4f7f\u7528\u4e0b\u9762\u5b9a\u4e49\u7684\u771f\u5b9e\u00a0<strong>get_weather<\/strong>\u00a0\u51fd\u6570\u7684\u6b65\u9aa4\uff1a<\/p>\n<p>\u5728\u4f60\u7684\u4ee3\u7801\u5e93\u4e2d\u5b9e\u73b0\u7684\u793a\u4f8b get_weather \u51fd\u6570<\/p>\n<pre><code>import requests\r\ndef get_weather(latitude, longitude):\r\nresponse = requests.get(f\"https:\/\/api.open-meteo.com\/v1\/forecast?latitude={latitude}&amp;longitude={longitude}\u00a4t=temperature_2m,wind_speed_10m&amp;hourly=temperature_2m,relative_humidity_2m,wind_speed_10m\")\r\ndata = response.json()\r\nreturn data['current']['temperature_2m']\r\n<\/code><\/pre>\n<p>\u4e0e\u4e4b\u524d\u7684\u6d41\u7a0b\u56fe\u4e0d\u540c\uff0c\u6b64\u51fd\u6570\u9700\u8981\u7cbe\u786e\u7684\u00a0<strong>latitude<\/strong>\u00a0\u548c\u00a0<strong>longitude<\/strong>\uff0c\u800c\u4e0d\u662f\u901a\u7528\u7684\u00a0<strong>location<\/strong>\u00a0\u53c2\u6570\u3002\uff08\u4f46\u662f\uff0c\u6211\u4eec\u7684\u6a21\u578b\u53ef\u4ee5\u81ea\u52a8\u786e\u5b9a\u8bb8\u591a\u4f4d\u7f6e\u7684\u5750\u6807\uff01\uff09<\/p>\n<h3>\u51fd\u6570\u8c03\u7528\u6b65\u9aa4<\/h3>\n<p>\u4f7f\u7528\u5b9a\u4e49\u7684\u51fd\u6570\u8c03\u7528\u6a21\u578b\u2014\u2014\u4ee5\u53ca\u4f60\u7684\u7cfb\u7edf\u548c\u7528\u6237\u6d88\u606f\u3002<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u4f7f\u7528\u5b9a\u4e49\u7684 get_weather \u5de5\u5177\u8c03\u7528\u6a21\u578b<\/strong><\/p>\n<pre><code>from openai import OpenAI\r\nimport json\r\nclient = OpenAI()\r\ntools = [{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u83b7\u53d6\u7ed9\u5b9a\u5750\u6807\u7684\u5f53\u524d\u6e29\u5ea6\uff0c\u5355\u4f4d\u4e3a\u6444\u6c0f\u5ea6\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"latitude\": {\"type\": \"number\"},\r\n\"longitude\": {\"type\": \"number\"}\r\n},\r\n\"required\": [\"latitude\", \"longitude\"],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}]\r\nmessages = [{\"role\": \"user\", \"content\": \"\u4eca\u5929\u5df4\u9ece\u7684\u5929\u6c14\u600e\u4e48\u6837\uff1f\"}]\r\ncompletion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=messages,\r\ntools=tools,\r\n)\r\n<\/code><\/pre>\n<p><strong>\u6b65\u9aa42\uff1a\u6a21\u578b\u51b3\u5b9a\u8c03\u7528\u51fd\u6570\u2014\u2014\u6a21\u578b\u8fd4\u56de \u540d\u79f0\u548c\u8f93\u5165\u53c2\u6570\u3002<\/strong><\/p>\n<p><strong>completion.choices[0].message.tool_calls<\/strong><\/p>\n<pre><code>[{\r\n\"id\": \"call_12345xyz\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"arguments\": \"{\\\"latitude\\\":48.8566,\\\"longitude\\\":2.3522}\"\r\n}\r\n}]\r\n<\/code><\/pre>\n<p><strong>\u6b65\u9aa4 3\uff1a\u6267\u884c\u51fd\u6570\u4ee3\u7801\u2014\u2014\u89e3\u6790\u6a21\u578b\u7684\u54cd\u5e94\u5e76\u5904\u7406\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<p>\u6267\u884c get_weather \u51fd\u6570<\/p>\n<pre><code>tool_call = completion.choices[0].message.tool_calls[0]\r\nargs = json.loads(tool_call.function.arguments)\r\nresult = get_weather(args[\"latitude\"], args[\"longitude\"])\r\n<\/code><\/pre>\n<p><strong>\u6b65\u9aa4 4\uff1a\u5411\u6a21\u578b\u63d0\u4f9b\u7ed3\u679c\u2014\u2014\u4ee5\u4fbf\u6a21\u578b\u53ef\u4ee5\u5c06\u7ed3\u679c\u6574\u5408\u5230\u5176\u6700\u7ec8\u56de\u590d\u4e2d\u3002<\/strong><\/p>\n<p>\u63d0\u4f9b\u7ed3\u679c\u5e76\u518d\u6b21\u8c03\u7528\u6a21\u578b<\/p>\n<pre><code>messages.append(completion.choices[0].message)  # append model's function call message\r\nmessages.append({                               # append result message\r\n\"role\": \"tool\",\r\n\"tool_call_id\": tool_call.id,\r\n\"content\": str(result)\r\n})\r\ncompletion_2 = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=messages,\r\ntools=tools,\r\n)\r\n<\/code><\/pre>\n<p><strong>\u6b65\u9aa4 4\uff1a\u6a21\u578b\u54cd\u5e94\u2014\u2014\u5c06\u7ed3\u679c\u6574\u5408\u5230\u5176\u8f93\u51fa\u4e2d\u3002<\/strong><\/p>\n<p>completion_2.choices[0].message.content<\/p>\n<pre><code>\"\u5df4\u9ece\u5f53\u524d\u6e29\u5ea6\u4e3a 14\u00b0C (57.2\u00b0F)\u3002\"\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u5b9a\u4e49\u51fd\u6570<\/h2>\n<p>\u51fd\u6570\u53ef\u4ee5\u5728\u6bcf\u4e2a API \u8bf7\u6c42\u7684\u00a0<strong>tools<\/strong>\u00a0\u53c2\u6570\u4e2d\uff0c\u4ee5\u00a0<strong>function<\/strong>\u00a0\u5bf9\u8c61\u7684\u5f62\u5f0f\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<p>\u51fd\u6570\u7531\u5176\u6a21\u5f0f\u5b9a\u4e49\uff0c\u8be5\u6a21\u5f0f\u544a\u77e5\u6a21\u578b\u51fd\u6570\u7684\u529f\u80fd\u4ee5\u53ca\u671f\u671b\u7684\u8f93\u5165\u53c2\u6570\u3002 \u5b83\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>\u5b57\u6bb5<\/strong><\/th>\n<th><strong>\u63cf\u8ff0<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>name<\/strong><\/td>\n<td><strong>\u51fd\u6570\u7684\u540d\u79f0\uff08\u4f8b\u5982\uff0cget_weather\uff09<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>description<\/strong><\/td>\n<td><strong>\u5173\u4e8e\u4f55\u65f6\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>parameters<\/strong><\/td>\n<td><strong>\u5b9a\u4e49\u51fd\u6570\u8f93\u5165\u53c2\u6570\u7684 JSON schema<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u51fd\u6570\u6a21\u5f0f<\/strong><\/p>\n<pre><code>{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u68c0\u7d22\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u5f53\u524d\u5929\u6c14\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"location\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n},\r\n\"units\": {\r\n\"type\": \"string\",\r\n\"enum\": [\r\n\"celsius\",\r\n\"fahrenheit\"\r\n],\r\n\"description\": \"\u6e29\u5ea6\u5c06\u4ee5\u4ec0\u4e48\u5355\u4f4d\u8fd4\u56de\u3002\"\r\n}\r\n},\r\n\"required\": [\r\n\"location\",\r\n\"units\"\r\n],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}\r\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u00a0<strong>parameters<\/strong>\u00a0\u7531 JSON schema \u5b9a\u4e49\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u5229\u7528\u5b83\u7684\u8bb8\u591a\u4e30\u5bcc\u7279\u6027\uff0c\u5982\u5c5e\u6027\u7c7b\u578b\u3001\u679a\u4e3e\u3001\u63cf\u8ff0\u3001\u5d4c\u5957\u5bf9\u8c61\u548c\u9012\u5f52\u5bf9\u8c61\u3002<\/p>\n<p><strong>\uff08\u53ef\u9009\uff09\u4f7f\u7528 pydantic \u548c zod \u8fdb\u884c\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<p>\u867d\u7136\u6211\u4eec\u9f13\u52b1\u4f60\u76f4\u63a5\u5b9a\u4e49\u51fd\u6570\u6a21\u5f0f\uff0c\u4f46\u6211\u4eec\u7684 SDK \u63d0\u4f9b\u4e86\u5e2e\u52a9\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5c06\u00a0<strong>pydantic<\/strong>\u00a0\u548c\u00a0<strong>zod<\/strong>\u00a0\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6a21\u5f0f\u3002 \u5e76\u975e\u6240\u6709\u00a0<strong>pydantic<\/strong>\u00a0\u548c\u00a0<strong>zod<\/strong>\u00a0\u529f\u80fd\u90fd\u53d7\u652f\u6301\u3002<\/p>\n<p><strong>\u5b9a\u4e49\u5bf9\u8c61\u4ee5\u8868\u793a\u51fd\u6570\u6a21\u5f0f<\/strong><\/p>\n<pre><code>from openai import OpenAI, pydantic_function_tool\r\nfrom pydantic import BaseModel, Field\r\nclient = OpenAI()\r\nclass GetWeather(BaseModel):\r\nlocation: str = Field(\r\n...,\r\ndescription=\"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n)\r\ntools = [pydantic_function_tool(GetWeather)]\r\ncompletion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=[{\"role\": \"user\", \"content\": \"\u4eca\u5929\u5df4\u9ece\u7684\u5929\u6c14\u600e\u4e48\u6837\uff1f\"}],\r\ntools=tools\r\n)\r\nprint(completion.choices[0].message.tool_calls)\r\n<\/code><\/pre>\n<h3>\u5b9a\u4e49\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p><strong>\u7f16\u5199\u6e05\u6670\u8be6\u7ec6\u7684\u51fd\u6570\u540d\u79f0\u3001\u53c2\u6570\u63cf\u8ff0\u548c\u8bf4\u660e\u3002<\/strong><\/p>\n<ul>\n<li>\u660e\u786e\u63cf\u8ff0\u51fd\u6570\u548c\u6bcf\u4e2a\u53c2\u6570\u7684\u7528\u9014\uff08\u53ca\u5176\u683c\u5f0f\uff09\uff0c\u4ee5\u53ca\u8f93\u51fa\u4ee3\u8868\u4ec0\u4e48\u3002<\/li>\n<li>\u4f7f\u7528\u7cfb\u7edf\u63d0\u793a\u6765\u63cf\u8ff0\u4f55\u65f6\uff08\u4ee5\u53ca\u4f55\u65f6\u4e0d\uff09\u4f7f\u7528\u6bcf\u4e2a\u51fd\u6570\u3002 \u901a\u5e38\uff0c\u8981 \u786e\u5207\u5730\u544a\u8bc9\u6a21\u578b\u8be5\u600e\u4e48\u505a\u3002<\/li>\n<li>\u5305\u542b\u793a\u4f8b\u548c\u8fb9\u7f18\u60c5\u51b5\uff0c\u5c24\u5176\u662f\u5728\u7ea0\u6b63\u4efb\u4f55\u91cd\u590d\u51fa\u73b0\u7684\u5931\u8d25\u65f6\u3002\uff08<strong>\u6ce8\u610f\uff1a<\/strong>\u00a0\u6dfb\u52a0\u793a\u4f8b\u53ef\u80fd\u4f1a\u964d\u4f4e \u63a8\u7406\u6a21\u578b \u7684\u6027\u80fd\u3002\uff09<br \/>\n<strong>\u5e94\u7528\u8f6f\u4ef6\u5de5\u7a0b\u6700\u4f73\u5b9e\u8df5\u3002<\/strong><\/p>\n<ul>\n<li>\u4f7f\u51fd\u6570\u663e\u800c\u6613\u61c2\u4e14\u76f4\u89c2\u3002(\u6700\u5c0f\u60ca\u559c\u539f\u5219)<\/li>\n<li>\u4f7f\u7528\u679a\u4e3e\u548c\u5bf9\u8c61\u7ed3\u6784\uff0c\u4f7f\u65e0\u6548\u72b6\u6001\u65e0\u6cd5\u8868\u793a\u3002\uff08\u4f8b\u5982\uff0c<strong>toggle_light(on: bool, off: bool)<\/strong>\u00a0\u5141\u8bb8\u65e0\u6548\u8c03\u7528\uff09<\/li>\n<li>\u901a\u8fc7\u5b9e\u4e60\u751f\u6d4b\u8bd5\u3002 \u5b9e\u4e60\u751f\/\u4eba\u7c7b\u662f\u5426\u53ef\u4ee5\u5728\u4ec5\u63d0\u4f9b\u4f60\u7ed9\u6a21\u578b\u7684\u5185\u5bb9\u7684\u60c5\u51b5\u4e0b\u6b63\u786e\u4f7f\u7528\u8be5\u51fd\u6570\uff1f\uff08\u5982\u679c\u4e0d\u80fd\uff0c\u4ed6\u4eec\u4f1a\u95ee\u4f60\u4ec0\u4e48\u95ee\u9898\uff1f \u5c06\u7b54\u6848\u6dfb\u52a0\u5230\u63d0\u793a\u4e2d\u3002\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u5c3d\u53ef\u80fd\u51cf\u8f7b\u6a21\u578b\u7684\u8d1f\u62c5\u5e76\u4f7f\u7528\u4ee3\u7801\u3002<\/strong><\/p>\n<ul>\n<li>\u4e0d\u8981\u8ba9\u6a21\u578b\u586b\u5145\u4f60\u5df2\u7ecf\u77e5\u9053\u7684\u53c2\u6570\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u57fa\u4e8e\u4e4b\u524d\u7684\u83dc\u5355\u83b7\u5f97\u4e86\u00a0<strong>order_id<\/strong>\uff0c\u5219\u4e0d\u8981\u8bbe\u7f6e\u00a0<strong>order_id<\/strong>\u00a0\u53c2\u6570\u2014\u2014\u800c\u662f\u4e0d\u8bbe\u7f6e\u53c2\u6570\u00a0<strong>submit_refund()<\/strong>\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u4f20\u9012\u00a0<strong>order_id<\/strong>\u3002<\/li>\n<li>\u5408\u5e76\u59cb\u7ec8\u6309\u987a\u5e8f\u8c03\u7528\u7684\u51fd\u6570\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u603b\u662f\u5728\u00a0<strong>query_location()<\/strong>\u00a0\u4e4b\u540e\u8c03\u7528\u00a0<strong>mark_location()<\/strong>\uff0c\u53ea\u9700\u5c06\u6807\u8bb0\u903b\u8f91\u79fb\u81f3\u67e5\u8be2\u51fd\u6570\u8c03\u7528\u4e2d\u3002<strong>\u4e3a\u4e86\u66f4\u9ad8\u7684\u51c6\u786e\u6027\uff0c\u8bf7\u4fdd\u6301\u51fd\u6570\u6570\u91cf\u8f83\u5c11\u3002<\/strong>\n<ul>\n<li>\u4f7f\u7528\u4e0d\u540c\u6570\u91cf\u7684\u51fd\u6570\u8bc4\u4f30\u4f60\u7684\u6027\u80fd\u3002<\/li>\n<li>\u76ee\u6807\u662f\u4efb\u4f55\u65f6\u5019\u51fd\u6570\u6570\u91cf\u5c11\u4e8e 20 \u4e2a\uff0c\u4f46\u8fd9\u53ea\u662f\u4e00\u4e2a\u8f6f\u6027\u5efa\u8bae\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u5229\u7528 OpenAI \u8d44\u6e90\u3002<\/strong><\/p>\n<ul>\n<li>\u5728 <a href=\"https:\/\/www.kdjingpai.com\/de\/playground\/\">Playground<\/a> \u4e2d\u751f\u6210\u548c\u8fed\u4ee3\u51fd\u6570\u6a21\u5f0f\u3002<\/li>\n<li>\u8003\u8651 fine-tuning \u4ee5\u63d0\u9ad8\u5927\u91cf\u51fd\u6570\u6216\u56f0\u96be\u4efb\u52a1\u7684\u51fd\u6570\u8c03\u7528\u51c6\u786e\u6027\u3002<\/li>\n<\/ul>\n<h3>Token \u4f7f\u7528\u91cf<\/h3>\n<p>\u5728\u5e95\u5c42\uff0c\u51fd\u6570\u4ee5\u6a21\u578b\u7ecf\u8fc7\u8bad\u7ec3\u7684\u8bed\u6cd5\u6ce8\u5165\u5230\u7cfb\u7edf\u6d88\u606f\u4e2d\u3002 \u8fd9\u610f\u5473\u7740\u51fd\u6570\u8ba1\u5165\u6a21\u578b\u7684\u4e0a\u4e0b\u6587\u9650\u5236\uff0c\u5e76\u4f5c\u4e3a\u8f93\u5165 <a href=\"https:\/\/www.kdjingpai.com\/de\/tokenization\/\">Token<\/a> \u6536\u8d39\u3002 \u5982\u679c\u4f60\u9047\u5230 Token \u9650\u5236\uff0c\u6211\u4eec\u5efa\u8bae\u9650\u5236\u51fd\u6570\u6570\u91cf\u6216\u4f60\u4e3a\u51fd\u6570\u53c2\u6570\u63d0\u4f9b\u7684\u63cf\u8ff0\u957f\u5ea6\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u5de5\u5177\u89c4\u8303\u4e2d\u5b9a\u4e49\u4e86\u8bb8\u591a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 fine-tuning\u6765\u51cf\u5c11\u4f7f\u7528\u7684 Token \u6570\u91cf\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5904\u7406\u51fd\u6570\u8c03\u7528<\/h2>\n<p>\u5f53\u6a21\u578b\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u6267\u884c\u5b83\u5e76\u8fd4\u56de\u7ed3\u679c\u3002 \u7531\u4e8e\u6a21\u578b\u54cd\u5e94\u53ef\u80fd\u5305\u542b\u96f6\u4e2a\u3001\u4e00\u4e2a\u6216\u591a\u4e2a\u8c03\u7528\uff0c\u56e0\u6b64\u6700\u4f73\u5b9e\u8df5\u662f\u5047\u8bbe\u6709\u591a\u4e2a\u8c03\u7528\u3002<\/p>\n<p>\u54cd\u5e94\u5305\u542b\u4e00\u4e2a\u00a0<strong>tool_calls<\/strong>\u00a0\u6570\u7ec4\uff0c\u6bcf\u4e2a\u6570\u7ec4\u90fd\u6709\u4e00\u4e2a\u00a0<strong>id<\/strong>\uff08\u7a0d\u540e\u7528\u4e8e\u63d0\u4ea4\u51fd\u6570\u7ed3\u679c\uff09\u548c\u4e00\u4e2a\u5305\u542b\u00a0<strong>name<\/strong>\u00a0\u548c JSON \u7f16\u7801\u7684\u00a0<strong>arguments<\/strong>\u00a0\u7684\u00a0<strong>function<\/strong>\u3002<\/p>\n<p><strong>\u5305\u542b\u591a\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u793a\u4f8b\u54cd\u5e94<\/strong><\/p>\n<pre><code>[\r\n{\r\n\"id\": \"call_12345xyz\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"arguments\": \"{\\\"location\\\":\\\"Paris, France\\\"}\"\r\n}\r\n},\r\n{\r\n\"id\": \"call_67890abc\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"arguments\": \"{\\\"location\\\":\\\"Bogot\u00e1, Colombia\\\"}\"\r\n}\r\n},\r\n{\r\n\"id\": \"call_99999def\",\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"send_email\",\r\n\"arguments\": \"{\\\"to\\\":\\\"bob@email.com\\\",\\\"body\\\":\\\"Hi bob\\\"}\"\r\n}\r\n}\r\n]\r\n<\/code><\/pre>\n<p><strong>\u6267\u884c\u51fd\u6570\u8c03\u7528\u5e76\u9644\u52a0\u7ed3\u679c<\/strong><\/p>\n<pre><code>for tool_call in completion.choices[0].message.tool_calls:\r\nname = tool_call.function.name\r\nargs = json.loads(tool_call.function.arguments)\r\nresult = call_function(name, args)\r\nmessages.append({\r\n\"role\": \"tool\",\r\n\"tool_call_id\": tool_call.id,\r\n\"content\": result\r\n})\r\n<\/code><\/pre>\n<p><strong>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5047\u8bbe\u7684\u00a0call_function\u00a0\u6765\u8def\u7531\u6bcf\u4e2a\u8c03\u7528\u3002 \u8fd9\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>\u51fd\u6570\u8c03\u7528\u6267\u884c\u548c\u7ed3\u679c\u8ffd\u52a0<\/p>\n<pre><code>def call_function(name, args):\r\nif name == \"get_weather\":\r\nreturn get_weather(**args)\r\nif name == \"send_email\":\r\nreturn send_email(**args)\r\n<\/code><\/pre>\n<h3>\u683c\u5f0f\u5316\u7ed3\u679c<\/h3>\n<p>\u7ed3\u679c\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\uff0c\u4f46\u683c\u5f0f\u7531\u4f60\u51b3\u5b9a\uff08JSON\u3001\u9519\u8bef\u4ee3\u7801\u3001\u7eaf\u6587\u672c\u7b49\uff09\u3002 \u6a21\u578b\u5c06\u6839\u636e\u9700\u8981\u89e3\u91ca\u8be5\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u4f8b\u5982\u00a0<strong>send_email<\/strong>\uff09\uff0c\u53ea\u9700\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u6307\u793a\u6210\u529f\u6216\u5931\u8d25\u3002\uff08\u4f8b\u5982\u00a0<strong>&#8220;success&#8221;<\/strong>\uff09<\/p>\n<h3>\u5c06\u7ed3\u679c\u6574\u5408\u5230\u56de\u590d\u4e2d<\/h3>\n<p>\u5c06\u7ed3\u679c\u9644\u52a0\u5230\u4f60\u7684\u00a0<strong>messages<\/strong>\u00a0\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u4eec\u53d1\u56de\u6a21\u578b\u4ee5\u83b7\u5f97\u6700\u7ec8\u56de\u590d\u3002<\/p>\n<p>\u5c06\u7ed3\u679c\u53d1\u56de\u6a21\u578b<\/p>\n<pre><code>completion = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=messages,\r\ntools=tools,\r\n)\r\n<\/code><\/pre>\n<p><strong>\u6700\u7ec8\u56de\u590d<\/strong><\/p>\n<pre><code>\"\u5df4\u9ece\u5927\u7ea6 15\u00b0C\uff0cBogot\u00e1 \u5927\u7ea6 18\u00b0C\uff0c\u5e76\u4e14\u6211\u5df2\u7ecf\u5411 Bob \u53d1\u9001\u4e86\u90a3\u5c01\u90ae\u4ef6\u3002\"\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u9644\u52a0\u914d\u7f6e<\/h2>\n<h3>\u5de5\u5177\u9009\u62e9<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6a21\u578b\u5c06\u786e\u5b9a\u4f55\u65f6\u4ee5\u53ca\u4f7f\u7528\u591a\u5c11\u5de5\u5177\u3002 \u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0<strong>tool_choice<\/strong>\u00a0\u53c2\u6570\u5f3a\u5236\u6267\u884c\u7279\u5b9a\u884c\u4e3a\u3002<\/p>\n<ul>\n<li>Auto\uff1a\uff08<strong>\u9ed8\u8ba4<\/strong>\uff09\u8c03\u7528\u96f6\u4e2a\u3001\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u3002 tool_choice: &#8220;auto&#8221;<\/li>\n<li>Required\uff1a\u8c03\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u3002 tool_choice: &#8220;required&#8221;<\/li>\n<li>Forced Function\uff1a\u7cbe\u786e\u8c03\u7528\u4e00\u4e2a\u7279\u5b9a\u51fd\u6570\u3002 tool_choice: {&#8220;type&#8221;: &#8220;function&#8221;, &#8220;function&#8221;: {&#8220;name&#8221;: &#8220;get_weather&#8221;}}<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-20514\" title=\"\u51fd\u6570\u8c03\u7528\uff08Function calling\uff09-2\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803.png\" alt=\"\u51fd\u6570\u8c03\u7528\uff08Function calling\uff09-2\" width=\"777\" height=\"417\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803.png 3296w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803-768x412.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803-1536x824.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803-2048x1099.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/a61fef5fc5b9803-18x10.png 18w\" sizes=\"auto, (max-width: 777px) 100vw, 777px\" \/><\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u5c06\u00a0<strong>tool_choice<\/strong>\u00a0\u8bbe\u7f6e\u4e3a\u00a0<strong>&#8220;none&#8221;<\/strong>\uff0c\u4ee5\u6a21\u62df\u4e0d\u4f20\u9012\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<h3>\u5e76\u884c\u51fd\u6570\u8c03\u7528<\/h3>\n<p>\u6a21\u578b\u53ef\u80fd\u4f1a\u9009\u62e9\u5728\u4e00\u4e2a\u56de\u5408\u4e2d\u8c03\u7528\u591a\u4e2a\u51fd\u6570\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06\u00a0<strong>parallel_tool_calls<\/strong>\u00a0\u8bbe\u7f6e\u4e3a\u00a0<strong>false<\/strong>\u00a0\u6765\u963b\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u8fd9\u5c06\u786e\u4fdd\u7cbe\u786e\u5730\u8c03\u7528\u96f6\u4e2a\u6216\u4e00\u4e2a\u5de5\u5177\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u00a0\u76ee\u524d\uff0c\u5982\u679c\u6a21\u578b\u5728\u4e00\u4e2a\u56de\u5408\u4e2d\u8c03\u7528\u591a\u4e2a\u51fd\u6570\uff0c\u5219 strict modev\u5c06\u5bf9\u8fd9\u4e9b\u8c03\u7528\u7981\u7528\u3002<\/p>\n<h3>\u4e25\u683c\u6a21\u5f0f<\/h3>\n<p>\u5c06\u00a0<strong>strict<\/strong>\u00a0\u8bbe\u7f6e\u4e3a\u00a0<strong>true<\/strong>\u00a0\u5c06\u786e\u4fdd\u51fd\u6570\u8c03\u7528\u53ef\u9760\u5730\u9075\u5b88\u51fd\u6570\u6a21\u5f0f\uff0c\u800c\u4e0d\u662f\u5c3d\u529b\u800c\u4e3a\u3002 \u6211\u4eec\u5efa\u8bae\u59cb\u7ec8\u542f\u7528\u4e25\u683c\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u5728\u5e95\u5c42\uff0c\u4e25\u683c\u6a21\u5f0f\u901a\u8fc7\u5229\u7528\u6211\u4eec\u7684 structured outputs \u529f\u80fd\u6765\u5b9e\u73b0\uff0c\u56e0\u6b64\u5f15\u5165\u4e86\u51e0\u4e2a\u8981\u6c42\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u00a0<strong>parameters<\/strong>\u00a0\u4e2d\u7684\u6bcf\u4e2a\u5bf9\u8c61\uff0c<strong>additionalProperties<\/strong>\u00a0\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u00a0<strong>false<\/strong>\u3002<\/li>\n<li>properties \u4e2d\u7684\u6240\u6709\u5b57\u6bb5\u90fd\u5fc5\u987b\u6807\u8bb0\u4e3a <strong>required<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u00a0<strong>null<\/strong>\u00a0\u4f5c\u4e3a\u00a0<strong>type<\/strong>\u00a0\u9009\u9879\u6765\u8868\u793a\u53ef\u9009\u5b57\u6bb5\uff08\u8bf7\u53c2\u9605\u4e0b\u9762\u7684\u793a\u4f8b\uff09\u3002<\/p>\n<p><strong>\u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/p>\n<pre><code>{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u68c0\u7d22\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u5f53\u524d\u5929\u6c14\u3002\",\r\n\"strict\": true,\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"location\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n},\r\n\"units\": {\r\n\"type\": [\"string\", \"null\"],\r\n\"enum\": [\"celsius\", \"fahrenheit\"],\r\n\"description\": \"\u6e29\u5ea6\u5c06\u4ee5\u4ec0\u4e48\u5355\u4f4d\u8fd4\u56de\u3002\"\r\n}\r\n},\r\n\"required\": [\"location\", \"units\"],\r\n\"additionalProperties\": False\r\n}\r\n}\r\n}\r\n<\/code><\/pre>\n<p><strong>\u7981\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/p>\n<pre><code>{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u68c0\u7d22\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u5f53\u524d\u5929\u6c14\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"location\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n},\r\n\"units\": {\r\n\"type\": \"string\",\r\n\"enum\": [\"celsius\", \"fahrenheit\"],\r\n\"description\": \"\u6e29\u5ea6\u5c06\u4ee5\u4ec0\u4e48\u5355\u4f4d\u8fd4\u56de\u3002\"\r\n}\r\n},\r\n\"required\": [\"location\"],\r\n}\r\n}\r\n}\r\n<\/code><\/pre>\n<p>playground\u4e2d\u751f\u6210\u7684\u6240\u6709\u6a21\u5f0f\u90fd\u542f\u7528\u4e86\u4e25\u683c\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u867d\u7136\u6211\u4eec\u5efa\u8bae\u4f60\u542f\u7528\u4e25\u683c\u6a21\u5f0f\uff0c\u4f46\u5b83\u6709\u4e00\u4e9b\u9650\u5236\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e0d\u652f\u6301 JSON schema \u7684\u67d0\u4e9b\u529f\u80fd\u3002\uff08\u8bf7\u53c2\u9605 supported schemas\uff09<\/li>\n<li>\u6a21\u5f0f\u5728\u9996\u6b21\u8bf7\u6c42\u65f6\u4f1a\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\uff08\u7136\u540e\u4f1a\u88ab\u7f13\u5b58\uff09\u3002 \u5982\u679c\u4f60\u7684\u6a21\u5f0f\u56e0\u8bf7\u6c42\u800c\u5f02\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u66f4\u9ad8\u7684\u5ef6\u8fdf\u3002<\/li>\n<li>\u6a21\u5f0f\u4e3a\u4e86\u6027\u80fd\u800c\u88ab\u7f13\u5b58\uff0c\u5e76\u4e14\u4e0d\u7b26\u5408 zero data retention\u7684\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u6d41\u5f0f\u4f20\u8f93<\/h2>\n<p>\u6d41\u5f0f\u4f20\u8f93\u53ef\u7528\u4e8e\u663e\u793a\u8fdb\u5ea6\uff0c\u901a\u8fc7\u5c55\u793a\u5728\u6a21\u578b\u586b\u5145\u5176\u53c2\u6570\u65f6\u8c03\u7528\u7684\u51fd\u6570\uff0c\u751a\u81f3\u5b9e\u65f6\u663e\u793a\u53c2\u6570\u3002<\/p>\n<p>\u6d41\u5f0f\u4f20\u8f93\u51fd\u6570\u8c03\u7528\u4e0e\u6d41\u5f0f\u4f20\u8f93\u5e38\u89c4\u54cd\u5e94\u975e\u5e38\u76f8\u4f3c\uff1a\u4f60\u5c06\u00a0<strong>stream<\/strong>\u00a0\u8bbe\u7f6e\u4e3a\u00a0<strong>true<\/strong>\u00a0\u5e76\u83b7\u53d6\u5e26\u6709\u00a0<strong>delta<\/strong>\u00a0\u5bf9\u8c61\u7684\u5757\u3002<\/p>\n<p><strong>\u6d41\u5f0f\u4f20\u8f93\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<pre><code>from openai import OpenAI\r\nclient = OpenAI()\r\ntools = [{\r\n\"type\": \"function\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"description\": \"\u83b7\u53d6\u7ed9\u5b9a\u4f4d\u7f6e\u7684\u5f53\u524d\u6e29\u5ea6\u3002\",\r\n\"parameters\": {\r\n\"type\": \"object\",\r\n\"properties\": {\r\n\"location\": {\r\n\"type\": \"string\",\r\n\"description\": \"\u57ce\u5e02\u548c\u56fd\u5bb6\uff0c\u4f8b\u5982 Bogot\u00e1, Colombia\"\r\n}\r\n},\r\n\"required\": [\"location\"],\r\n\"additionalProperties\": False\r\n},\r\n\"strict\": True\r\n}\r\n}]\r\nstream = client.chat.completions.create(\r\nmodel=\"gpt-4o\",\r\nmessages=[{\"role\": \"user\", \"content\": \"\u4eca\u5929\u5df4\u9ece\u7684\u5929\u6c14\u600e\u4e48\u6837\uff1f\"}],\r\ntools=tools,\r\nstream=True\r\n)\r\nfor chunk in stream:\r\ndelta = chunk.choices[0].delta\r\nprint(delta.tool_calls)\r\n<\/code><\/pre>\n<p><strong>\u8f93\u51fa delta.tool_calls<\/strong><\/p>\n<pre><code>[{\"index\": 0, \"id\": \"call_DdmO9pD3xa9XTPNJ32zg2hcA\", \"function\": {\"arguments\": \"\", \"name\": \"get_weather\"}, \"type\": \"function\"}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \"{\\\"\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \"location\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \"\\\":\\\"\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \"Paris\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \",\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \" France\", \"name\": null}, \"type\": null}]\r\n[{\"index\": 0, \"id\": null, \"function\": {\"arguments\": \"\\\"}\", \"name\": null}, \"type\": null}]\r\nnull\r\n<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u4f60\u4e0d\u662f\u5c06\u5757\u805a\u5408\u5230\u5355\u4e2a\u00a0<strong>content<\/strong>\u00a0\u5b57\u7b26\u4e32\u4e2d\uff0c\u800c\u662f\u5c06\u5757\u805a\u5408\u5230\u7f16\u7801\u7684\u00a0<strong>arguments<\/strong>\u00a0JSON \u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u5f53\u6a21\u578b\u8c03\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u65f6\uff0c\u6bcf\u4e2a\u00a0<strong>delta<\/strong>\u00a0\u7684\u00a0<strong>tool_calls<\/strong>\u00a0\u5b57\u6bb5\u5c06\u88ab\u586b\u5145\u3002 \u6bcf\u4e2a\u00a0<strong>tool_call<\/strong>\u00a0\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>\u5b57\u6bb5<\/strong><\/th>\n<th><strong>\u63cf\u8ff0<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>index<\/strong><\/td>\n<td><strong>\u6807\u8bc6 delta \u5bf9\u5e94\u7684\u51fd\u6570\u8c03\u7528<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>id<\/strong><\/td>\n<td><strong>\u5de5\u5177\u8c03\u7528 ID\u3002<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>function<\/strong><\/td>\n<td><strong>\u51fd\u6570\u8c03\u7528 delta\uff08\u540d\u79f0\u548c\u53c2\u6570\uff09<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>type<\/strong><\/td>\n<td><strong>tool_call \u7684\u7c7b\u578b\uff08\u5bf9\u4e8e\u51fd\u6570\u8c03\u7528\u59cb\u7ec8\u4e3a function\uff09<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u5b57\u6bb5\u4e2d\u7684\u8bb8\u591a\u5b57\u6bb5\u4ec5\u5728\u6bcf\u4e2a\u5de5\u5177\u8c03\u7528\u7684\u7b2c\u4e00\u4e2a\u00a0<strong>delta<\/strong>\u00a0\u4e2d\u8bbe\u7f6e\uff0c\u4f8b\u5982\u00a0<strong>id<\/strong>\u3001<strong>function.name<\/strong>\u00a0\u548c\u00a0<strong>type<\/strong>\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5c06\u00a0<strong>delta<\/strong>\u00a0\u805a\u5408\u5230\u6700\u7ec8\u7684\u00a0<strong>tool_calls<\/strong>\u00a0\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p><strong>\u7d2f\u79ef tool_call delta<\/strong><\/p>\n<pre><code>final_tool_calls = {}\r\nfor chunk in stream:\r\nfor tool_call in chunk.choices[0].delta.tool_calls or []:\r\nindex = tool_call.index\r\nif index not in final_tool_calls:\r\nfinal_tool_calls[index] = tool_call\r\nfinal_tool_calls[index].function.arguments += tool_call.function.arguments\r\n<\/code><\/pre>\n<p><strong>\u7d2f\u79ef\u7684 final_tool_calls[0]<\/strong><\/p>\n<pre><code>{\r\n\"index\": 0,\r\n\"id\": \"call_RzfkBpJgzeR0S242qfvjadNe\",\r\n\"function\": {\r\n\"name\": \"get_weather\",\r\n\"arguments\": \"{\\\"location\\\":\\\"Paris, France\\\"}\"\r\n}\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OpenAI Function calling V2 \u7279\u70b9 Function calling V2 \u7684\u6838\u5fc3\u76ee\u6807\u662f\u8d4b\u4e88 OpenAI \u6a21\u578b\u4e0e\u5916\u90e8\u4e16\u754c\u4ea4\u4e92\u7684\u80fd\u529b\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u6838\u5fc3\u529f\u80fd\uff1a \u6570\u636e\u83b7\u53d6 (Fetching Data) &#038;#821&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-20513","post","type-post","status-publish","format-standard","hentry","category-knowledge"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/20513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=20513"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/20513\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=20513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=20513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=20513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}