1"use strict";(self.webpackChunkspring_ai_alibaba_website=self.webpackChunkspring_ai_alibaba_website||[]).push([[3095],{28453:(a,e,n)=>{n.d(e,{R:()=>p,x:()=>o});var t=n(96540);const r={},i=t.createContext(r);function p(a){const e=t.useContext(i);return t.useMemo(function(){return"function"==typeof a?a(e):{...e,...a}},[e,a])}function o(a){let e;return e=a.disableParentContext?"function"==typeof a.components?a.components(r):a.components||r:p(a.components),t.createElement(i.Provider,{value:e},a.children)}},31671:(a,e,n)=>{n.r(e),n.d(e,{assets:()=>l,contentTitle:()=>o,default:()=>u,frontMatter:()=>p,metadata:()=>t,toc:()=>s});const t=JSON.parse('{"id":"frameworks/graph-core/examples/subgraph-as-nodeaction","title":"\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c","description":"\u5b66\u4e60\u5982\u4f55\u5c06\u5b50\u56fe\u4f5c\u4e3aNodeAction\u5728\u7236\u56fe\u4e2d\u4f7f\u7528\uff0c\u5b9e\u73b0\u6a21\u5757\u5316\u7684\u5de5\u4f5c\u6d41\u8bbe\u8ba1","source":"@site/docs/frameworks/graph-core/examples/subgraph-as-nodeaction.md","sourceDirName":"frameworks/graph-core/examples","slug":"/frameworks/graph-core/examples/subgraph-as-nodeaction","permalink":"/docs/frameworks/graph-core/examples/subgraph-as-nodeaction","draft":false,"unlisted":false,"editUrl":"https://github.com/spring-ai-alibaba/website/tree/main/docs/frameworks/graph-core/examples/subgraph-as-nodeaction.md","tags":[],"version":"current","lastUpdatedBy":"Ken Liu","lastUpdatedAt":1763556354000,"frontMatter":{"title":"\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c","description":"\u5b66\u4e60\u5982\u4f55\u5c06\u5b50\u56fe\u4f5c\u4e3aNodeAction\u5728\u7236\u56fe\u4e2d\u4f7f\u7528\uff0c\u5b9e\u73b0\u6a21\u5757\u5316\u7684\u5de5\u4f5c\u6d41\u8bbe\u8ba1","keywords":["\u5b50\u56fe","Subgraph","NodeAction","\u6a21\u5757\u5316","\u5de5\u4f5c\u6d41\u7ec4\u5408","Graph\u5d4c\u5957"]},"sidebar":"docsSidebar","previous":{"title":"\u5b50\u56fe\u4f5c\u4e3a CompiledGraph","permalink":"/docs/frameworks/graph-core/examples/subgraph-as-compiledgraph"},"next":{"title":"\u5b50\u56fe\u4f5c\u4e3a StateGraph","permalink":"/docs/frameworks/graph-core/examples/subgraph-as-stategraph"}}');var r=n(74848),i=n(28453);const p={title:"\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c",description:"\u5b66\u4e60\u5982\u4f55\u5c06\u5b50\u56fe\u4f5c\u4e3aNodeAction\u5728\u7236\u56fe\u4e2d\u4f7f\u7528\uff0c\u5b9e\u73b0\u6a21\u5757\u5316\u7684\u5de5\u4f5c\u6d41\u8bbe\u8ba1",keywords:["\u5b50\u56fe","Subgraph","NodeAction","\u6a21\u5757\u5316","\u5de5\u4f5c\u6d41\u7ec4\u5408","Graph\u5d4c\u5957"]},o="\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c",l={},s=[{value:"\u6982\u5ff5",id:"\u6982\u5ff5",level:2},{value:"\u57fa\u672c\u793a\u4f8b",id:"\u57fa\u672c\u793a\u4f8b",level:2},{value:"\u5b9a\u4e49\u5b50\u56fe",id:"\u5b9a\u4e49\u5b50\u56fe",level:3},{value:"\u5c06\u5b50\u56fe\u5305\u88c5\u4e3a NodeAction",id:"\u5c06\u5b50\u56fe\u5305\u88c5\u4e3a-nodeaction",level:3},{value:"\u5728\u7236\u56fe\u4e2d\u4f7f\u7528",id:"\u5728\u7236\u56fe\u4e2d\u4f7f\u7528",level:3},{value:"\u6267\u884c\u7236\u56fe",id:"\u6267\u884c\u7236\u56fe",level:3},{value:"\u9ad8\u7ea7\u793a\u4f8b - \u5e26\u53c2\u6570\u7684\u5b50\u56fe",id:"\u9ad8\u7ea7\u793a\u4f8b---\u5e26\u53c2\u6570\u7684\u5b50\u56fe",level:2},{value:"\u72b6\u6001\u6620\u5c04",id:"\u72b6\u6001\u6620\u5c04",level:2},{value:"\u5e94\u7528\u573a\u666f",id:"\u5e94\u7528\u573a\u666f",level:2},{value:"\u9644\u56fe",id:"\u9644\u56fe",level:2},{value:"\u76f8\u5173\u6587\u6863",id:"\u76f8\u5173\u6587\u6863",level:2}];function c(a){const e={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",img:"img",li:"li",ol:"ol",p:"p",strong:"strong",ul:"ul",...(0,i.R)(),...a.components},{Code:t}=e;return t||function(a,e){throw new Error("Expected "+(e?"component":"object")+" `"+a+"` to be defined: you likely forgot to import, pass, or provide it.")}("Code",!0),(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(e.header,{children:(0,r.jsx)(e.h1,{id:"\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c",children:"\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\u64cd\u4f5c"})}),"\n",(0,r.jsx)(e.p,{children:"\u5728 Spring AI Alibaba Graph \u4e2d\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b8c\u6574\u7684 Graph \u4f5c\u4e3a\u53e6\u4e00\u4e2a Graph \u7684\u8282\u70b9\uff0c\u5b9e\u73b0\u5de5\u4f5c\u6d41\u7684\u6a21\u5757\u5316\u8bbe\u8ba1\u3002"}),"\n",(0,r.jsx)(e.h2,{id:"\u6982\u5ff5",children:"\u6982\u5ff5"}),"\n",(0,r.jsxs)(e.p,{children:["\u5c06\u5b50\u56fe\u4f5c\u4e3a ",(0,r.jsx)(e.code,{children:"NodeAction"})," \u4f7f\u7528\uff0c\u53ef\u4ee5\uff1a"]}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u6a21\u5757\u5316"}),": \u5c06\u590d\u6742\u5de5\u4f5c\u6d41\u62c6\u5206\u4e3a\u53ef\u91cd\u7528\u7684\u5b50\u6a21\u5757"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u5c01\u88c5"}),": \u9690\u85cf\u5b50\u56fe\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u7ec4\u5408"}),": \u6784\u5efa\u5c42\u6b21\u5316\u7684\u5de5\u4f5c\u6d41\u7ed3\u6784"]}),"\n"]}),"\n",(0,r.jsx)(e.h2,{id:"\u57fa\u672c\u793a\u4f8b",children:"\u57fa\u672c\u793a\u4f8b"}),"\n",(0,r.jsx)(e.h3,{id:"\u5b9a\u4e49\u5b50\u56fe",children:"\u5b9a\u4e49\u5b50\u56fe"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u5b9a\u4e49\u5b50\u56fe",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:'import
1com.alibaba.cloud.ai.graph.CompiledGraph;\nimport com.alibaba.cloud.ai.graph.KeyStrategy;\nimport com.alibaba.cloud.ai.graph.KeyStrategyFactory;\nimport com.alibaba.cloud.ai.graph.StateGraph;\nimport com.alibaba.cloud.ai.graph.exception.GraphStateException;\nimport com.alibaba.cloud.ai.graph.state.strategy.ReplaceStrategy;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport static com.alibaba.cloud.ai.graph.StateGraph.END;\nimport static com.alibaba.cloud.ai.graph.StateGraph.START;\nimport static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;\n\n/**\n* \u5b9a\u4e49\u5b50\u56fe\n*/\npublic static CompiledGraph createSubGraph(KeyStrategyFactory keyStrategyFactory) throws GraphStateException {\n StateGraph subGraph = new StateGraph(keyStrategyFactory)\n .addNode("substep1", node_async(state -> {\n String input = (String) state.value("input").orElse("");\n return Map.of("result", "SubStep1:" + input);\n }))\n .addNode("substep2", node_async(state -> {\n String prev = (String) state.value("result").orElse("");\n return Map.of("result", prev + "->SubStep2");\n }))\n .addEdge(START, "substep1")\n .addEdge("substep1", "substep2")\n .addEdge("substep2", END);\n\n return subGraph.compile();\n}'}),"\n",(0,r.jsx)(e.h3,{id:"\u5c06\u5b50\u56fe\u5305\u88c5\u4e3a-nodeaction",children:"\u5c06\u5b50\u56fe\u5305\u88c5\u4e3a NodeAction"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u5c06\u5b50\u56fe\u5305\u88c5\u4e3a NodeAction",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:'import com.alibaba.cloud.ai.graph.CompiledGraph;\nimport com.alibaba.cloud.ai.graph.OverAllState;\nimport com.alibaba.cloud.ai.graph.RunnableConfig;\nimport com.alibaba.cloud.ai.graph.action.NodeAction;\n\nimport java.util.Map;\nimport java.util.Optional;\n\n/**\n* \u5c06\u5b50\u56fe\u5305\u88c5\u4e3a NodeAction\n*/\npublic static class SubGraphNode implements NodeAction {\n\n private final CompiledGraph subGraph;\n\n public SubGraphNode(CompiledGraph subGraph) {\n this.subGraph = subGraph;\n }\n\n @Override\n public Map<String, Object> apply(OverAllState state) {\n // \u4ece\u7236\u72b6\u6001\u63d0\u53d6\u5b50\u56fe\u9700\u8981\u7684\u6570\u636e\n String input = (String) state.value("data").orElse("");\n\n // \u6267\u884c\u5b50\u56fe\n Map<String, Object> subInput = Map.of("input", input);\n Optional<OverAllState> subResult = subGraph.invoke(subInput, RunnableConfig.builder().build());\n\n // \u8fd4\u56de\u7ed3\u679c\u7ed9\u7236\u56fe\n String result = (String) subResult.get().value("result").orElse("");\n return Map.of("processed", result);\n }\n}'}),"\n",(0,r.jsx)(e.h3,{id:"\u5728\u7236\u56fe\u4e2d\u4f7f\u7528",children:"\u5728\u7236\u56fe\u4e2d\u4f7f\u7528"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u5728\u7236\u56fe\u4e2d\u4f7f\u7528",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:'import com.alibaba.cloud.ai.graph.CompiledGraph;\nimport com.alibaba.cloud.ai.graph.StateGraph;\n\nimport static com.alibaba.cloud.ai.graph.StateGraph.END;\nimport static com.alibaba.cloud.ai.graph.StateGraph.START;\nimport static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;\n\n/**\n* \u5728\u7236\u56fe\u4e2d\u4f7f\u7528\n*/\npublic static CompiledGraph useInParentGraph(KeyStrategyFactory keyStrategyFactory, CompiledGraph subGraph) throws GraphStateException {\nSubGraphNode subGraphNode = new SubGraphNode(subGraph);\n\nStateGraph parentGraph = new StateGraph(keyStrategyFactory)\n .addNode("prepare", node_async(state -> {\n return Map.of("data", "Input Data");\n }))\n .addNode("process", node_async(subGraphNode)) // \u4f7f\u7528\u5b50\u56fe\u4f5c\u4e3a\u8282\u70b9\n .addNode("finalize", node_async(state -> {\n String processed = (String) state.value("processed").orElse("");\n return Map.of("final", "Final:" + processed);\n }))\n .addEdge(START, "prepare")\n .addEdge("prepare", "process")\n .addEdge("process", "finalize")\n .addEdge("finalize", END);\n\n return parentGraph.compile();\n}'}),"\n",(0,r.jsx)(e.h3,{id:"\u6267\u884c\u7236\u56fe",children:"\u6267\u884c\u7236\u56fe"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u6267\u884c\u7236\u56fe",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:'// \u6267\u884c\nOverAllState result = compiledParent.invoke(Map.of());\nSystem.out.println("Final result: " + result.value("final").orElse(""));\n\n// \u8f93\u51fa: Final result: Final:SubStep1:Input Data->SubStep2'}),"\n",(0,r.jsx)(e.h2,{id:"\u9ad8\u7ea7\u793a\u4f8b---\u5e26\u53c2\u6570\u7684\u5b50\u56fe",children:"\u9ad8\u7ea7\u793a\u4f8b - \u5e26\u53c2\u6570\u7684\u5b50\u56fe"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u5e26\u53c2\u6570\u7684\u5b50\u56fe",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:'// \u53ef\u914d\u7f6e\u7684\u5b50\u56fe\u8282\u70b9\npublic class ConfigurableSubGraphNode implements NodeAction {\n\n private final CompiledGraph subGraph;
1\n private final String inputKey;\n private final String outputKey;\n\n public ConfigurableSubGraphNode(\n CompiledGraph subGraph,\n String inputKey,\n String outputKey\n ) {\n this.subGraph = subGraph;\n this.inputKey = inputKey;\n this.outputKey = outputKey;\n }\n\n @Override\n public Map<String, Object> apply(OverAllState state) {\n // \u4ece\u7236\u72b6\u6001\u8bfb\u53d6\u6307\u5b9a\u952e\u7684\u6570\u636e\n Object input = state.value(inputKey).orElse(null);\n\n // \u6267\u884c\u5b50\u56fe\n OverAllState subResult = subGraph.invoke(Map.of("input", input));\n\n // \u5c06\u7ed3\u679c\u5199\u5165\u6307\u5b9a\u952e\n Object output = subResult.value("result").orElse(null);\n return Map.of(outputKey, output);\n }\n}\n\n// \u4f7f\u7528\nCompiledGraph subGraph = createSubGraph(keyStrategyFactory);\nNodeAction configurableNode = new ConfigurableSubGraphNode(\n subGraph,\n "userInput", // \u4ece\u7236\u72b6\u6001\u8bfb\u53d6 userInput\n "processedOutput" // \u5199\u5165 processedOutput\n);'}),"\n",(0,r.jsx)(e.h2,{id:"\u72b6\u6001\u6620\u5c04",children:"\u72b6\u6001\u6620\u5c04"}),"\n",(0,r.jsx)(e.p,{children:"\u5904\u7406\u7236\u5b50\u56fe\u4e4b\u95f4\u7684\u72b6\u6001\u6620\u5c04\uff1a"}),"\n",(0,r.jsx)(t,{language:"java",title:"\u72b6\u6001\u6620\u5c04",sourceUrl:"https://github.com/alibaba/spring-ai-alibaba/tree/main/examples/documentation/src/main/java/com/alibaba/cloud/ai/examples/documentation/graph/examples/SubgraphAsNodeActionExample.java",children:"public class SubGraphNodeWithMapping implements NodeAction {\n\n private final CompiledGraph subGraph;\n private final Function<OverAllState, Map<String, Object>> inputMapper;\n private final BiFunction<OverAllState, OverAllState, Map<String, Object>> outputMapper;\n\n public SubGraphNodeWithMapping(\n CompiledGraph subGraph,\n Function<OverAllState, Map<String, Object>> inputMapper,\n BiFunction<OverAllState, OverAllState, Map<String, Object>> outputMapper\n ) {\n this.subGraph = subGraph;\n this.inputMapper = inputMapper;\n this.outputMapper = outputMapper;\n }\n\n @Override\n public Map<String, Object> apply(OverAllState parentState) {\n // \u4f7f\u7528 mapper \u8f6c\u6362\u7236\u72b6\u6001\u5230\u5b50\u72b6\u6001\n Map<String, Object> subInput = inputMapper.apply(parentState);\n\n // \u6267\u884c\u5b50\u56fe\n OverAllState subResult = subGraph.invoke(subInput);\n\n // \u4f7f\u7528 mapper \u8f6c\u6362\u5b50\u72b6\u6001\u5230\u7236\u72b6\u6001\n return outputMapper.apply(parentState, subResult);\n }\n}"}),"\n",(0,r.jsx)(e.h2,{id:"\u5e94\u7528\u573a\u666f",children:"\u5e94\u7528\u573a\u666f"}),"\n",(0,r.jsxs)(e.ol,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u6570\u636e\u5904\u7406\u7ba1\u9053"}),": \u5c06\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u3001\u9a8c\u8bc1\u7b49\u6b65\u9aa4\u5c01\u88c5\u4e3a\u72ec\u7acb\u5b50\u56fe"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u5fae\u670d\u52a1\u7f16\u6392"}),": \u6bcf\u4e2a\u5fae\u670d\u52a1\u8c03\u7528\u5c01\u88c5\u4e3a\u5b50\u56fe"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u6761\u4ef6\u5904\u7406"}),": \u6839\u636e\u4e0d\u540c\u6761\u4ef6\u9009\u62e9\u4e0d\u540c\u7684\u5b50\u56fe\u6267\u884c"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u5e76\u884c\u5904\u7406"}),": \u591a\u4e2a\u5b50\u56fe\u5e76\u884c\u6267\u884c\u4e0d\u540c\u4efb\u52a1"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"\u7248\u672c\u63a7\u5236"}),": \u4e0d\u540c\u7248\u672c\u7684\u5904\u7406\u903b\u8f91\u5c01\u88c5\u4e3a\u4e0d\u540c\u5b50\u56fe"]}),"\n"]}),"\n",(0,r.jsx)(e.h2,{id:"\u9644\u56fe",children:"\u9644\u56fe"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.img,{alt:"png",src:n(36193).A+"",width:"125",height:"693"})}),"\n",(0,r.jsx)(e.h2,{id:"\u76f8\u5173\u6587\u6863",children:"\u76f8\u5173\u6587\u6863"}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.a,{href:"./subgraph-as-stategraph",children:"\u5b50\u56fe\u4f5c\u4e3a StateGraph"}
1)," - \u53e6\u4e00\u79cd\u5b50\u56fe\u96c6\u6210\u65b9\u5f0f"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.a,{href:"./subgraph-as-compiledgraph",children:"\u5b50\u56fe\u4f5c\u4e3a CompiledGraph"})," - \u7f16\u8bd1\u540e\u7684\u5b50\u56fe\u4f7f\u7528"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.a,{href:"../quick-start",children:"\u5feb\u901f\u5165\u95e8"})," - Graph \u57fa\u7840\u4f7f\u7528"]}),"\n"]})]})}function u(a={}){const{wrapper:e}={...(0,i.R)(),...a.components};return e?(0,r.jsx)(e,{...a,children:(0,r.jsx)(c,{...a})}):c(a)}},36193:(a,e,n)=>{n.d(e,{A:()=>t});const t=n.p+"assets/images/subgraph-as-nodeaction_15_0-f81f98566aa3b6806d961197176bbd24.png"}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.