(node_map: Dict[str, NodeData], llm)
| 150 | return "False" |
| 151 | |
| 152 | def build_subgraph(node_map: Dict[str, NodeData], llm) -> StateGraph: |
| 153 | # Define the state machine |
| 154 | subgraph = StateGraph(PipelineState) |
| 155 | |
| 156 | # Start node, only one start point |
| 157 | start_node = find_nodes_by_type(node_map, "START")[0] |
| 158 | logger(f"Start root ID: {start_node.uniq_id}") |
| 159 | |
| 160 | # Step nodes |
| 161 | step_nodes = find_nodes_by_type(node_map, "STEP") |
| 162 | for current_node in step_nodes: |
| 163 | if current_node.tool: |
| 164 | tool_info = tool_info_registry[current_node.tool] |
| 165 | prompt_template = f""" |
| 166 | history: {{history}} |
| 167 | {current_node.description} |
| 168 | Available tool: {tool_info} |
| 169 | Based on Available tool, arguments in the json format: |
| 170 | "function": "<func_name>", "args": [<arg1>, <arg2>, ...] |
| 171 | |
| 172 | next stage directly parse then run <func_name>(<arg1>,<arg2>, ...) make sure syntax is right json and align function siganture |
| 173 | """ |
| 174 | subgraph.add_node( |
| 175 | current_node.uniq_id, |
| 176 | lambda state, template=prompt_template, llm=llm, name=current_node.name : execute_tool(name, state, template, llm) |
| 177 | ) |
| 178 | else: |
| 179 | prompt_template=f""" |
| 180 | history: {{history}} |
| 181 | {current_node.description} |
| 182 | you reply in the json format |
| 183 | """ |
| 184 | subgraph.add_node( |
| 185 | current_node.uniq_id, |
| 186 | lambda state, template=prompt_template, llm=llm, name=current_node.name: execute_step(name, state, template, llm) |
| 187 | ) |
| 188 | |
| 189 | # Add INFO nodes |
| 190 | info_nodes = find_nodes_by_type(node_map, "INFO") |
| 191 | for info_node in info_nodes: |
| 192 | # INFO nodes just append predefined information to the state history |
| 193 | subgraph.add_node( |
| 194 | info_node.uniq_id, |
| 195 | lambda state, template=info_node.description, llm=llm, name=info_node.name: info_add(name, state, template, llm) |
| 196 | ) |
| 197 | |
| 198 | # Add SUBGRAPH nodes |
| 199 | subgraph_nodes = find_nodes_by_type(node_map, "SUBGRAPH") |
| 200 | for sg_node in subgraph_nodes: |
| 201 | subgraph.add_node( |
| 202 | sg_node.uniq_id, |
| 203 | lambda state, llm=llm, name=sg_node.name, sg_name=sg_node.name: sg_add(name, state, sg_name) |
| 204 | ) |
| 205 | |
| 206 | # Edges |
| 207 | # Find all next nodes from start_node |
| 208 | next_node_ids = start_node.nexts |
| 209 | next_nodes = [node_map[next_id] for next_id in next_node_ids] |
no test coverage detected