{"id":7366,"date":"2024-06-10T15:14:36","date_gmt":"2024-06-10T07:14:36","guid":{"rendered":"https:\/\/aict.nkust.edu.tw\/digitrans\/?p=7366"},"modified":"2024-12-13T21:11:53","modified_gmt":"2024-12-13T13:11:53","slug":"langgraph-langchain-agent-%e7%9a%84%e6%ae%ba%e6%89%8b%e9%90%a7-%e5%85%a5%e9%96%80","status":"publish","type":"post","link":"https:\/\/aict.nkust.edu.tw\/digitrans\/?p=7366","title":{"rendered":"LangGraph: LangChain Agent \u7684\u6bba\u624b\u9427 (\u5165\u9580)"},"content":{"rendered":"\n<p>2024-06-10 | YWC \u79d1\u6280\u7b46\u8a18<\/p>\n\n\n\n<p>\u300c<em>\u563f\u5144\u5f1f\uff0c\u6211\u597d\u60f3\u4ea4\u5973\u670b\u53cb\u4f46\u90fd\u4ea4\u4e0d\u5230\uff0c\u600e\u9ebc\u8fa6\uff1f<\/em>\u300d<\/p>\n\n\n\n<p>\u8eab\u70ba AI \u5de5\u7a0b\u5e2b\uff0c\u70ba\u4e86\u5e6b\u4ed6\uff0c\u7576\u7136\u662f\u756b\u500b\u6d41\u7a0b\u5716\u554a\uff01<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/ywctech.net\/ml-ai\/langchain-langgraph-agent-part1\/dating-flowchart.webp?w=640&#038;ssl=1\" alt=\"Dating: the flow chart\"\/><\/figure>\n<\/div>\n\n\n<p>\u4ea4\u5973\u670b\u53cb\u8981\u5206\u6b65\u9a5f\uff0c\u6bcf\u500b\u6b65\u9a5f\u90fd\u6709\u55ae\u4e00\u76ee\u7684\u3002\u5982\u679c\u5931\u6557\u4e5f\u6c92\u95dc\u4fc2\uff0c\u6d41\u7a0b\u4e0a\u6211\u5011\u9000\u56de\u53bb\u53cd\u7701\u4e00\u4e0b\uff0c\u518d\u63a5\u518d\u53b2<\/p>\n\n\n\n<p>\u300c<em>\u2026\u9019\u4ec0\u9ebc\uff1f\u4e00\u9ede\u90fd\u4e0d\u5be6\u969b<\/em>\u300d<\/p>\n\n\n\n<p>\u6709\u9053\u7406\uff0c\u4e00\u5b9a\u662f\u56e0\u70ba\u6c92\u6709\u7528 LangGraph \u7684\u95dc\u4fc2\uff01<strong>\u7528\u4e86 LangGraph \u4e00\u5207\u90fd\u5be6\u969b\u4e86\u8d77\u4f86<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/ywctech.net\/ml-ai\/langchain-langgraph-agent-part1\/dating-flowchart-langgraph.webp?w=640&#038;ssl=1\" alt=\"Dating agent: use LangGraph to implement the flow chart\"\/><\/figure>\n<\/div>\n\n\n<p>\u7528 LangGraph \u628a\u6bcf\u500b\u6b65\u9a5f\u90fd\u63a5\u4e0a ChatGPT \uff08LLM \u8a9e\u8a00\u6a21\u578b\uff09\u6216\u8005\u662f\u5de5\u5177\uff08\u641c\u5c0b\uff09\uff0c<strong>\u8981\u6d41\u7a0b\u6709\u6d41\u7a0b\uff0c\u8981\u884c\u52d5\u6709\u884c\u52d5<\/strong>\uff0c\u9019\u5c31\u662f\u4ea4\u53cb&nbsp;<strong>agent \u4ee3\u7406\u4eba<\/strong><\/p>\n\n\n\n<p>\uff08\u2026 \u90a3\u500b\uff0c\u9019\u53ea\u662f\u4f8b\u5b50\uff0c\u771f\u5be6\u4e16\u754c\u4ea4\u53cb\u62dc\u8a17\u62dc\u8a17 \u8acb\u5225\u9019\u9ebc\u505a \uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u70ba\u4ec0\u9ebc\u8981-langgraph\">\u70ba\u4ec0\u9ebc\u8981 LangGraph\uff1f<\/h2>\n\n\n\n<p>\u7576\u4f60\u7684\u7522\u54c1\u9700\u8981\u4e00\u4e9b\u6d41\u7a0b\u3001\u6b65\u9a5f\uff0c\u7528&nbsp;<strong>LangGraph<\/strong>&nbsp;\u642d\u914d LangChain \u80fd\u5920<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5be6\u4f5c\u51fa\u6d41\u7a0b\u7684\u904b\u4f5c<\/li>\n\n\n\n<li>\u8f15\u6613\u628a LLM \u5f15\u5165\u6bcf\u500b\u6b65\u9a5f\u7576\u4e2d<\/li>\n\n\n\n<li>\u628a\u300c\u6d41\u7a0b\u300d\u62bd\u8c61\u51fa\u4f86\uff0c\u597d\u7dad\u8b77\u3002\u628a\u6bcf\u4e00\u500b\u6b65\u9a5f\u8907\u96dc\u7684\u5be6\u4f5c\u5c01\u88dd\u8d77\u4f86<\/li>\n<\/ul>\n\n\n\n<p>\u53ef\u662f\uff0cLangGraph \u600e\u9ebc\u505a\u5230\u5be6\u8e10\u6d41\u7a0b\u5462\uff1f\u6709\u4e09\u500b\u5143\u7d20<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"langgraph-\u662f\u4ec0\u9ebc\">LangGraph \u662f\u4ec0\u9ebc\uff1f<\/h2>\n\n\n\n<p>LangGraph \u50cf\u662f\u300c\u5728 python \u88e1\u9762\u5beb\u53e6\u4e00\u500b\u8a9e\u8a00\u7a0b\u5f0f\u300d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>State<\/strong>: \u5982\u540c\u8b8a\u6578\u8868<\/li>\n\n\n\n<li><strong>Node<\/strong>: \u505a\u4e8b\u60c5 \/ function<\/li>\n\n\n\n<li><strong>Edge<\/strong>: \u6d41\u7a0b\u63a7\u5236<\/li>\n<\/ul>\n\n\n\n<p>\u592a\u62bd\u8c61\uff1f\u7d66\u500b\u7c21\u55ae\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># **State**\nclass MyState(TypedDict):  # from typing import TypedDict\n    i: int\n    j: int\n\n# Functions on **nodes**\ndef fn1(state: MyState):\n    print(f\"Enter fn1: {state&#91;'i']}\")\n    return {\"i\": 1}\n\ndef fn2(state: MyState):\n    i = state&#91;\"i\"]\n    return {\"i\": i+1}\n\n# Conditional **edge** function\ndef is_big_enough(state: MyState):\n    if state&#91;\"i\"] &gt; 10:\n        return END\n    else:\n        return \"n2\"\n\n# The Graph!  The \"Program\" !!\nworkflow = StateGraph(MyState)\n\nworkflow.add_node(\"n1\", fn1)\nworkflow.add_node(\"n2\", fn2)\nworkflow.set_entry_point(\"n1\")\n\nworkflow.add_edge(\"n1\", \"n2\")\nworkflow.add_conditional_edges(\n    source=\"n2\", path=is_big_enough\n)\n\n# Compile, and then run\ngraph = workflow.compile()\nr = graph.invoke({\"i\": 1000, \"j\": 123})\nprint(r)\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/ywctech.net\/ml-ai\/langchain-langgraph-agent-part1\/example-graph.webp?w=640&#038;ssl=1\" alt=\"Flow diagram of my LangGraph example\"\/><figcaption class=\"wp-element-caption\">\u9019\u500b graph \u7684\u8996\u89ba\u5316<\/figcaption><\/figure>\n<\/div>\n\n\n<p>\u731c\u731c\u8f38\u51fa\u9577\u600e\u6a23\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter fn1: 1000\n{'i': 11, 'j': 123}\n<\/code><\/pre>\n\n\n\n<p>\u9019\u5176\u5be6\u5f88\u50cf\u4e0b\u9762\u9019\u500b python \u7a0b\u5f0f\u5427<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 1\nwhile True:\n    i += 1\n    if i &gt; 10:\n        break\n<\/code><\/pre>\n\n\n\n<p>\u8ddf\u55ae\u7d14\u5beb python \u7a0b\u5f0f\u7684\u4e0d\u540c\u5728\uff0cLangGraph \u80fd\u5920\u8b93\u6bcf\u4e00\u500b\u300c\u6b65\u9a5f\u300d\u90fd\u5f88\u8907\u96dc\uff0c<strong>\u5f15\u5165 LLM \u8ddf\u5de5\u5177\u7684\u8655\u7406\uff0c\u628a\u300c\u6d41\u7a0b\u300d\u62bd\u8c61\u51fa\u4f86<\/strong>\uff0c\u8b8a\u5f97\u4e7e\u6de8\u597d\u7dad\u8b77<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"graph-\u50cf\u662f\u4e00\u500b\u7a0b\u5f0f\">Graph \u50cf\u662f\u4e00\u500b\u7a0b\u5f0f<\/h2>\n\n\n\n<p>\u5728\u9019\u500b\u4f8b\u5b50&nbsp;<code>MyState<\/code>&nbsp;\u5176\u5be6\u662f\u4e00\u500b\u5b57\u5178\uff0c\u5148\u77e5\u9053\u9019\u6a23\uff0c\u5f8c\u9762\u6709\u66f4\u591a\u89e3\u91cb<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"node\">Node<\/h3>\n\n\n\n<p>\u505a\u4e8b\u7684\u662f&nbsp;<code>fn1<\/code>&nbsp;\u8ddf&nbsp;<code>fn2<\/code>&nbsp;\u5169\u500b function\uff0c\u56e0\u70ba\u4ed6\u5011\u7531&nbsp;<code>add_node()<\/code>&nbsp;\u7d81\u5728\u7bc0\u9ede\u4e0a \u2013 \u524d\u9762\u8aaa\u4e86 Node \u5c31\u662f\u5728\u505a\u4e8b<\/p>\n\n\n\n<p>\u4f55\u8b02\u505a\u4e8b\uff1f\u901a\u5e38\u662f\u300c\u6539\u8b8a state\u300d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fn1(state: MyState):\n    print(f\"Enter fn1: {state&#91;'i']}\")\n    return {\"i\": 1}\n\ndef fn2(state: MyState):\n    i = state&#91;\"i\"]\n    return {\"i\": i+1}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u60f3\u50cf state (\u72c0\u614b) \u662f\u5c6c\u65bc\u9019\u500b graph \u7684\u300c\u8b8a\u6578\u8868\u300d<\/li>\n\n\n\n<li><code>fn1<\/code>&nbsp;\u5148\u5370\u50b3\u5165\u7684 state\uff1b<strong>\u56de\u50b3<\/strong>&nbsp;<code>{\"i\": 1}<\/code>&nbsp;\u4ee3\u8868\u300c\u4e0d\u7ba1 state \u7684 i \u4ee5\u524d\u662f\u591a\u5c11\uff0c\u73fe\u5728\u8986\u84cb\u6389\uff0c\u8b8a\u6210 1 \u300d<\/li>\n\n\n\n<li>\u540c\u7406\uff0c<code>fn2<\/code>&nbsp;\u628a state \u7684 i \u52a0\u4e0a 1 \u4ee5\u5f8c\u56de\u50b3\uff0c\u4ee3\u8868\u300cstate \u88e1\u9762\u7684 i \u591a\u52a0 1 \u300d\n<ul class=\"wp-block-list\">\n<li>\u6ce8\u610f&nbsp;<code>state[\"i\"]<\/code>&nbsp;\u662f\u9019\u500b graph \u88e1\u9762\u7684\u8b8a\u6578\uff0c\u5728\u5176\u4ed6\u7bc0\u9ede\u7684 function \u4e5f\u80fd\u5b58\u53d6\u3002\u4f46\u88f8\u8457\u7684&nbsp;<code>i<\/code>&nbsp;\u662f\u4f60\u9019\u500b python \u7a0b\u5f0f\u88e1\u9762\u7684&nbsp;<code>i<\/code>\uff0c\u5169\u8005\u4e0d\u540c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u5149\u662f\u5ba3\u544a function \u4e0d\u5920\uff0c\u9084\u8981\u7d81\u5230 graph \u4e0a\uff0c\u7d66\u6bcf\u500b node \u4e00\u500b\u540d\u5b57<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workflow.add_node(\"n1\", fn1)\nworkflow.add_node(\"n2\", fn2)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"edge\">Edge<\/h3>\n\n\n\n<p>\u57f7\u884c\u9806\u5e8f\u5462\uff1f\u9019\u5c31\u662f \u201cedge\u201d \u63a7\u5236\u4e86\uff1a\u5f9e\u4e00\u500b\u9ede\u6709\u65b9\u5411\u5730\u9023\u5230\u53e6\u4e00\u500b\u9ede<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workflow.set_entry_point(\"n1\")\nworkflow.add_edge(\"n1\", \"n2\")\nworkflow.add_conditional_edges(\n    source=\"n2\", path=is_big_enough\n)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.set_entry_point()<\/code>&nbsp;\u6307\u5b9a\u5f9e\u54ea\u500b node \u958b\u59cb\u57f7\u884c<\/li>\n\n\n\n<li><code>.add_edge(\"n1\", \"n2\")<\/code>&nbsp;\u8868\u793a\u7576 n1 \u57f7\u884c\u5b8c\u4ee5\u5f8c\uff0c\u4e0b\u4e00\u6b65\u5c31\u4ea4\u7d66 n2 \u57f7\u884c<\/li>\n\n\n\n<li>\u5beb\u7a0b\u5f0f\u7684 \u201cif\u201d \u7528&nbsp;<code>.add_conditional_edges()<\/code>&nbsp;\u9054\u6210\uff0c\u5403\u81f3\u5c11\u5169\u500b\u53c3\u6578\n<ul class=\"wp-block-list\">\n<li><code>source<\/code>&nbsp;\u8868\u793a\u8d77\u9ede<\/li>\n\n\n\n<li><code>path<\/code>&nbsp;\u662f\u4e00\u500b function \u6c7a\u5b9a\u4e0b\u4e00\u6b65\u7d66\u8ab0\uff1a\u770b\u4ed6\u56de\u50b3\u7684\u5b57\u4e32\u4ee3\u8868\u54ea\u500b node<\/li>\n\n\n\n<li>\u6211\u7684\u7bc4\u4f8b&nbsp;<code>is_big_enough<\/code>&nbsp;\u9019\u500b\u51fd\u6578\uff0c\u53bb\u770b state \u8b8a\u6578\u8868\u88e1\u7684 i \u6709\u6c92\u6709\u5927\u65bc 10\uff0c\u6c92\u6709\u7684\u8a71\u5c31\u56de\u50b3 \u201cn2\u201d\uff0c\u4e5f\u5c31\u662f\u4e0b\u4e00\u68d2\u518d\u4ea4\u7d66\u81ea\u5df1\u3002\u5927\u65bc 10 \u7684\u8a71\u5c31\u7d50\u675f (<code>END<\/code>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_big_enough(state: MyState):\n    if state&#91;\"i\"] &gt; 10:\n        return END\n    else:\n        return \"n2\"\n<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u6574\u500b Graph \u770b\u8d77\u4f86\u50cf\u500b\u5c0f\u7a0b\u5f0f\uff01&nbsp;<strong>\u628a workflow \u7576\u4f5c\u5c0f\u7a0b\u5f0f\u78bc\u3001state \u7576\u4f5c\u5c0f\u8b8a\u6578\u8868\uff0ccompile \u7de8\u8b6f\u6210\u5c0f\u57f7\u884c\u6a94<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>graph = workflow.compile()\nr = graph.invoke({\"i\": 1000, \"j\": 123})\nprint(r)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6574\u500b graph \u9700\u8981&nbsp;<code>.compile()<\/code>&nbsp;\u624d\u662f\u4e00\u500b\u80fd\u57f7\u884c\u7684 graph\u3002\u800c Compile \u4e4b\u5f8c\uff0c\u5f8c\u52a0\u7684 node\/edge \u4e0d\u6703\u53cd\u6620\u5728\u4e0a\u9762<\/li>\n\n\n\n<li><code>.invoke()<\/code>&nbsp;\u662f\u8f38\u5165\u521d\u59cb\u7684 state \u53bb\u57f7\u884c\uff1b\u8f38\u51fa\u662f\u6700\u5f8c\u7684 state \u9577\u600e\u6a23<\/li>\n\n\n\n<li>\u96d6\u7136\u6240\u6709\u7bc0\u9ede\u90fd\u6c92\u78b0\u89f8&nbsp;<code>j<\/code>&nbsp;\u2013 \u4e5f\u5c31\u662f\u56de\u50b3\u7684\u90fd\u662f&nbsp;<strong>partial state<\/strong>&nbsp;\uff0c\u4f46 state \u7684 schema \u662f TypeDict, \u88e1\u9762\u6709\u5ba3\u544a&nbsp;<code>j<\/code>\uff0c\u6240\u4ee5\u5982\u679c\u521d\u59cb\u6709\u7d66&nbsp;<code>j<\/code>&nbsp;\u503c\uff0c\u4e5f\u5c31\u9019\u6a23\u4fdd\u7559\u8457\uff0c\u6700\u5f8c\u7684\u8f38\u51fa\u6703\u6709&nbsp;<code>j<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter fn1: 1000\n{'i': 11, 'j': 123}\n<\/code><\/pre>\n\n\n\n<p>\u5c0d\u4e86\uff0c\u6709\u6c92\u6709\u767c\u73fe\uff1a<strong>\u9019\u7bc4\u4f8b\u5b8c\u5168\u6c92 AI \u6a21\u578b<\/strong>\u3002LangGraph \u672c\u8cea\u4e0a\u5c31\u662f\u500b \u201cgraph processing\u201d engine \u800c\u5df2<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"state-state_schema-\u8ddf-typeddict\">State, state_schema, \u8ddf TypedDict<\/h2>\n\n\n\n<p>\u6709\u500b\u6700\u91cd\u8981\u6c92\u63d0\u7684\u662f\uff1a\u8b8a\u6578\u8868\u600e\u9ebc\u5ba3\u544a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import TypedDict\nfrom langgraph.graph import StateGraph\n\nclass MyState(TypedDict):\n    i: int\n    j: int\n\n...\ndef fn2(state: MyState):\n    i = state&#91;\"i\"]\n    return {\"i\": i+1}\n\n...\nworkflow = StateGraph(state_schema=MyState)\n<\/code><\/pre>\n\n\n\n<p>TypedDict&nbsp;\u662f python \u88e1\u7684\u4e00\u7a2e\u578b\u614b\u5ba3\u544a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u8d77\u4f86\u8ddf\u5b57\u5178&nbsp;<code>dict<\/code>&nbsp;\u4e00\u6a21\u4e00\u6a23\u3002\u53ea\u4e0d\u904e\u4ed6\u9810\u544a\u6709\u54ea\u4e9b key \u8207\u5176\u578b\u614b<\/li>\n\n\n\n<li>\u4f8b\u5982\u7bc4\u4f8b\u4e2d&nbsp;<code>MyState<\/code>&nbsp;\u7e7c\u627f&nbsp;<code>TypedDict<\/code>\uff0c\u53ea\u662f\u95e1\u8ff0&nbsp;<code>MyState<\/code>&nbsp;\u662f\u4e00\u500b\u5b57\u5178\uff0c\u300c\u9810\u671f\u300d\u6703\u6709 i \u8ddf j \u5169\u500b key\uff0c\u800c\u4e14\u503c\u90fd\u662f\u6574\u6578<\/li>\n\n\n\n<li>\u5be6\u969b\u57f7\u884c\u7684\u6642\u5019\uff0c\u5c11\u4e86 key \u6216\u591a\u4e86 key \uff0c\u662f\u4e0d\u662f\u6574\u6578\uff0c\u90fd&nbsp;<strong>\u4e0d\u6703\u5831\u932f<\/strong>&nbsp;\uff08\u53ea\u662f\u7d66\u578b\u5225\u6aa2\u67e5\u5668\u770b\u800c\u5df2\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u524d\u9762\u8aaa\u7684 workflow, \u90a3\u4e9b\u9ede\u554a\u7dda\u7684\uff0c\u5176\u5be6\u662f\u4e00\u500b&nbsp;<code>StateGraph<\/code>&nbsp;\u7269\u4ef6\u3002\u5275\u9020\u6642\u8981\u8ddf\u4ed6\u8aaa state \u7684\u578b\u614b (<code>state_schema<\/code>)<\/p>\n\n\n\n<p>\u7b49\u7b49\uff0c\u5982\u679c\u57f7\u884c\u6642\u4e0d\u6703\u5831\u932f\uff0c\u70ba\u4ec0\u9ebc\u9084\u8981&nbsp;<code>TypedDict<\/code>&nbsp;\uff08\u6216\u8005\u8aaa\uff0c\u5305\u4e00\u5c64&nbsp;<code>MyState<\/code>\uff09?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u56e0\u70ba\u5728\u5275\u9020&nbsp;<code>StateGraph<\/code>&nbsp;\u7269\u4ef6\u6642\uff0c\u4f60\u53ef\u4ee5\u7d66 LangGraph \u9019\u500b\u300c\u9810\u671f\u300d\uff0c\u8ddf\u4ed6\u8aaa\u9019\u500b state \u5408\u6cd5\u7684 key \u6709\u9019\u5169\u500b i \u8ddf j\uff0c\u5176\u4ed6\u4e0d\u7528\u7406\u6703<\/li>\n\n\n\n<li>\u7528&nbsp;<code>TypedDict<\/code>&nbsp;\u5ba3\u544a\u6bcf\u500b key \u7684\u578b\u614b\uff0c\u9084\u53ef\u4ee5\u66f4\u9032\u4e00\u6b65\u662d\u544a LangGraph \u8a72\u600e\u9ebc\u300c\u5408\u4f75\u300d(reduce) \u540c\u4e00\u500b key\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u55ae\u7d14\u7528&nbsp;<code>StateGraph(state_schema=dict)<\/code>\uff0c\u6c92\u6709\u9810\u544a\uff0cLangGraph \u4e0d\u77e5\u9053\u6bcf\u500b node \u56de\u50b3\u7684 key \u91cd\u4e0d\u91cd\u8981\uff0c\u65bc\u662f\u5be6\u4f5c\u4e0a\u6703\u300c\u5f88\u5feb\u5fd8\u8a18\u300d\u90a3\u4e9b key : \u5982\u679c\u67d0\u500b\u6b65\u9a5f\u6c92\u6709\u56de\u50b3\u67d0\u500b key\uff0c\u4e0b\u4e00\u6b65\u5c31\u6703\u5931\u53bb\u90a3\u500b key \u7684\u8cc7\u8a0a\u3002\u4ee5\u6211\u7684\u7bc4\u4f8b\u4f86\u8aaa\uff0c\u6700\u5f8c\u8f38\u51fa\u4e0d\u6703\u6709 j \u4e86\uff0c\u56e0\u70ba&nbsp;<code>fn1<\/code>&nbsp;\u56de\u50b3\u7684\u5b57\u5178\u4e26\u6c92\u6709 j \u9019\u500b key\uff0c\u5c31\u6703\u5fd8\u8a18 invoke \u4e00\u958b\u59cb\u50b3\u9032\u53bb\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workflow = StateGraph(state_schema=MyState)\n.invoke({\"i\": 1000, \"j\": 123}) \u5f8c\u7684\u7d50\u679c\u662f\n{'i': 11, 'j': 123}\n\nworkflow = StateGraph(state_schema=dict)\n.invoke({\"i\": 1000, \"j\": 123}) \u5f8c\u7684\u7d50\u679c\u662f\n{'i': 11}\n<\/code><\/pre>\n\n\n\n<p>\u96d6\u7136\u00a0<code>state_schema<\/code>\u00a0\u6c92\u6709\u9650\u5236\u578b\u614b\uff0c\u4e0d\u904e\u901a\u5e38\u6703\u662f\u00a0<code>TypedDict<\/code>\u00a0\u6216\u00a0<code>pydantic.BaseModel<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"graphstream\">graph.stream()<\/h2>\n\n\n\n<p>graph \u9664\u4e86&nbsp;<code>.invoke()<\/code>&nbsp;\u9084\u53ef\u4ee5&nbsp;<code>.stream()<\/code>, \u628a\u6bcf\u500b node \u6b65\u9a5f\u300c\u5f71\u97ff\u5230\u7684 state key\u300d \u7684\u300c\u7d50\u679c\u300d\u62ff\u51fa\u4f86<\/p>\n\n\n\n<p>\uff08\u6709\u9ede\u62d7\u53e3\uff0c\u4e0b\u4e00\u7bc7\u6703\u89e3\u91cb\u66f4\u8a73\u7d30\uff0c\u73fe\u5728\u5c31\u5148\u7576\u4f5c\u662f\u6bcf\u500b\u6b65\u9a5f\u7684\u7d50\u679c\u5c31\u597d\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for s in graph.stream({\"i\": 1000}):\n    print(s)\n<\/code><\/pre>\n\n\n\n<p>\u6703\u8f38\u51fa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter fn1: 1000\n{'n1': {'i': 1}}\n{'n2': {'i': 2}}\n... \u592a\u9577\u4e86\n{'n2': {'i': 10}}\n{'n2': {'i': 11}}\n<\/code><\/pre>\n\n\n\n<p>\u5169\u500b\u8981\u6ce8\u610f\u7684<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9019\u908a\u7684 stream \u4e0d\u662f\u300cLLM token \u4e00\u500b\u500b\u5370\u51fa\u4f86\u300d\uff0c\u800c\u662f\u6307\u300c\u4e00\u6709 node \u5b8c\u6210\u6b65\u9a5f\u5c31\u5370\u51fa\u4f86\u300d<\/li>\n\n\n\n<li>\u4ed6\u53ea\u6703\u56de\u50b3\u5f71\u97ff\u5230\u7684 key\uff0c\u6240\u4ee5\u4f60\u4e0d\u6703\u770b\u5230 j<\/li>\n<\/ul>\n\n\n\n<p>\u8cc7\u6599\u4f86\u6e90: <a href=\"https:\/\/ywctech.net\/ml-ai\/langchain-langgraph-agent-part1\/\">https:\/\/ywctech.net\/ml-ai\/langchain-langgraph-agent-part1\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2024-06-10 | YWC \u79d1\u6280\u7b46\u8a18 \u300c\u563f\u5144\u5f1f\uff0c\u6211\u597d\u60f3\u4ea4\u5973\u670b\u53cb\u4f46\u90fd\u4ea4\u4e0d\u5230\uff0c\u600e\u9ebc\u8fa6\uff1f\u300d \u8eab\u70ba AI \u5de5\u7a0b\u5e2b\uff0c\u70ba\u4e86\u5e6b\u4ed6\uff0c\u7576\u7136\u662f\u756b\u500b\u6d41\u7a0b\u5716\u554a\uff01 \u4ea4\u5973\u670b\u53cb\u8981\u5206\u6b65\u9a5f\uff0c\u6bcf\u500b\u6b65\u9a5f\u90fd\u6709\u55ae\u4e00\u76ee&hellip;<\/p>\n","protected":false},"author":4,"featured_media":7367,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[579,4],"tags":[40],"class_list":["post-7366","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-579","category-industry-news","tag-40"],"gutentor_comment":0,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/aict.nkust.edu.tw\/digitrans\/wp-content\/uploads\/2024\/10\/%E8%9E%A2%E5%B9%95%E6%93%B7%E5%8F%96%E7%95%AB%E9%9D%A2-2024-10-31-151807.png?fit=742%2C417&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/7366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7366"}],"version-history":[{"count":2,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/7366\/revisions"}],"predecessor-version":[{"id":7369,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/7366\/revisions\/7369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/media\/7367"}],"wp:attachment":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}