{"id":25,"date":"2026-11-08T13:16:01","date_gmt":"2026-11-08T13:16:01","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=25"},"modified":"2026-11-08T13:16:01","modified_gmt":"2026-11-08T13:16:01","slug":"%e7%94%a8-langchain-%e8%87%aa%e7%a0%94%e6%a1%86%e6%9e%b6%e6%90%ad%e4%b8%80%e4%b8%aa%e8%83%bd%e6%9f%a5%e5%ba%93%e7%9a%84-agent","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=25","title":{"rendered":"\u7528 LangChain \/ \u81ea\u7814\u6846\u67b6\u642d\u4e00\u4e2a\u80fd\u67e5\u5e93\u7684 Agent"},"content":{"rendered":"<p>\u5f88\u591a\u6559\u7a0b\u4e00\u4e0a\u6765\u5806\u6846\u67b6\u3002\u5176\u5b9e\u4f60\u4e5f\u53ef\u4ee5\u81ea\u7814\u4e00\u4e2a\u300cLLM + \u5de5\u5177\u6ce8\u518c\u8868 + \u5faa\u73af\u300d\u7684\u5c0f\u5185\u6838\u3002\u672c\u6587\u7ed9\u4e24\u79cd\u8def\u7ebf\uff1a\u81ea\u7814\u6700\u5c0f\u6838\uff0c\u4ee5\u53ca LangChain \u5feb\u901f\u62fc\u88c5\u3002<\/p>\n<h2>\u81ea\u7814\u6700\u5c0f\u6838<\/h2>\n<pre><code>TOOL_REGISTRY = {}\n\ndef tool(name, desc, schema):\n    def deco(fn):\n        TOOL_REGISTRY[name] = {\"fn\": fn, \"desc\": desc, \"schema\": schema}\n        return fn\n    return deco\n\n@tool(\"sql_query\", \"\u53ea\u8bfb\u67e5\u8be2\u4e1a\u52a1\u5e93\", {\"sql\": \"string\"})\ndef sql_query(sql: str) -&gt; str:\n    assert sql.strip().lower().startswith(\"select\")\n    # \u6267\u884c\u53ea\u8bfb SQL \u5e76\u8fd4\u56de markdown \u8868\n    return run_readonly_sql(sql)\n<\/code><\/pre>\n<p>\u5173\u952e\u5b89\u5168\uff1aAgent \u67e5\u5e93\u9ed8\u8ba4<strong>\u53ea\u8bfb\u8d26\u53f7<\/strong>\uff0c\u5e76\u62e6\u622a\u975e SELECT\u3002<\/p>\n<h2>LangChain \u601d\u8def\uff08\u6982\u5ff5\uff09<\/h2>\n<pre><code># \u4f2a\u4ee3\u7801\nfrom langchain.agents import create_tool_calling_agent, AgentExecutor\nfrom langchain_core.tools import tool\n\n@tool\ndef get_course_price(course_name: str) -&gt; str:\n    \"\"\"\u67e5\u8be2\u8bfe\u7a0b\u4ef7\u683c\"\"\"\n    return query_price(course_name)\n\nagent = create_tool_calling_agent(llm, [get_course_price], prompt)\nexecutor = AgentExecutor(agent=agent, tools=[get_course_price])\nprint(executor.invoke({\"input\": \"Python\u722c\u866b\u8bfe\u591a\u5c11\u94b1\uff1f\"}))\n<\/code><\/pre>\n<h2>\u4ec0\u4e48\u65f6\u5019\u81ea\u7814 \/ \u7528\u6846\u67b6<\/h2>\n<ul>\n<li>\u5b66\u4e60\u539f\u7406\u3001\u63a7\u5236\u529b\u8981\u6c42\u9ad8 \u2192 \u81ea\u7814\u5c0f\u6838<\/li>\n<li>\u5feb\u901f\u9a8c\u8bc1\u3001\u5de5\u5177\u591a\u3001\u94fe\u8def\u957f \u2192 \u6846\u67b6\u66f4\u7701\u4e8b<\/li>\n<li>\u4e0a\u7ebf\u540e\u90fd\u8981\u8865\uff1a\u8d85\u65f6\u3001\u91cd\u8bd5\u3001\u8f68\u8ff9\u65e5\u5fd7\u3001\u4eba\u5de5\u63a5\u7ba1<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u6559\u7a0b\u4e00\u4e0a\u6765\u5806\u6846\u67b6\u3002\u5176\u5b9e\u4f60\u4e5f\u53ef\u4ee5\u81ea\u7814\u4e00\u4e2a\u300cLLM + \u5de5\u5177\u6ce8\u518c\u8868 + \u5faa\u73af\u300d\u7684\u5c0f\u5185\u6838\u3002\u672c\u6587\u7ed9\u4e24\u79cd\u8def\u7ebf\uff1a\u81ea\u7814\u6700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-agent-dev"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}