{"id":539,"date":"2026-10-07T14:53:47","date_gmt":"2026-10-07T06:53:47","guid":{"rendered":"https:\/\/www.linerroom.cn\/?p=539"},"modified":"2026-10-07T14:53:47","modified_gmt":"2026-10-07T06:53:47","slug":"langchain-create_agent%e4%bd%a0%e6%98%af%e5%90%a6%e7%9f%a5%e9%81%93%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.linerroom.cn\/?p=539","title":{"rendered":"LangChain `create_agent`\u4f60\u662f\u5426\u77e5\u9053\uff1f"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h1>LangChain <code>create_agent<\/code> \u8be6\u89e3<\/h1>\n<blockquote>\n<p><code>create_agent<\/code> \u662f LangChain \u7528\u6765\u521b\u5efa Agent \u7684\u5de5\u5382\u51fd\u6570\u3002\u5b83\u628a\u8bed\u8a00\u6a21\u578b\u3001\u5de5\u5177\u3001\u63d0\u793a\u8bcd\u3001\u72b6\u6001\u3001\u6301\u4e45\u5316\u548c\u4e2d\u95f4\u4ef6\u7ec4\u5408\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7684 Agent \u56fe\u3002<\/p>\n<\/blockquote>\n<h2>1. <code>create_agent<\/code> \u662f\u505a\u4ec0\u4e48\u7684<\/h2>\n<p>\u4ece\u529f\u80fd\u4e0a\u770b\uff0c<code>create_agent<\/code> \u7528\u4e8e\u521b\u5efa\u80fd\u591f\u53cd\u590d\u6267\u884c\u201c\u6a21\u578b\u5224\u65ad \u2192 \u8c03\u7528\u5de5\u5177 \u2192 \u83b7\u53d6\u7ed3\u679c \u2192 \u518d\u6b21\u5224\u65ad\u201d\u7684 Agent\u3002<\/p>\n<pre><code class=\"language-text\">\u7528\u6237\u95ee\u9898\n   \u2193\n\u8bed\u8a00\u6a21\u578b\u5224\u65ad\u662f\u5426\u9700\u8981\u5de5\u5177\n   \u2193\n\u9700\u8981\u5de5\u5177 \u2500\u2500\u2192 \u6267\u884c\u5de5\u5177 \u2500\u2500\u2192 \u5de5\u5177\u7ed3\u679c\u52a0\u5165\u6d88\u606f\n   \u2502                            \u2502\n   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 \u8bed\u8a00\u6a21\u578b\u518d\u6b21\u5224\u65ad \u2190\u2500\u2500\u2518\n                \u2193\n          \u6700\u7ec8\u56de\u7b54\n<\/code><\/pre>\n<p>\u5f53\u524d\u5b98\u65b9 API \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u540e\u7684 Agent \u56fe\uff0c\u7c7b\u578b\u662f <code>CompiledStateGraph<\/code>\u3002\u5f53\u6a21\u578b\u8fd4\u56de\u5de5\u5177\u8c03\u7528\u65f6\uff0cAgent \u4f1a\u6267\u884c\u5de5\u5177\uff0c\u5e76\u628a\u5de5\u5177\u7ed3\u679c\u4f5c\u4e3a <code>ToolMessage<\/code> \u52a0\u56de\u6d88\u606f\u5217\u8868\uff0c\u7136\u540e\u7ee7\u7eed\u8c03\u7528\u6a21\u578b\uff1b\u5f53\u6a21\u578b\u4e0d\u518d\u8bf7\u6c42\u5de5\u5177\u65f6\uff0c\u6d41\u7a0b\u7ed3\u675f\u3002<a href=\"https:\/\/reference.langchain.com\/python\/langchain\/agents\/factory\/create_agent\">LangChain \u5b98\u65b9 API Reference<\/a><\/p>\n<h2>2. \u51fd\u6570\u7b7e\u540d<\/h2>\n<p>\u9898\u76ee\u4e2d\u7684\u51fd\u6570\u7b7e\u540d\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u7406\u89e3\uff1a<\/p>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\n\nagent = create_agent(\n    model,                     # str | BaseChatModel\uff1a\u8bed\u8a00\u6a21\u578b\n    tools=None,                # Sequence\uff1a\u5de5\u5177\u5217\u8868\n    *,\n    system_prompt=None,        # str | SystemMessage\uff1a\u7cfb\u7edf\u63d0\u793a\n    middleware=(),             # Sequence[AgentMiddleware]\uff1a\u4e2d\u95f4\u4ef6\u5217\u8868\n    response_format=None,      # ResponseFormat | type\uff1a\u7ed3\u6784\u5316\u8f93\u51fa\u914d\u7f6e\n    state_schema=None,         # type[AgentState]\uff1a\u81ea\u5b9a\u4e49\u72b6\u6001\u7ed3\u6784\n    context_schema=None,       # \u8fd0\u884c\u65f6\u4e0a\u4e0b\u6587\u7ed3\u6784\n    checkpointer=None,         # Checkpointer\uff1a\u5bf9\u8bdd\u6301\u4e45\u5316\n    store=None,                # BaseStore\uff1a\u8de8\u4f1a\u8bdd\u5b58\u50a8\n    interrupt_before=None,     # list[str]\uff1a\u5728\u54ea\u4e9b\u8282\u70b9\u524d\u6682\u505c\n    interrupt_after=None,      # list[str]\uff1a\u5728\u54ea\u4e9b\u8282\u70b9\u540e\u6682\u505c\n    debug=False,               # bool\uff1a\u662f\u5426\u8f93\u51fa\u8be6\u7ec6\u65e5\u5fd7\n    name=None,                 # str\uff1aAgent \u540d\u79f0\n    cache=None,                # BaseCache\uff1a\u7f13\u5b58\u914d\u7f6e\n    transformers=None,         # \u5f53\u524d\u7248\u672c\u989d\u5916\u652f\u6301\u7684\u6d41\u8f6c\u6362\u5668\n)\n<\/code><\/pre>\n<p><code>transformers<\/code> \u672a\u51fa\u73b0\u5728\u9898\u76ee\u7ed9\u51fa\u7684\u7b7e\u540d\u4e2d\uff0c\u4f46\u5f53\u524d\u5b98\u65b9\u53c2\u8003\u6587\u6863\u7684\u7b7e\u540d\u5df2\u7ecf\u5305\u542b\u8be5\u53c2\u6570\u3002\u56e0\u6b64\uff0c\u5728\u5177\u4f53\u9879\u76ee\u4e2d\u5e94\u4ee5\u5f53\u524d\u5b89\u88c5\u7248\u672c\u7684 API \u4e3a\u51c6\u3002<\/p>\n<h2>3. \u53c2\u6570\u603b\u89c8<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u5e38\u89c1\u7528\u9014<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>model<\/code><\/td>\n<td><code>str | BaseChatModel<\/code><\/td>\n<td>\u6307\u5b9a Agent \u4f7f\u7528\u7684\u8bed\u8a00\u6a21\u578b<\/td>\n<td>\u51b3\u5b9a\u7406\u89e3\u3001\u89c4\u5212\u548c\u56de\u7b54\u80fd\u529b<\/td>\n<\/tr>\n<tr>\n<td><code>tools<\/code><\/td>\n<td><code>Sequence<\/code> \u6216 <code>None<\/code><\/td>\n<td>\u63d0\u4f9b Agent \u53ef\u4ee5\u8c03\u7528\u7684\u5de5\u5177<\/td>\n<td>\u641c\u7d22\u3001\u6570\u636e\u5e93\u3001API\u3001\u8ba1\u7b97\u3001\u6587\u4ef6\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td><code>system_prompt<\/code><\/td>\n<td><code>str | SystemMessage | None<\/code><\/td>\n<td>\u8bbe\u7f6e Agent \u7684\u8eab\u4efd\u3001\u89c4\u5219\u548c\u884c\u4e3a\u8fb9\u754c<\/td>\n<td>\u89d2\u8272\u8bbe\u5b9a\u3001\u56de\u7b54\u89c4\u8303\u3001\u5b89\u5168\u7ea6\u675f<\/td>\n<\/tr>\n<tr>\n<td><code>middleware<\/code><\/td>\n<td><code>Sequence[AgentMiddleware]<\/code><\/td>\n<td>\u5728\u6a21\u578b\u8c03\u7528\u3001\u5de5\u5177\u8c03\u7528\u7b49\u73af\u8282\u63d2\u5165\u903b\u8f91<\/td>\n<td>\u65e5\u5fd7\u3001\u91cd\u8bd5\u3001\u6743\u9650\u3001\u9650\u6d41\u3001\u52a8\u6001\u63d0\u793a\u8bcd<\/td>\n<\/tr>\n<tr>\n<td><code>response_format<\/code><\/td>\n<td><code>ResponseFormat<\/code> \u6216\u7c7b\u578b<\/td>\n<td>\u8981\u6c42 Agent \u8fd4\u56de\u7ed3\u6784\u5316\u7ed3\u679c<\/td>\n<td>JSON\u3001Pydantic \u6a21\u578b\u3001\u5206\u7c7b\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td><code>state_schema<\/code><\/td>\n<td><code>type[AgentState]<\/code><\/td>\n<td>\u6269\u5c55 Agent \u7684\u8fd0\u884c\u72b6\u6001\u5b57\u6bb5<\/td>\n<td>\u4fdd\u5b58\u7528\u6237 ID\u3001\u4efb\u52a1\u72b6\u6001\u3001\u4e2d\u95f4\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td><code>context_schema<\/code><\/td>\n<td>\u7c7b\u578b<\/td>\n<td>\u5b9a\u4e49\u4e00\u6b21\u8fd0\u884c\u53ef\u4ee5\u4f20\u5165\u7684\u4e0a\u4e0b\u6587<\/td>\n<td>\u7528\u6237\u4fe1\u606f\u3001\u79df\u6237\u4fe1\u606f\u3001\u8fd0\u884c\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td><code>checkpointer<\/code><\/td>\n<td><code>Checkpointer<\/code><\/td>\n<td>\u4fdd\u5b58\u5355\u4e2a\u7ebf\u7a0b\u6216\u4f1a\u8bdd\u7684\u72b6\u6001<\/td>\n<td>\u591a\u8f6e\u5bf9\u8bdd\u3001\u6682\u505c\u540e\u6062\u590d<\/td>\n<\/tr>\n<tr>\n<td><code>store<\/code><\/td>\n<td><code>BaseStore<\/code><\/td>\n<td>\u4fdd\u5b58\u8de8\u7ebf\u7a0b\u3001\u8de8\u7528\u6237\u7684\u957f\u671f\u6570\u636e<\/td>\n<td>\u7528\u6237\u504f\u597d\u3001\u957f\u671f\u8bb0\u5fc6\u3001\u5171\u4eab\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td><code>interrupt_before<\/code><\/td>\n<td><code>list[str]<\/code><\/td>\n<td>\u5728\u6307\u5b9a\u8282\u70b9\u6267\u884c\u524d\u6682\u505c<\/td>\n<td>\u5de5\u5177\u6267\u884c\u524d\u4eba\u5de5\u786e\u8ba4<\/td>\n<\/tr>\n<tr>\n<td><code>interrupt_after<\/code><\/td>\n<td><code>list[str]<\/code><\/td>\n<td>\u5728\u6307\u5b9a\u8282\u70b9\u6267\u884c\u540e\u6682\u505c<\/td>\n<td>\u5de5\u5177\u6267\u884c\u540e\u5ba1\u6838\u6216\u4e8c\u6b21\u5904\u7406<\/td>\n<\/tr>\n<tr>\n<td><code>debug<\/code><\/td>\n<td><code>bool<\/code><\/td>\n<td>\u8f93\u51fa\u66f4\u8be6\u7ec6\u7684\u56fe\u6267\u884c\u4fe1\u606f<\/td>\n<td>\u8c03\u8bd5\u6d41\u7a0b\u3001\u68c0\u67e5\u72b6\u6001\u53d8\u5316<\/td>\n<\/tr>\n<tr>\n<td><code>name<\/code><\/td>\n<td><code>str<\/code><\/td>\n<td>\u8bbe\u7f6e Agent \u56fe\u7684\u540d\u79f0<\/td>\n<td>\u591a Agent\u3001\u5b50\u56fe\u3001\u8ffd\u8e2a\u548c\u8bc6\u522b<\/td>\n<\/tr>\n<tr>\n<td><code>cache<\/code><\/td>\n<td><code>BaseCache<\/code><\/td>\n<td>\u4e3a\u56fe\u6267\u884c\u914d\u7f6e\u7f13\u5b58<\/td>\n<td>\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u548c\u8c03\u7528\u6210\u672c<\/td>\n<\/tr>\n<tr>\n<td><code>transformers<\/code><\/td>\n<td><code>Sequence[TransformerFactory]<\/code><\/td>\n<td>\u6ce8\u518c\u6d41\u5f0f\u8f93\u51fa\u8f6c\u6362\u5668<\/td>\n<td>\u63a7\u5236\u6d41\u8f93\u51fa\u3001\u5904\u7406\u5de5\u5177\u8c03\u7528\u6d41<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>4. \u6838\u5fc3\u53c2\u6570\u8be6\u89e3<\/h2>\n<h3>4.1 <code>model<\/code>\uff1aAgent \u7684\u5927\u8111<\/h3>\n<p><code>model<\/code> \u53ef\u4ee5\u4f20\u5165\u6a21\u578b\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u4f20\u5165\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u804a\u5929\u6a21\u578b\u5b9e\u4f8b\u3002<\/p>\n<h4>\u4f20\u5165\u6a21\u578b\u5b57\u7b26\u4e32<\/h4>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n)\n<\/code><\/pre>\n<p>\u6a21\u578b\u5b57\u7b26\u4e32\u901a\u5e38\u4f7f\u7528 <code>provider:model<\/code> \u7684\u5f62\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-text\">openai:gpt-5.5\nanthropic:claude-sonnet-4-5-20250929\n<\/code><\/pre>\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u5b89\u88c5\u5bf9\u5e94\u7684 Provider \u96c6\u6210\u5305\uff0c\u5e76\u914d\u7f6e API Key\u3002<\/p>\n<h4>\u4f20\u5165\u804a\u5929\u6a21\u578b\u5bf9\u8c61<\/h4>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\nfrom langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(model=&quot;gpt-4o-mini&quot;, temperature=0)\n\nagent = create_agent(\n    model=model,\n    tools=[],\n)\n<\/code><\/pre>\n<p>\u4e24\u79cd\u65b9\u5f0f\u7684\u533a\u522b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u5f0f<\/th>\n<th>\u4f18\u70b9<\/th>\n<th>\u9002\u5408\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6a21\u578b\u5b57\u7b26\u4e32<\/td>\n<td>\u7b80\u6d01\uff0c\u65b9\u4fbf\u5207\u6362 Provider<\/td>\n<td>\u5feb\u901f\u539f\u578b\u3001\u914d\u7f6e\u9a71\u52a8<\/td>\n<\/tr>\n<tr>\n<td>\u6a21\u578b\u5b9e\u4f8b<\/td>\n<td>\u53ef\u7cbe\u786e\u914d\u7f6e\u6a21\u578b\u53c2\u6570<\/td>\n<td>\u751f\u4ea7\u9879\u76ee\u3001\u590d\u6742\u6a21\u578b\u914d\u7f6e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>4.2 <code>tools<\/code>\uff1aAgent \u80fd\u505a\u4ec0\u4e48<\/h3>\n<p>\u5de5\u5177\u53ef\u4ee5\u662f\u666e\u901a Python \u51fd\u6570\u3001LangChain \u5de5\u5177\u5bf9\u8c61\u6216\u5de5\u5177\u63cf\u8ff0\u5b57\u5178\u3002<\/p>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\n\n\ndef add(a: int, b: int) -&gt; int:\n    &quot;&quot;&quot;\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002&quot;&quot;&quot;\n    return a + b\n\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[add],\n)\n<\/code><\/pre>\n<p>\u5de5\u5177\u7684\u6587\u6863\u5b57\u7b26\u4e32\u975e\u5e38\u91cd\u8981\u3002\u6a21\u578b\u901a\u5e38\u4f1a\u6839\u636e\u5de5\u5177\u540d\u79f0\u3001\u53c2\u6570\u7c7b\u578b\u548c\u63cf\u8ff0\u51b3\u5b9a\u662f\u5426\u8c03\u7528\u5b83\u3002<\/p>\n<p>\u4e00\u4e2a\u597d\u7684\u5de5\u5177\u5e94\u8be5\u5177\u5907\uff1a<\/p>\n<ul>\n<li>\u6e05\u6670\u7684\u540d\u79f0\u3002<\/li>\n<li>\u51c6\u786e\u7684\u53c2\u6570\u7c7b\u578b\u3002<\/li>\n<li>\u5177\u4f53\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u660e\u786e\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<li>\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u3002<\/li>\n<li>\u6700\u5c0f\u5fc5\u8981\u6743\u9650\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c <code>tools=None<\/code> \u6216\u4f20\u5165\u7a7a\u5217\u8868\uff0cAgent \u4ecd\u7136\u53ef\u4ee5\u8c03\u7528\u6a21\u578b\uff0c\u4f46\u4e0d\u4f1a\u6267\u884c\u5de5\u5177\u8c03\u7528\u5faa\u73af\u3002<\/p>\n<h3>4.3 <code>system_prompt<\/code>\uff1a\u5b9a\u4e49 Agent \u7684\u884c\u4e3a<\/h3>\n<p><code>system_prompt<\/code> \u4f1a\u4f5c\u4e3a\u7cfb\u7edf\u6d88\u606f\u653e\u5728\u6d88\u606f\u5217\u8868\u5f00\u5934\u3002<\/p>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[add],\n    system_prompt=(\n        &quot;\u4f60\u662f\u4e00\u4e2a\u4e25\u8c28\u7684\u6570\u5b66\u52a9\u624b\u3002&quot;\n        &quot;\u8ba1\u7b97\u95ee\u9898\u5fc5\u987b\u8c03\u7528\u5de5\u5177\uff0c\u4e0d\u8981\u5fc3\u7b97\u3002&quot;\n        &quot;\u56de\u7b54\u65f6\u8bf4\u660e\u8ba1\u7b97\u7ed3\u679c\u3002&quot;\n    ),\n)\n<\/code><\/pre>\n<p>\u7cfb\u7edf\u63d0\u793a\u8bcd\u901a\u5e38\u53ef\u4ee5\u89c4\u5b9a\uff1a<\/p>\n<ul>\n<li>Agent \u7684\u89d2\u8272\u3002<\/li>\n<li>Agent \u7684\u76ee\u6807\u3002<\/li>\n<li>\u4ec0\u4e48\u65f6\u5019\u8c03\u7528\u5de5\u5177\u3002<\/li>\n<li>\u4e0d\u80fd\u505a\u4ec0\u4e48\u3002<\/li>\n<li>\u8f93\u51fa\u683c\u5f0f\u548c\u8bed\u8a00\u3002<\/li>\n<li>\u5b89\u5168\u548c\u6743\u9650\u8fb9\u754c\u3002<\/li>\n<\/ul>\n<p>\u9700\u7279\u522b\u8bf4\u660e\u7684\u662f\uff0c<code>system_prompt<\/code> \u4e0d\u80fd\u66ff\u4ee3\u5de5\u5177\u6743\u9650\u63a7\u5236\u3002\u6d89\u53ca\u98ce\u9669\u7684\u64cd\u4f5c\u4ecd\u7136\u9700\u8981\u5728\u5de5\u5177\u4ee3\u7801\u548c\u4e1a\u52a1\u670d\u52a1\u7aef\u8fdb\u884c\u6821\u9a8c\u3002<\/p>\n<h3>4.4 <code>middleware<\/code>\uff1a\u5728 Agent \u6d41\u7a0b\u4e2d\u63d2\u5165\u903b\u8f91<\/h3>\n<p>\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u62e6\u622a\u6216\u4fee\u6539 Agent \u7684\u8fd0\u884c\u8fc7\u7a0b\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u8c03\u7528\u6a21\u578b\u524d\u4fee\u6539\u6d88\u606f\u3002<\/li>\n<li>\u8c03\u7528\u5de5\u5177\u524d\u8fdb\u884c\u6743\u9650\u68c0\u67e5\u3002<\/li>\n<li>\u5de5\u5177\u5931\u8d25\u540e\u81ea\u52a8\u91cd\u8bd5\u3002<\/li>\n<li>\u8bb0\u5f55\u6a21\u578b\u548c\u5de5\u5177\u8c03\u7528\u65e5\u5fd7\u3002<\/li>\n<li>\u6839\u636e\u4e0a\u4e0b\u6587\u52a8\u6001\u9009\u62e9\u6a21\u578b\u3002<\/li>\n<li>\u9650\u5236\u6a21\u578b\u8c03\u7528\u6b21\u6570\u3002<\/li>\n<li>\u5bf9\u8f93\u51fa\u8fdb\u884c\u5ba1\u6838\u3002<\/li>\n<\/ul>\n<p>\u6982\u5ff5\u4e0a\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a<\/p>\n<pre><code class=\"language-text\">\u8bf7\u6c42\n  \u2193\nMiddleware before\n  \u2193\n\u6a21\u578b\u6216\u5de5\u5177\u6267\u884c\n  \u2193\nMiddleware after\n  \u2193\n\u7ed3\u679c\n<\/code><\/pre>\n<p>\u4e2d\u95f4\u4ef6\u66f4\u9002\u5408\u627f\u8f7d\u901a\u7528\u80fd\u529b\uff0c\u800c\u4e0d\u662f\u5177\u4f53\u4e1a\u52a1\u6d41\u7a0b\u3002\u4f8b\u5982\uff0c\u201c\u6240\u6709\u5de5\u5177\u90fd\u9700\u8981\u6743\u9650\u68c0\u67e5\u201d\u9002\u5408\u653e\u8fdb\u4e2d\u95f4\u4ef6\uff1b\u201c\u672c\u6b21\u8ba2\u5355\u5fc5\u987b\u5148\u67e5\u8be2\u5e93\u5b58\u201d\u5219\u66f4\u9002\u5408\u653e\u8fdb\u660e\u786e\u7684\u4e1a\u52a1\u6d41\u7a0b\u8282\u70b9\u3002<\/p>\n<h3>4.5 <code>response_format<\/code>\uff1a\u8ba9\u7ed3\u679c\u7ed3\u6784\u5316<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cAgent \u4e3b\u8981\u901a\u8fc7\u6d88\u606f\u8fd4\u56de\u81ea\u7136\u8bed\u8a00\u7ed3\u679c\u3002\u4f7f\u7528 <code>response_format<\/code> \u53ef\u4ee5\u8981\u6c42\u6a21\u578b\u8fd4\u56de\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-python\">from pydantic import BaseModel, Field\nfrom langchain.agents import create_agent\n\n\nclass TicketClassification(BaseModel):\n    category: str = Field(description=&quot;\u95ee\u9898\u5206\u7c7b&quot;)\n    priority: str = Field(description=&quot;\u4f18\u5148\u7ea7&quot;)\n    reason: str = Field(description=&quot;\u5206\u7c7b\u539f\u56e0&quot;)\n\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n    response_format=TicketClassification,\n)\n<\/code><\/pre>\n<p>\u7ed3\u6784\u5316\u7ed3\u679c\u901a\u5e38\u53ef\u4ee5\u4ece\u8fd4\u56de\u72b6\u6001\u4e2d\u7684 <code>structured_response<\/code> \u5b57\u6bb5\u8bfb\u53d6\uff1a<\/p>\n<pre><code class=\"language-python\">result = agent.invoke({\n    &quot;messages&quot;: [\n        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u7528\u6237\u65e0\u6cd5\u767b\u5f55\uff0c\u5df2\u7ecf\u5f71\u54cd\u751f\u4ea7\uff0c\u8bf7\u5206\u7c7b\u3002&quot;}\n    ]\n})\n\nclassification = result[&quot;structured_response&quot;]\nprint(classification.category)\n<\/code><\/pre>\n<p>\u9002\u5408\u4f7f\u7528\u7ed3\u6784\u5316\u8f93\u51fa\u7684\u573a\u666f\uff1a<\/p>\n<ul>\n<li>\u610f\u56fe\u5206\u7c7b\u3002<\/li>\n<li>\u4fe1\u606f\u62bd\u53d6\u3002<\/li>\n<li>\u8868\u5355\u586b\u5199\u3002<\/li>\n<li>\u4efb\u52a1\u8def\u7531\u3002<\/li>\n<li>\u7ed3\u6784\u5316\u62a5\u544a\u3002<\/li>\n<li>\u7ed9\u5176\u4ed6\u7a0b\u5e8f\u63d0\u4f9b\u53ef\u9760\u8f93\u5165\u3002<\/li>\n<\/ul>\n<h3>4.6 <code>state_schema<\/code>\uff1a\u5b9a\u4e49 Agent \u7684\u8fd0\u884c\u72b6\u6001<\/h3>\n<p>Agent \u7684\u72b6\u6001\u53ef\u4ee5\u7406\u89e3\u4e3a Agent \u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u5171\u4eab\u7684\u201c\u5de5\u4f5c\u8bb0\u5f55\u201d\u3002\u9ed8\u8ba4\u72b6\u6001\u901a\u5e38\u5305\u542b\u6d88\u606f\u5217\u8868\u7b49\u57fa\u7840\u5b57\u6bb5\uff1b\u5982\u679c\u4e1a\u52a1\u9700\u8981\u66f4\u591a\u5b57\u6bb5\uff0c\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5b9a\u4e49\u72b6\u6001\u3002<\/p>\n<pre><code class=\"language-python\">from typing_extensions import NotRequired\nfrom langchain.agents import AgentState, create_agent\n\n\nclass MyAgentState(AgentState):\n    user_id: NotRequired[str]\n    order_id: NotRequired[str]\n    risk_level: NotRequired[str]\n\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n    state_schema=MyAgentState,\n)\n<\/code><\/pre>\n<p>\u9002\u5408\u653e\u8fdb\u72b6\u6001\u7684\u6570\u636e\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5f53\u524d\u6d88\u606f\u3002<\/li>\n<li>\u5f53\u524d\u4efb\u52a1 ID\u3002<\/li>\n<li>\u5206\u7c7b\u7ed3\u679c\u3002<\/li>\n<li>\u5de5\u5177\u6267\u884c\u7ed3\u679c\u3002<\/li>\n<li>\u98ce\u9669\u7b49\u7ea7\u3002<\/li>\n<li>\u9700\u8981\u4eba\u5de5\u5ba1\u6838\u7684\u6807\u8bb0\u3002<\/li>\n<\/ul>\n<p>\u4e0d\u5b9c\u628a\u6240\u6709\u53ef\u4ee5\u5373\u65f6\u8ba1\u7b97\u7684\u6570\u636e\u90fd\u653e\u8fdb\u72b6\u6001\u3002\u72b6\u6001\u5e94\u4e3b\u8981\u4fdd\u5b58\u8de8\u6b65\u9aa4\u9700\u8981\u590d\u7528\u3001\u91cd\u65b0\u83b7\u53d6\u6210\u672c\u8f83\u9ad8\u6216\u5fc5\u987b\u4fdd\u7559\u7684\u6570\u636e\u3002<\/p>\n<h3>4.7 <code>context_schema<\/code>\uff1a\u4f20\u5165\u672c\u6b21\u8fd0\u884c\u7684\u4e0a\u4e0b\u6587<\/h3>\n<p><code>context_schema<\/code> \u63cf\u8ff0\u4e00\u6b21\u8fd0\u884c\u65f6\u4f20\u5165\u7684\u4e0a\u4e0b\u6587\uff0c\u4f8b\u5982\u7528\u6237 ID\u3001\u79df\u6237 ID\u3001\u8fd0\u884c\u6a21\u5f0f\u6216\u6743\u9650\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-python\">from typing_extensions import TypedDict\nfrom langchain.agents import create_agent\n\n\nclass RuntimeContext(TypedDict):\n    user_id: str\n    tenant_id: str\n    mode: str\n\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n    context_schema=RuntimeContext,\n)\n<\/code><\/pre>\n<p>\u4e0a\u4e0b\u6587\u53ef\u4ee5\u5728\u8c03\u7528 Agent \u65f6\u4f20\u5165\uff1a<\/p>\n<pre><code class=\"language-python\">result = agent.invoke(\n    {&quot;messages&quot;: [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u67e5\u8be2\u6211\u7684\u8ba2\u5355&quot;}]},\n    context={\n        &quot;user_id&quot;: &quot;user-001&quot;,\n        &quot;tenant_id&quot;: &quot;tenant-001&quot;,\n        &quot;mode&quot;: &quot;production&quot;,\n    },\n)\n<\/code><\/pre>\n<p>\u9700\u8981\u533a\u5206\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6982\u5ff5<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>state_schema<\/code><\/td>\n<td>Agent \u6267\u884c\u8fc7\u7a0b\u4e2d\u6301\u7eed\u53d8\u5316\u7684\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td><code>context_schema<\/code><\/td>\n<td>\u672c\u6b21\u8fd0\u884c\u5f00\u59cb\u65f6\u63d0\u4f9b\u7684\u4e0a\u4e0b\u6587\u914d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td><code>checkpointer<\/code><\/td>\n<td>\u4fdd\u5b58\u67d0\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td><code>store<\/code><\/td>\n<td>\u4fdd\u5b58\u8de8\u7ebf\u7a0b\u3001\u8de8\u4f1a\u8bdd\u7684\u957f\u671f\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>4.8 <code>checkpointer<\/code>\uff1a\u4fdd\u5b58\u5355\u4e2a\u4f1a\u8bdd<\/h3>\n<p><code>checkpointer<\/code> \u7528\u4e8e\u4fdd\u5b58\u67d0\u4e2a\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c\u9002\u5408\u591a\u8f6e\u5bf9\u8bdd\u548c\u6682\u505c\u540e\u6062\u590d\u3002<\/p>\n<pre><code class=\"language-python\">from langgraph.checkpoint.memory import InMemorySaver\nfrom langchain.agents import create_agent\n\ncheckpointer = InMemorySaver()\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n    checkpointer=checkpointer,\n)\n\nconfig = {\n    &quot;configurable&quot;: {\n        &quot;thread_id&quot;: &quot;conversation-001&quot;\n    }\n}\n\nagent.invoke(\n    {&quot;messages&quot;: [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u6211\u53eb\u5c0f\u660e&quot;}]},\n    config,\n)\n\nresult = agent.invoke(\n    {&quot;messages&quot;: [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u6211\u53eb\u4ec0\u4e48\uff1f&quot;}]},\n    config,\n)\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c<code>thread_id<\/code> \u7528\u4e8e\u6807\u8bc6\u4f1a\u8bdd\uff0c\u4f7f Checkpointer \u80fd\u591f\u5224\u65ad\u54ea\u4e9b\u8c03\u7528\u5c5e\u4e8e\u540c\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p><code>InMemorySaver<\/code> \u4e3b\u8981\u9002\u7528\u4e8e\u5b66\u4e60\u548c\u6d4b\u8bd5\uff1b\u751f\u4ea7\u73af\u5883\u901a\u5e38\u9700\u8981\u4f7f\u7528\u6301\u4e45\u5316\u5b58\u50a8\u3002<\/p>\n<h3>4.9 <code>store<\/code>\uff1a\u4fdd\u5b58\u8de8\u4f1a\u8bdd\u6570\u636e<\/h3>\n<p><code>checkpointer<\/code> \u4e3b\u8981\u4fdd\u5b58\u67d0\u4e00\u4e2a\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c<code>store<\/code> \u5219\u9002\u5408\u4fdd\u5b58\u8de8\u7ebf\u7a0b\u3001\u8de8\u7528\u6237\u6216\u8de8\u4f1a\u8bdd\u7684\u6570\u636e\u3002<\/p>\n<p>\u7b80\u5355\u7406\u89e3\uff1a<\/p>\n<pre><code class=\"language-text\">checkpointer\uff1a\u8fd9\u4e2a\u5bf9\u8bdd\u8fdb\u884c\u5230\u54ea\u4e00\u6b65\u4e86\uff1f\nstore\uff1a\u8fd9\u4e2a\u7528\u6237\u957f\u671f\u504f\u597d\u4ec0\u4e48\uff1f\n<\/code><\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u957f\u671f\u504f\u597d\u3002<\/li>\n<li>\u7528\u6237\u7684\u7ec4\u7ec7\u4fe1\u606f\u3002<\/li>\n<li>\u591a\u4e2a\u4f1a\u8bdd\u5171\u4eab\u7684\u4e1a\u52a1\u914d\u7f6e\u3002<\/li>\n<li>Agent \u4e4b\u95f4\u5171\u4eab\u7684\u957f\u671f\u77e5\u8bc6\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u666e\u901a\u591a\u8f6e\u5bf9\u8bdd\uff0c\u4f7f\u7528 <code>checkpointer<\/code> \u5373\u53ef\uff1b\u9700\u8981\u8de8\u4f1a\u8bdd\u5171\u4eab\u6570\u636e\u65f6\uff0c\u518d\u5f15\u5165 <code>store<\/code>\u3002<\/p>\n<h3>4.10 <code>interrupt_before<\/code> \u548c <code>interrupt_after<\/code>\uff1a\u4eba\u5de5\u4ecb\u5165<\/h3>\n<p>\u8fd9\u4e24\u4e2a\u53c2\u6570\u7528\u4e8e\u5728\u56fe\u8282\u70b9\u524d\u540e\u6682\u505c\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[send_email],\n    interrupt_before=[&quot;tools&quot;],\n)\n<\/code><\/pre>\n<p>\u5178\u578b\u7528\u9014\uff1a<\/p>\n<ul>\n<li>\u53d1\u90ae\u4ef6\u524d\u8ba9\u7528\u6237\u786e\u8ba4\u3002<\/li>\n<li>\u5220\u9664\u6570\u636e\u524d\u8ba9\u7ba1\u7406\u5458\u5ba1\u6279\u3002<\/li>\n<li>\u8f6c\u8d26\u524d\u8fdb\u884c\u4eba\u5de5\u5ba1\u6838\u3002<\/li>\n<li>\u5de5\u5177\u6267\u884c\u540e\u68c0\u67e5\u7ed3\u679c\u3002<\/li>\n<li>\u9ad8\u98ce\u9669\u4efb\u52a1\u8fdb\u5165\u4eba\u5de5\u5de5\u4f5c\u53f0\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u6682\u505c\u80fd\u529b\u65f6\u901a\u5e38\u8fd8\u9700\u8981 Checkpointer \u4fdd\u5b58\u6682\u505c\u524d\u7684\u72b6\u6001\uff0c\u624d\u80fd\u5728\u4e4b\u540e\u6062\u590d\u6267\u884c\u3002<\/p>\n<h3>4.11 <code>debug<\/code>\uff1a\u67e5\u770b\u6267\u884c\u8fc7\u7a0b<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[add],\n    debug=True,\n)\n<\/code><\/pre>\n<p>\u8c03\u8bd5\u6a21\u5f0f\u53ef\u7528\u4e8e\u5b66\u4e60\u548c\u6392\u9519\uff0c\u4e3b\u8981\u53ef\u4ee5\u89c2\u5bdf\uff1a<\/p>\n<ul>\n<li>\u6a21\u578b\u4ec0\u4e48\u65f6\u5019\u88ab\u8c03\u7528\u3002<\/li>\n<li>\u6a21\u578b\u662f\u5426\u8bf7\u6c42\u4e86\u5de5\u5177\u3002<\/li>\n<li>\u5de5\u5177\u63a5\u6536\u4e86\u4ec0\u4e48\u53c2\u6570\u3002<\/li>\n<li>\u5de5\u5177\u8fd4\u56de\u4e86\u4ec0\u4e48\u7ed3\u679c\u3002<\/li>\n<li>\u72b6\u6001\u5982\u4f55\u53d1\u751f\u53d8\u5316\u3002<\/li>\n<\/ul>\n<p>\u751f\u4ea7\u73af\u5883\u4e0d\u5b9c\u65e0\u6761\u4ef6\u8f93\u51fa\u5168\u90e8\u8be6\u7ec6\u5185\u5bb9\uff0c\u56e0\u4e3a\u65e5\u5fd7\u4e2d\u53ef\u80fd\u5305\u542b\u7528\u6237\u9690\u79c1\u3001\u5bc6\u94a5\u6216\u4e1a\u52a1\u6570\u636e\u3002<\/p>\n<h3>4.12 <code>name<\/code>\uff1a\u7ed9 Agent \u56fe\u547d\u540d<\/h3>\n<pre><code class=\"language-python\">research_agent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[],\n    name=&quot;research_agent&quot;,\n)\n<\/code><\/pre>\n<p><code>name<\/code> \u5728\u591a Agent \u7cfb\u7edf\u4e2d\u5c24\u5176\u6709\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\uff1a<\/p>\n<ul>\n<li>\u533a\u5206\u4e0d\u540c Agent\u3002<\/li>\n<li>\u4f5c\u4e3a\u5b50\u56fe\u540d\u79f0\u3002<\/li>\n<li>\u8bc6\u522b\u65e5\u5fd7\u548c\u8ffd\u8e2a\u8bb0\u5f55\u3002<\/li>\n<li>\u8868\u793a Agent \u7684\u804c\u8d23\u3002<\/li>\n<\/ul>\n<h3>4.13 <code>cache<\/code>\uff1a\u7f13\u5b58\u6267\u884c\u7ed3\u679c<\/h3>\n<p><code>cache<\/code> \u7528\u4e8e\u51cf\u5c11\u91cd\u590d\u6267\u884c\uff0c\u9002\u7528\u4e8e\u7ed3\u679c\u7a33\u5b9a\u4e14\u91cd\u590d\u7387\u8f83\u9ad8\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u9002\u5408\u7f13\u5b58\u7684\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u7a33\u5b9a\u7684\u6587\u6863\u89e3\u6790\u7ed3\u679c\u3002<\/li>\n<li>\u91cd\u590d\u7684\u53ea\u8bfb\u67e5\u8be2\u3002<\/li>\n<li>\u6210\u672c\u8f83\u9ad8\u4f46\u7ed3\u679c\u53ef\u590d\u7528\u7684\u6a21\u578b\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u4e0d\u9002\u5408\u76f4\u63a5\u7f13\u5b58\u7684\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u5b9e\u65f6\u5e93\u5b58\u3002<\/li>\n<li>\u5b9e\u65f6\u5929\u6c14\u3002<\/li>\n<li>\u7528\u6237\u6743\u9650\u3002<\/li>\n<li>\u4ed8\u6b3e\u548c\u8f6c\u8d26\u3002<\/li>\n<li>\u4f9d\u8d56\u5f53\u524d\u65f6\u95f4\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u7f13\u5b58\u5fc5\u987b\u8003\u8651\u8f93\u5165\u3001\u7528\u6237\u6743\u9650\u3001\u6570\u636e\u7248\u672c\u548c\u8fc7\u671f\u65f6\u95f4\uff0c\u5426\u5219\u53ef\u80fd\u8fd4\u56de\u9519\u8bef\u6216\u8d8a\u6743\u7684\u6570\u636e\u3002<\/p>\n<h3>4.14 <code>transformers<\/code>\uff1a\u5904\u7406\u6d41\u5f0f\u8f93\u51fa<\/h3>\n<p>\u5f53\u524d\u5b98\u65b9\u63a5\u53e3\u8fd8\u652f\u6301 <code>transformers<\/code>\uff0c\u7528\u4e8e\u5411\u7f16\u8bd1\u540e\u7684 Agent \u56fe\u6ce8\u518c\u6d41\u8f6c\u6362\u5668\u3002<\/p>\n<p>\u8be5\u53c2\u6570\u4e3b\u8981\u7528\u4e8e\u5904\u7406\uff1a<\/p>\n<ul>\n<li>\u6d41\u5f0f\u6d88\u606f\u8f6c\u6362\u3002<\/li>\n<li>\u5de5\u5177\u8c03\u7528\u4e8b\u4ef6\u8f6c\u6362\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n<li>\u5bf9\u6d41\u4e2d\u7684\u5185\u5bb9\u8fdb\u884c\u8fc7\u6ee4\u6216\u52a0\u5de5\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u662f\u76f8\u5bf9\u9ad8\u7ea7\u7684\u53c2\u6570\u3002\u521d\u5b66\u9636\u6bb5\u53ef\u4ee5\u5148\u4f7f\u7528 <code>stream()<\/code> \u7684\u6807\u51c6\u8f93\u51fa\u6a21\u5f0f\uff0c\u5f85\u9700\u8981\u7edf\u4e00\u5904\u7406\u6d41\u4e8b\u4ef6\u65f6\u518d\u4f7f\u7528\u8be5\u53c2\u6570\u3002<\/p>\n<h2>5. \u6700\u5c0f\u53ef\u8fd0\u884c\u7ed3\u6784<\/h2>\n<p>\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2a\u6700\u5c0f Agent \u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\n\n\ndef get_weather(city: str) -&gt; str:\n    &quot;&quot;&quot;\u67e5\u8be2\u6307\u5b9a\u57ce\u5e02\u7684\u5929\u6c14\u3002&quot;&quot;&quot;\n    return f&quot;{city}\uff1a\u6674\u5929\uff0c25\u00b0C&quot;\n\n\nagent = create_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    tools=[get_weather],\n    system_prompt=&quot;\u4f60\u662f\u5929\u6c14\u52a9\u624b\u3002\u9700\u8981\u5929\u6c14\u4fe1\u606f\u65f6\u8c03\u7528\u5929\u6c14\u5de5\u5177\u3002&quot;,\n)\n\nresult = agent.invoke({\n    &quot;messages&quot;: [\n        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u5317\u4eac\u4eca\u5929\u7684\u5929\u6c14\u600e\u4e48\u6837\uff1f&quot;}\n    ]\n})\n\nprint(result[&quot;messages&quot;][-1].content)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u5173\u952e\u5173\u7cfb\u662f\uff1a<\/p>\n<pre><code class=\"language-text\">model       \u2192 \u8d1f\u8d23\u5224\u65ad\u548c\u751f\u6210\ntools       \u2192 \u8d1f\u8d23\u6267\u884c\u771f\u5b9e\u52a8\u4f5c\nsystem_prompt \u2192 \u8d1f\u8d23\u884c\u4e3a\u7ea6\u675f\ninvoke      \u2192 \u542f\u52a8 Agent \u56fe\nmessages    \u2192 \u4fdd\u5b58\u4ea4\u4e92\u8fc7\u7a0b\n<\/code><\/pre>\n<h2>6. Agent \u7684\u6267\u884c\u8fc7\u7a0b<\/h2>\n<p>\u5f53\u7528\u6237\u63d0\u51fa\u4e00\u4e2a\u9700\u8981\u8c03\u7528\u5de5\u5177\u7684\u95ee\u9898\u65f6\uff0c\u5927\u81f4\u4f1a\u53d1\u751f\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<pre><code class=\"language-text\">1. \u63a5\u6536 messages\n2. \u52a0\u5165 system_prompt\n3. \u8c03\u7528 model\n4. \u5224\u65ad\u6a21\u578b\u662f\u5426\u8fd4\u56de tool_calls\n5. \u5982\u679c\u6ca1\u6709 tool_calls\uff0c\u76f4\u63a5\u7ed3\u675f\n6. \u5982\u679c\u6709 tool_calls\uff0c\u6267\u884c\u5bf9\u5e94 tools\n7. \u5c06\u5de5\u5177\u7ed3\u679c\u5199\u5165 messages\n8. \u518d\u6b21\u8c03\u7528 model\n9. \u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u6a21\u578b\u7ed9\u51fa\u6700\u7ec8\u56de\u7b54\n<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f\u5e38\u89c1\u7684\u5de5\u5177\u8c03\u7528\u5faa\u73af\u3002<code>create_agent<\/code> \u9884\u7f6e\u4e86\u5faa\u73af\u63a7\u5236\u3001\u72b6\u6001\u4f20\u9012\u548c\u5de5\u5177\u8282\u70b9\u3002<\/p>\n<h2>7. \u5e38\u89c1\u914d\u7f6e\u7ec4\u5408<\/h2>\n<h3>7.1 \u666e\u901a\u95ee\u7b54 Agent<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=model,\n    tools=[],\n    system_prompt=&quot;\u4f60\u662f\u4e00\u4e2a\u77e5\u8bc6\u95ee\u7b54\u52a9\u624b\u3002&quot;,\n)\n<\/code><\/pre>\n<h3>7. \u5de5\u5177\u8c03\u7528 Agent<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=model,\n    tools=[search_web, query_database, calculate],\n    system_prompt=&quot;\u9700\u8981\u5916\u90e8\u4fe1\u606f\u65f6\u4f18\u5148\u8c03\u7528\u5de5\u5177\u3002&quot;,\n)\n<\/code><\/pre>\n<h3>7. \u591a\u8f6e\u5bf9\u8bdd Agent<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=model,\n    tools=tools,\n    checkpointer=checkpointer,\n)\n<\/code><\/pre>\n<h3>7. \u7ed3\u6784\u5316\u8f93\u51fa Agent<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=model,\n    tools=[],\n    response_format=ResultSchema,\n)\n<\/code><\/pre>\n<h3>7. \u9700\u8981\u4eba\u5de5\u786e\u8ba4\u7684 Agent<\/h3>\n<pre><code class=\"language-python\">agent = create_agent(\n    model=model,\n    tools=[send_email, delete_record],\n    checkpointer=checkpointer,\n    interrupt_before=[&quot;tools&quot;],\n)\n<\/code><\/pre>\n<h3>7. \u751f\u4ea7\u7ea7 Agent<\/h3>\n<p>\u751f\u4ea7\u9879\u76ee\u901a\u5e38\u4f1a\u7ec4\u5408\uff1a<\/p>\n<pre><code class=\"language-text\">model\n  + tools\n  + system_prompt\n  + middleware\n  + response_format\n  + checkpointer\n  + store\n  + interrupt\n  + tracing \/ logging\n  + retry \/ timeout \/ permission control\n<\/code><\/pre>\n<h2>8. <code>create_agent<\/code> \u548c\u65e7\u63a5\u53e3\u7684\u5173\u7cfb<\/h2>\n<p>\u5728\u8f83\u65b0\u7684 LangChain \u7248\u672c\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">from langchain.agents import create_agent\n<\/code><\/pre>\n<p>\u4e00\u4e9b\u65e7\u6559\u7a0b\u4f1a\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">from langgraph.prebuilt import create_react_agent\n<\/code><\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre><code class=\"language-python\">from langchain_classic.agents import initialize_agent\n<\/code><\/pre>\n<p>\u5b98\u65b9\u53c2\u8003\u6587\u6863\u5c06 <code>create_react_agent<\/code> \u548c <code>initialize_agent<\/code> \u6807\u8bb0\u4e3a\u8fc1\u79fb\u65b9\u5411\u4e0a\u7684\u65e7\u63a5\u53e3\uff0c\u65b0\u7684 Agent \u901a\u5e38\u5e94\u4f18\u5148\u4f7f\u7528 <code>langchain.agents.create_agent<\/code>\u3002<code>create_agent<\/code> \u5728\u4fdd\u7559\u5de5\u5177\u8c03\u7528\u5faa\u73af\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u4e86\u66f4\u7edf\u4e00\u7684\u4e2d\u95f4\u4ef6\u3001\u7ed3\u6784\u5316\u8f93\u51fa\u548c LangGraph \u80fd\u529b\u3002<a href=\"https:\/\/reference.langchain.com\/python\/langchain\/agents\">LangChain Agents \u53c2\u8003\u6587\u6863<\/a><\/p>\n<h2>9. \u5e38\u89c1\u8bef\u533a<\/h2>\n<h3>\u8bef\u533a\u4e00\uff1aAgent \u4f1a\u81ea\u52a8\u62e5\u6709\u6240\u6709\u80fd\u529b<\/h3>\n<p>Agent \u53ea\u80fd\u8c03\u7528\u4f20\u5165\u7684\u5de5\u5177\u3002\u6ca1\u6709\u641c\u7d22\u5de5\u5177\u65f6\uff0c\u5b83\u65e0\u6cd5\u771f\u6b63\u641c\u7d22\u4e92\u8054\u7f51\uff1b\u6ca1\u6709\u6570\u636e\u5e93\u5de5\u5177\u65f6\uff0c\u5b83\u4e5f\u65e0\u6cd5\u67e5\u8be2\u6570\u636e\u5e93\u3002<\/p>\n<h3>\u8bef\u533a\u4e8c\uff1a\u53ea\u9760 system prompt \u5c31\u80fd\u4fdd\u8bc1\u5b89\u5168<\/h3>\n<p>\u4e0d\u80fd\u3002\u5b89\u5168\u63a7\u5236\u5fc5\u987b\u5728\u5de5\u5177\u548c\u670d\u52a1\u7aef\u6267\u884c\uff0c\u4f8b\u5982\u68c0\u67e5\u7528\u6237\u8eab\u4efd\u3001\u6743\u9650\u3001\u53c2\u6570\u8303\u56f4\u548c\u64cd\u4f5c\u98ce\u9669\u3002<\/p>\n<h3>\u8bef\u533a\u4e09\uff1a<code>state_schema<\/code> \u5c31\u662f\u957f\u671f\u8bb0\u5fc6<\/h3>\n<p>\u4e0d\u662f\u3002<code>state_schema<\/code> \u662f\u8fd0\u884c\u72b6\u6001\u7ed3\u6784\uff1b\u8981\u5b9e\u73b0\u4f1a\u8bdd\u6301\u4e45\u5316\uff0c\u9700\u8981 <code>checkpointer<\/code>\uff1b\u8981\u5b9e\u73b0\u8de8\u4f1a\u8bdd\u957f\u671f\u5b58\u50a8\uff0c\u9700\u8981 <code>store<\/code>\u3002<\/p>\n<h3>\u8bef\u533a\u56db\uff1a\u6240\u6709\u95ee\u9898\u90fd\u5e94\u8be5\u4ea4\u7ed9 Agent \u81ea\u4e3b\u51b3\u5b9a<\/h3>\n<p>\u5e76\u975e\u6240\u6709\u95ee\u9898\u90fd\u9700\u8981 Agent\u3002\u5bf9\u4e8e\u56fa\u5b9a\u4e14\u53ef\u9884\u6d4b\u7684\u4e1a\u52a1\u6d41\u7a0b\uff0c\u666e\u901a\u5de5\u4f5c\u6d41\u901a\u5e38\u66f4\u52a0\u5408\u9002\uff1b\u53ea\u6709\u5728\u9700\u8981\u52a8\u6001\u9009\u62e9\u5de5\u5177\u6216\u6b65\u9aa4\u65f6\uff0c\u624d\u6709\u5fc5\u8981\u5f15\u5165 Agent\u3002<\/p>\n<h3>\u8bef\u533a\u4e94\uff1a\u5de5\u5177\u8d8a\u591a\u8d8a\u597d<\/h3>\n<p>\u5de5\u5177\u8fc7\u591a\u4f1a\u589e\u52a0\u6a21\u578b\u9009\u62e9\u96be\u5ea6\u3001\u8c03\u7528\u6210\u672c\u548c\u9519\u8bef\u6982\u7387\u3002\u5e94\u8be5\u63d0\u4f9b\u804c\u8d23\u6e05\u6670\u3001\u6743\u9650\u6700\u5c0f\u7684\u5de5\u5177\u3002<\/p>\n<h2>10. \u5b66\u4e60\u987a\u5e8f<\/h2>\n<p>\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u638c\u63e1\u76f8\u5173\u5185\u5bb9\uff1a<\/p>\n<ol>\n<li><code>model<\/code>\uff1a\u8ba9 Agent \u80fd\u591f\u8c03\u7528\u6a21\u578b\u3002<\/li>\n<li><code>tools<\/code>\uff1a\u8ba9 Agent \u80fd\u591f\u6267\u884c\u771f\u5b9e\u52a8\u4f5c\u3002<\/li>\n<li><code>system_prompt<\/code>\uff1a\u7ea6\u675f Agent \u7684\u884c\u4e3a\u3002<\/li>\n<li><code>invoke<\/code> \u548c <code>stream<\/code>\uff1a\u8fd0\u884c\u548c\u89c2\u5bdf Agent\u3002<\/li>\n<li><code>response_format<\/code>\uff1a\u83b7\u5f97\u7a33\u5b9a\u7684\u7ed3\u6784\u5316\u7ed3\u679c\u3002<\/li>\n<li><code>checkpointer<\/code>\uff1a\u5b9e\u73b0\u591a\u8f6e\u5bf9\u8bdd\u3002<\/li>\n<li><code>middleware<\/code>\uff1a\u52a0\u5165\u91cd\u8bd5\u3001\u65e5\u5fd7\u548c\u6743\u9650\u903b\u8f91\u3002<\/li>\n<li><code>interrupt_before<\/code>\uff1a\u5b9e\u73b0\u4eba\u5de5\u786e\u8ba4\u3002<\/li>\n<li><code>state_schema<\/code>\u3001<code>context_schema<\/code> \u548c <code>store<\/code>\uff1a\u6784\u5efa\u590d\u6742\u5e94\u7528\u3002<\/li>\n<\/ol>\n<h2>11. \u603b\u7ed3<\/h2>\n<pre><code class=\"language-text\">create_agent = \u6a21\u578b + \u5de5\u5177 + \u63d0\u793a\u8bcd + \u72b6\u6001 + \u6267\u884c\u5faa\u73af + \u53ef\u9009\u7684\u6301\u4e45\u5316\u4e0e\u4e2d\u95f4\u4ef6\n<\/code><\/pre>\n<p>\u7406\u89e3\u8be5\u51fd\u6570\u7684\u91cd\u70b9\u5e76\u975e\u8bb0\u4f4f\u6bcf\u4e2a\u53c2\u6570\u7684\u540d\u79f0\uff0c\u800c\u662f\u660e\u786e\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><code>model<\/code> \u51b3\u5b9a Agent \u5982\u4f55\u7406\u89e3\u548c\u51b3\u7b56\u3002<\/li>\n<li><code>tools<\/code> \u51b3\u5b9a Agent \u80fd\u591f\u505a\u4ec0\u4e48\u3002<\/li>\n<li><code>system_prompt<\/code> \u51b3\u5b9a Agent \u5e94\u8be5\u9075\u5b88\u4ec0\u4e48\u89c4\u5219\u3002<\/li>\n<li><code>middleware<\/code> \u51b3\u5b9a\u5982\u4f55\u63a7\u5236 Agent \u7684\u6267\u884c\u8fc7\u7a0b\u3002<\/li>\n<li><code>state_schema<\/code> \u548c <code>context_schema<\/code> \u51b3\u5b9a Agent \u5982\u4f55\u7ba1\u7406\u6570\u636e\u3002<\/li>\n<li><code>checkpointer<\/code> \u548c <code>store<\/code> \u51b3\u5b9a\u6570\u636e\u4fdd\u5b58\u591a\u4e45\u3001\u5728\u54ea\u4e9b\u4f1a\u8bdd\u4e4b\u95f4\u5171\u4eab\u3002<\/li>\n<li><code>interrupt<\/code> \u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u4ea4\u7ed9\u4eba\u786e\u8ba4\u3002<\/li>\n<\/ul>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/reference.langchain.com\/python\/langchain\/agents\/factory\/create_agent\">LangChain \u5b98\u65b9 <code>create_agent<\/code> API Reference<\/a><\/li>\n<li><a href=\"https:\/\/reference.langchain.com\/python\/langchain\/agents\">LangChain \u5b98\u65b9 Agents Reference<\/a><\/li>\n<li><a href=\"https:\/\/docs.langchain.com\/oss\/python\/\">LangChain \u5b98\u65b9 Python \u6587\u6863<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-539","post","type-post","status-publish","format-standard","hentry","category-py-agent"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=539"}],"version-history":[{"count":1,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/539\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/539\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}