{"id":33108,"date":"2025-07-22T18:35:10","date_gmt":"2025-07-22T10:35:10","guid":{"rendered":"https:\/\/www.kdjingpai.com\/?p=33108"},"modified":"2025-07-22T18:35:10","modified_gmt":"2025-07-22T10:35:10","slug":"12-factor-agentsai-8","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/12-factor-agentsai-8\/","title":{"rendered":"\u300a12-Factor Agents\u300b7. \u4f7f\u7528\u5de5\u5177\u8c03\u7528\u4e0e\u4eba\u4ea4\u6d41"},"content":{"rendered":"<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5927\u8bed\u8a00\u6a21\u578b (LLM) API \u4f9d\u8d56\u4e8e\u4e00\u4e2a\u6839\u672c\u6027\u7684\u9ad8\u98ce\u9669 <a href=\"https:\/\/www.kdjingpai.com\/tokenization\/\">Token<\/a> \u9009\u62e9\uff1a\u6211\u4eec\u662f\u8fd4\u56de\u7eaf\u6587\u672c\u5185\u5bb9\uff0c\u8fd8\u662f\u8fd4\u56de\u7ed3\u6784\u5316\u6570\u636e\uff1f<\/p>\n<p><img decoding=\"async\" title=\"\u300a12-Factor Agents\u300b7. \u4f7f\u7528\u5de5\u5177\u8c03\u7528\u4e0e\u4eba\u4ea4\u6d41-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/07\/4f477bed962bd23.jpg\" alt=\"\u300a12-Factor Agents\u300b7. \u4f7f\u7528\u5de5\u5177\u8c03\u7528\u4e0e\u4eba\u4ea4\u6d41-1\" \/><\/p>\n<p>\u4f60\u5c06\u5f88\u5927\u7684\u6743\u91cd\u653e\u5728\u4e86\u7b2c\u4e00\u4e2a Token \u7684\u9009\u62e9\u4e0a\uff0c\u5728\u00a0<code>the weather in tokyo<\/code>\u00a0\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e2a Token \u662f<\/p>\n<p>&#8220;the&#8221;<\/p>\n<p>\u4f46\u5728\u00a0<code>fetch_weather<\/code>\u00a0\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Token\uff0c\u7528\u6765\u8868\u793a JSON \u5bf9\u8c61\u7684\u5f00\u59cb\u3002<\/p>\n<p>|JSON&gt;<\/p>\n<p>\u901a\u8fc7\u8ba9\u5927\u8bed\u8a00\u6a21\u578b (LLM)\u00a0<em>\u59cb\u7ec8<\/em>\u8f93\u51fa json\uff0c\u7136\u540e\u7528\u4e00\u4e9b\u81ea\u7136\u8bed\u8a00 Token (\u4f8b\u5982\u00a0<code>request_human_input<\/code>\u00a0\u6216\u00a0<code>done_for_now<\/code>) \u6765\u58f0\u660e\u5176\u610f\u56fe (\u800c\u4e0d\u662f\u50cf\u00a0<code>check_weather_in_city<\/code>\u00a0\u8fd9\u6837\u7684\u201c\u6807\u51c6\u201d\u5de5\u5177)\uff0c\u4f60\u53ef\u80fd\u4f1a\u83b7\u5f97\u66f4\u597d\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u540c\u6837\uff0c\u8fd9\u53ef\u80fd\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u6027\u80fd\u63d0\u5347\uff0c\u4f46\u4f60\u5e94\u8be5\u8fdb\u884c\u5b9e\u9a8c\uff0c\u5e76\u786e\u4fdd\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u5c1d\u8bd5\u4e00\u4e9b\u975e\u5e38\u89c4\u7684\u65b9\u6cd5\u6765\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\u3002<\/p>\n<pre><code>class Options:\r\nurgency: Literal[\"low\", \"medium\", \"high\"]\r\nformat: Literal[\"free_text\", \"yes_no\", \"multiple_choice\"]\r\nchoices: List[str]\r\n# \u7528\u4e8e\u4eba\u7c7b\u4ea4\u4e92\u7684\u5de5\u5177\u5b9a\u4e49\r\nclass RequestHumanInput:\r\nintent: \"request_human_input\"\r\nquestion: str\r\ncontext: str\r\noptions: Options\r\n# \u5728\u4ee3\u7406\u5faa\u73af\u4e2d\u7684\u4f7f\u7528\u793a\u4f8b\r\nif nextStep.intent == 'request_human_input':\r\nthread.events.append({\r\ntype: 'human_input_requested',\r\ndata: nextStep\r\n})\r\nthread_id = await save_state(thread)\r\nawait notify_human(nextStep, thread_id)\r\nreturn # \u4e2d\u65ad\u5faa\u73af\u5e76\u7b49\u5f85\u5e26\u6709\u7ebf\u7a0b ID \u7684\u54cd\u5e94\u8fd4\u56de\r\nelse:\r\n# ... \u5176\u4ed6\u60c5\u51b5\r\n<\/code><\/pre>\n<p>\u4e4b\u540e\uff0c\u4f60\u53ef\u80fd\u4f1a\u4ece\u5904\u7406 slack\u3001\u7535\u5b50\u90ae\u4ef6\u3001\u77ed\u4fe1\u6216\u5176\u4ed6\u4e8b\u4ef6\u7684\u7cfb\u7edf\u4e2d\u6536\u5230\u4e00\u4e2a webhook\u3002<\/p>\n<pre><code>@app.post('\/webhook')\r\ndef webhook(req: Request):\r\nthread_id = req.body.threadId\r\nthread = await load_state(thread_id)\r\nthread.events.push({\r\ntype: 'response_from_human',\r\ndata: req.body\r\n})\r\n# ... \u4e3a\u7b80\u6d01\u8d77\u89c1\u5df2\u7b80\u5316\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u5728\u8fd9\u91cc\u963b\u585e web worker\r\nnext_step = await determine_next_step(thread_to_prompt(thread))\r\nthread.events.append(next_step)\r\nresult = await handle_next_step(thread, next_step)\r\n# todo - \u5faa\u73af\u3001\u4e2d\u65ad\u6216\u4efb\u4f55\u4f60\u60f3\u8981\u7684\u64cd\u4f5c\r\nreturn {\"status\": \"ok\"}\r\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5185\u5bb9\u5305\u542b\u4e86\u6765\u81ea\u00a0<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-6\/\">\u8981\u7d20 5 &#8211; \u7edf\u4e00\u6267\u884c\u72b6\u6001\u548c\u4e1a\u52a1\u72b6\u6001<\/a>\u3001<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-9\/\">\u8981\u7d20 8 &#8211; \u638c\u63a7\u4f60\u7684\u63a7\u5236\u6d41<\/a>\u3001<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-4\/\">\u8981\u7d20 3 &#8211; \u638c\u63a7\u4f60\u7684\u4e0a\u4e0b\u6587\u7a97\u53e3<\/a>\u00a0\u548c\u00a0<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-5\/\">\u8981\u7d20 4 &#8211; \u5de5\u5177\u5373\u7ed3\u6784\u5316\u8f93\u51fa<\/a>\u00a0\u4ee5\u53ca\u5176\u4ed6\u51e0\u4e2a\u8981\u7d20\u7684\u6a21\u5f0f\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u4f7f\u7528\u6765\u81ea\u00a0<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-4\/\">\u8981\u7d20 3 &#8211; \u638c\u63a7\u4f60\u7684\u4e0a\u4e0b\u6587\u7a97\u53e3<\/a>\u00a0\u7684\u7c7b XML \u683c\u5f0f\uff0c\u7ecf\u8fc7\u51e0\u8f6e\u5bf9\u8bdd\u540e\uff0c\u6211\u4eec\u7684\u4e0a\u4e0b\u6587\u7a97\u53e3\u53ef\u80fd\u4f1a\u662f\u8fd9\u6837\uff1a<\/p>\n<pre><code>(\u4e3a\u7b80\u6d01\u8d77\u89c1\u5df2\u7701\u7565)\r\n&lt;slack_message&gt;\r\nFrom: @alex\r\nChannel: #deployments\r\nText: Can you deploy backend v1.2.3 to production?\r\nThread: []\r\n&lt;\/slack_message&gt;\r\n&lt;request_human_input&gt;\r\nintent: \"request_human_input\"\r\nquestion: \"Would you like to proceed with deploying v1.2.3 to production?\"\r\ncontext: \"This is a production deployment that will affect live users.\"\r\noptions: {\r\nurgency: \"high\"\r\nformat: \"yes_no\"\r\n}\r\n&lt;\/request_human_input&gt;\r\n&lt;human_response&gt;\r\nresponse: \"yes please proceed\"\r\napproved: true\r\ntimestamp: \"2024-03-15T10:30:00Z\"\r\nuser: \"alex@company.com\"\r\n&lt;\/human_response&gt;\r\n&lt;deploy_backend&gt;\r\nintent: \"deploy_backend\"\r\ntag: \"v1.2.3\"\r\nenvironment: \"production\"\r\n&lt;\/deploy_backend&gt;\r\n&lt;deploy_backend_result&gt;\r\nstatus: \"success\"\r\nmessage: \"Deployment v1.2.3 to production completed successfully.\"\r\ntimestamp: \"2024-03-15T10:30:00Z\"\r\n&lt;\/deploy_backend_result&gt;\r\n<\/code><\/pre>\n<p>\u597d\u5904:<\/p>\n<ol>\n<li><strong>\u6e05\u6670\u7684\u6307\u4ee4<\/strong>: \u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u4eba\u7c7b\u8054\u7cfb\u65b9\u5f0f\u4f7f\u7528\u4e0d\u540c\u5de5\u5177\uff0c\u53ef\u4ee5\u8ba9\u5927\u8bed\u8a00\u6a21\u578b (LLM) \u7684\u6307\u4ee4\u66f4\u5177\u4f53<\/li>\n<li><strong>\u5185\u5faa\u73af\u4e0e\u5916\u5faa\u73af<\/strong>: \u53ef\u4ee5\u5728\u4f20\u7edf\u7684 <a href=\"https:\/\/www.kdjingpai.com\/chatgpt-6\/\">chatGPT<\/a> \u5f0f\u754c\u9762\u00a0<strong>\u4e4b\u5916<\/strong>\u00a0\u542f\u7528\u4ee3\u7406\u5de5\u4f5c\u6d41\uff0c\u5176\u63a7\u5236\u6d41\u548c\u4e0a\u4e0b\u6587\u521d\u59cb\u5316\u53ef\u80fd\u662f\u00a0<code>Agent-&gt;Human<\/code>\u00a0\u800c\u4e0d\u662f\u00a0<code>Human-&gt;Agent<\/code>\u00a0(\u4f8b\u5982\uff0c\u7531 cron \u6216\u4e8b\u4ef6\u89e6\u53d1\u7684\u4ee3\u7406)<\/li>\n<li><strong>\u591a\u7528\u6237\u8bbf\u95ee<\/strong>: \u53ef\u4ee5\u901a\u8fc7\u7ed3\u6784\u5316\u4e8b\u4ef6\u8f7b\u677e\u8ddf\u8e2a\u548c\u534f\u8c03\u6765\u81ea\u4e0d\u540c\u7528\u6237\u7684\u8f93\u5165<\/li>\n<li><strong>\u591a\u4ee3\u7406<\/strong>: \u7b80\u5355\u7684\u62bd\u8c61\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u4ee5\u652f\u6301\u00a0<code>Agent-&gt;Agent<\/code>\u00a0\u7684\u8bf7\u6c42\u548c\u54cd\u5e94<\/li>\n<li><strong>\u6301\u4e45\u6027<\/strong>: \u4e0e\u00a0<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-7\/\">\u8981\u7d20 6 &#8211; \u901a\u8fc7\u7b80\u5355\u7684 API \u542f\u52a8\/\u6682\u505c\/\u6062\u590d<\/a>\u00a0\u76f8\u7ed3\u5408\uff0c\u53ef\u4ee5\u521b\u5efa\u6301\u4e45\u3001\u53ef\u9760\u4e14\u53ef\u5185\u7701\u7684\u591a\u4eba\u5de5\u4f5c\u6d41<\/li>\n<\/ol>\n<p><a href=\"https:\/\/theouterloop.substack.com\/p\/openais-realtime-api-is-a-step-towards\">\u70b9\u51fb\u6b64\u5904\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5916\u5faa\u73af\u4ee3\u7406\u7684\u4fe1\u606f<\/a><\/p>\n<p><img decoding=\"async\" title=\"\u300a12-Factor Agents\u300b7. \u4f7f\u7528\u5de5\u5177\u8c03\u7528\u4e0e\u4eba\u4ea4\u6d41-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/07\/cd48db6170a224f.jpg\" alt=\"\u300a12-Factor Agents\u300b7. \u4f7f\u7528\u5de5\u5177\u8c03\u7528\u4e0e\u4eba\u4ea4\u6d41-1\" \/><\/p>\n<p>\u4e0e\u00a0<a href=\"https:\/\/www.kdjingpai.com\/12-factor-agentsai-12\/\">\u8981\u7d20 11 &#8211; \u4ece\u4efb\u4f55\u5730\u65b9\u89e6\u53d1\uff0c\u5728\u7528\u6237\u6240\u5728\u4e4b\u5904\u4e0e\u4ed6\u4eec\u4f1a\u9762<\/a>\u00a0\u914d\u5408\u4f7f\u7528\u6548\u679c\u5f88\u597d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5927\u8bed\u8a00\u6a21\u578b (LLM) API \u4f9d\u8d56\u4e8e\u4e00\u4e2a\u6839\u672c\u6027\u7684\u9ad8\u98ce\u9669 Token \u9009\u62e9\uff1a\u6211\u4eec\u662f\u8fd4\u56de\u7eaf\u6587\u672c\u5185\u5bb9\uff0c\u8fd8\u662f\u8fd4\u56de\u7ed3\u6784\u5316\u6570\u636e\uff1f \u4f60\u5c06\u5f88\u5927\u7684\u6743\u91cd\u653e\u5728\u4e86\u7b2c\u4e00\u4e2a Token \u7684\u9009\u62e9\u4e0a\uff0c\u5728\u00a0the weather in tokyo\u00a0\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33108","post","type-post","status-publish","format-standard","hentry","category-kecheng"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/33108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=33108"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/33108\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=33108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=33108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=33108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}