{"id":128,"date":"2026-07-23T23:41:02","date_gmt":"2026-07-23T15:41:02","guid":{"rendered":"https:\/\/fsdata.site\/?p=128"},"modified":"2026-07-23T23:41:02","modified_gmt":"2026-07-23T15:41:02","slug":"ai-agent-function-calling","status":"publish","type":"post","link":"https:\/\/fsdata.site\/?p=128","title":{"rendered":"AI Agent \u5f00\u53d1\u5b9e\u6218\uff1a\u4ece Function Calling \u5230\u5de5\u5177\u8c03\u7528"},"content":{"rendered":"<h2>\u4ec0\u4e48\u662f Function Calling<\/h2>\n<p>\u5927\u6a21\u578b\u672c\u8eab\u53ea\u4f1a&#8221;\u8bf4\u8bdd&#8221;\uff0c\u4e0d\u4f1a&#8221;\u505a\u4e8b&#8221;\u3002Function Calling\uff08\u51fd\u6570\u8c03\u7528\uff09\u662f\u8ba9\u6a21\u578b\u5728\u56de\u7b54\u65f6\uff0c\u80fd\u591f<strong>\u8f93\u51fa\u4e00\u4e2a\u7ed3\u6784\u5316\u7684&#8221;\u6211\u60f3\u8c03\u7528\u54ea\u4e2a\u51fd\u6570\u3001\u53c2\u6570\u662f\u4ec0\u4e48&#8221;<\/strong>\uff0c\u7531\u4f60\u7684\u7a0b\u5e8f\u771f\u6b63\u53bb\u6267\u884c\uff0c\u518d\u628a\u7ed3\u679c\u5582\u56de\u6a21\u578b\u7ee7\u7eed\u63a8\u7406\u3002\u8fd9\u5c31\u662f AI Agent \u6700\u57fa\u7840\u7684\u80fd\u529b\u95ed\u73af\u3002<\/p>\n<h2>\u4e00\u3001\u4e00\u6b21\u5b8c\u6574\u7684 Agent \u8c03\u7528\u5faa\u73af<\/h2>\n<ol>\n<li>\u4f60\u5b9a\u4e49\u4e00\u4e2a\u5de5\u5177\u5217\u8868\uff08\u51fd\u6570\u7684\u540d\u5b57\u3001\u63cf\u8ff0\u3001\u53c2\u6570 JSON Schema\uff09\u3002<\/li>\n<li>\u628a\u7528\u6237\u95ee\u9898 + \u5de5\u5177\u5217\u8868\u53d1\u7ed9\u6a21\u578b\u3002<\/li>\n<li>\u6a21\u578b\u8fd4\u56de&#8221;\u6211\u8981\u8c03\u7528 <code>get_weather(city=\"\u5317\u4eac\")<\/code>&#8220;\u3002<\/li>\n<li>\u4f60\u7684\u4ee3\u7801\u771f\u7684\u53bb\u67e5\u5929\u6c14\uff0c\u62ff\u5230\u7ed3\u679c\u3002<\/li>\n<li>\u628a\u7ed3\u679c\u4f5c\u4e3a\u65b0\u6d88\u606f\u53d1\u56de\u6a21\u578b\uff0c\u6a21\u578b\u751f\u6210\u6700\u7ec8\u81ea\u7136\u8bed\u8a00\u56de\u7b54\u3002<\/li>\n<\/ol>\n<h2>\u4e8c\u3001\u5b9a\u4e49\u5de5\u5177\uff08\u4ee5 OpenAI \u517c\u5bb9\u683c\u5f0f\u4e3a\u4f8b\uff09<\/h2>\n<pre><code>tools = [\n  {\n    \"type\": \"function\",\n    \"function\": {\n      \"name\": \"get_weather\",\n      \"description\": \"\u67e5\u8be2\u6307\u5b9a\u57ce\u5e02\u7684\u5f53\u524d\u5929\u6c14\",\n      \"parameters\": {\n        \"type\": \"object\",\n        \"properties\": {\n          \"city\": {\"type\": \"string\", \"description\": \"\u57ce\u5e02\u540d\uff0c\u5982 \u5317\u4eac\"}\n        },\n        \"required\": [\"city\"]\n      }\n    }\n  }\n]\n<\/code><\/pre>\n<h2>\u4e09\u3001\u5faa\u73af\u6267\u884c\uff08Python \u793a\u4f8b\uff09<\/h2>\n<pre><code>import requests, json\n\nAPI = \"https:\/\/apihub.agnes-ai.com\/v1\/chat\/completions\"\nHEADERS = {\"Authorization\": \"Bearer &lt;\u4f60\u7684KEY&gt;\", \"Content-Type\": \"application\/json\"}\n\nmessages = [{\"role\": \"user\", \"content\": \"\u5317\u4eac\u4eca\u5929\u9002\u5408\u51fa\u95e8\u5417\uff1f\"}]\n\nresp = requests.post(API, headers=HEADERS, json={\n    \"model\": \"agnes-2.0-flash\",\n    \"messages\": messages,\n    \"tools\": tools\n}).json()\n\nmsg = resp[\"choices\"][0][\"message\"]\nif msg.get(\"tool_calls\"):\n    for tc in msg[\"tool_calls\"]:\n        fn = tc[\"function\"]\n        args = json.loads(fn[\"arguments\"])\n        # \u771f\u5b9e\u6267\u884c\u5de5\u5177\n        result = get_weather(args[\"city\"])\n        # \u628a\u7ed3\u679c\u585e\u56de\u5bf9\u8bdd\uff0c\u8ba9\u6a21\u578b\u7ee7\u7eed\n        messages.append(msg)\n        messages.append({\n            \"role\": \"tool\",\n            \"tool_call_id\": tc[\"id\"],\n            \"content\": result\n        })\n\nfinal = requests.post(API, headers=HEADERS, json={\n    \"model\": \"agnes-2.0-flash\", \"messages\": messages\n}).json()\nprint(final[\"choices\"][0][\"message\"][\"content\"])\n<\/code><\/pre>\n<h2>\u56db\u3001\u4ece\u5355\u5de5\u5177\u5230\u591a\u5de5\u5177 Agent<\/h2>\n<p>\u771f\u5b9e\u7684 Agent \u5f80\u5f80\u6709\u591a\u4e2a\u5de5\u5177\uff1a\u67e5\u6570\u636e\u5e93\u3001\u7b97\u6570\u3001\u641c\u7f51\u9875\u3001\u53d1\u90ae\u4ef6\u3002\u6a21\u578b\u4f1a\u6839\u636e\u95ee\u9898\u81ea\u4e3b\u9009\u62e9\u8c03\u7528\u987a\u5e8f\uff0c\u751a\u81f3<strong>\u591a\u8f6e\u4e32\u8054<\/strong>\uff08\u5148\u67e5\u8ba2\u5355\u3001\u518d\u67e5\u5e93\u5b58\u3001\u6700\u540e\u5224\u65ad\u662f\u5426\u53d1\u8d27\uff09\u3002\u4f60\u53ea\u9700\u4e0d\u65ad\u628a <code>tool<\/code> \u7ed3\u679c\u56de\u704c\uff0c\u76f4\u5230\u6a21\u578b\u4e0d\u518d\u8fd4\u56de <code>tool_calls<\/code> \u4e3a\u6b62\u3002<\/p>\n<h2>\u4e94\u3001\u5fc5\u987b\u6ce8\u610f\u7684\u5b89\u5168\u95ee\u9898<\/h2>\n<ul>\n<li><strong>\u4e0d\u8981\u628a\u5de5\u5177\u6267\u884c\u7ed3\u679c\u76f4\u63a5\u5f53\u4ee3\u7801\u8dd1<\/strong>\uff08\u9632\u6b62 prompt injection \u89e6\u53d1\u5371\u9669\u64cd\u4f5c\uff09\u3002<\/li>\n<li>\u7ed9\u5de5\u5177\u52a0<strong>\u767d\u540d\u5355\u548c\u6743\u9650\u8fb9\u754c<\/strong>\uff0c\u6bd4\u5982&#8221;\u53d1\u90ae\u4ef6&#8221;\u5de5\u5177\u53ea\u5141\u8bb8\u53d1\u7ed9\u56fa\u5b9a\u5730\u5740\u3002<\/li>\n<li>\u5bf9 <code>tool_calls<\/code> \u7684\u53c2\u6570\u505a<strong>\u6821\u9a8c\u4e0e\u8131\u654f<\/strong>\uff0c\u522b\u628a\u7528\u6237\u8f93\u5165\u539f\u6837\u585e\u8fdb shell\u3002<\/li>\n<li>\u5173\u952e\u64cd\u4f5c\uff08\u5220\u6570\u636e\u3001\u8f6c\u8d26\uff09\u8bbe<strong>\u4eba\u5de5\u786e\u8ba4<\/strong>\u73af\u8282\u3002<\/li>\n<\/ul>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>Function Calling \u662f AI Agent \u7684\u53d1\u52a8\u673a\u3002\u7406\u89e3\u4e86&#8221;\u6a21\u578b\u53ea\u51b3\u5b9a\u8c03\u4ec0\u4e48\u3001\u4f60\u7684\u4ee3\u7801\u624d\u771f\u6b63\u6267\u884c&#8221;\u8fd9\u4e2a\u5206\u5de5\uff0c\u4f60\u5c31\u80fd\u7528\u4efb\u610f\u5927\u6a21\u578b\uff08\u5305\u62ec\u79c1\u6709\u5316\u90e8\u7f72\u7684\uff09\u642d\u51fa\u4f1a\u67e5\u6570\u636e\u3001\u4f1a\u8c03\u63a5\u53e3\u3001\u4f1a\u5e72\u6d3b\u7684\u667a\u80fd\u4f53\u3002\u5b89\u5168\u548c\u8fb9\u754c\u63a7\u5236\uff0c\u662f\u5b83\u80fd\u4e0d\u80fd\u4e0a\u751f\u4ea7\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3\u6a21\u578b\u53ea\u51b3\u5b9a\u8c03\u4ec0\u4e48\u3001\u4ee3\u7801\u624d\u771f\u6b63\u6267\u884c\u7684\u5206\u5de5\uff0c\u7528 Function Calling \u642d\u51fa\u4f1a\u5e72\u6d3b\u7684\u667a\u80fd\u4f53\u3002<\/p>\n","protected":false},"author":2,"featured_media":118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai-app"],"_links":{"self":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":0,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/media\/118"}],"wp:attachment":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}