{ "kind": "open-flow-workflow", "version": 1, "name": "Repair Structured State Data with AI", "content": { "modelVersion": 14, "document": { "bindings": {}, "graph": { "nodes": { "n_afe7796060a8ec73": { "name": "Run the example", "kind": "start", "inputDefs": [] }, "n_59bedbb766a42977": { "inputs": {}, "name": "Ask for state data", "kind": "task", "task": { "inputs": [ { "jsonSchema": { "items": { "type": "object" }, "type": "array" }, "nullable": true, "handle": "messages", "value": null }, { "jsonSchema": { "items": { "type": "object" }, "minItems": 1, "type": "array" }, "nullable": false, "handle": "template", "value": [ { "role": "user", "content": "Return the 5 largest states by area in the USA with their 3 largest cities and their population.\nReturn JSON only, as {\"states\":[{\"state\":\"name\",\"cities\":[{\"name\":\"city\",\"population\":123}]}]}. Include exactly five states and exactly three cities per state; population must be a number." } ] }, { "jsonSchema": { "type": "object" }, "nullable": false, "handle": "model", "value": { "model": "gpt-4o-mini-2024-07-18", "temperature": 0 } } ], "outputs": [ { "description": "Generated response.", "jsonSchema": { "type": "string" }, "nullable": false, "handle": "output" } ], "name": "Ask for state data", "executor": { "kind": "llm", "mode": "chat" } } }, "n_aa4a276c10735bb8": { "inputs": { "text": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_59bedbb766a42977", "output": "output" } ] } }, "name": "Check the answer", "kind": "task", "task": { "inputs": [ { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "text" } ], "outputs": [ { "jsonSchema": { "type": "boolean" }, "nullable": false, "handle": "valid" }, { "jsonSchema": { "type": "object", "additionalProperties": true }, "nullable": false, "handle": "result" }, { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "error" }, { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "completion" } ], "name": "Check the answer", "moduleId": "n_e5678019b1109243", "capabilities": [ { "kind": "connector", "mode": "independent", "actions": [] } ] } }, "n_b1225dca3afa903b": { "inputs": {}, "name": "Answer matches format", "kind": "condition", "matchMode": "first", "cases": [ { "output": "valid", "groups": [ { "expressions": [ { "left": { "kind": "source", "source": { "kind": "node", "nodeId": "n_aa4a276c10735bb8", "output": "valid" } }, "operator": "isTrue" } ] } ] }, { "output": "invalid", "groups": [ { "expressions": [ { "left": { "kind": "source", "source": { "kind": "node", "nodeId": "n_aa4a276c10735bb8", "output": "valid" } }, "operator": "isFalse" } ] } ] } ] }, "n_a6c5f038a9c03199": { "inputs": { "result": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_aa4a276c10735bb8", "output": "result" } ] } }, "name": "Keep valid answer", "kind": "task", "task": { "inputs": [ { "jsonSchema": { "type": "object", "additionalProperties": true }, "nullable": false, "handle": "result" } ], "outputs": [ { "jsonSchema": { "type": "object", "additionalProperties": true }, "nullable": false, "handle": "result" } ], "name": "Keep valid answer", "moduleId": "n_a11dd88c5cc8f973", "capabilities": [ { "kind": "connector", "mode": "independent", "actions": [] } ] } }, "n_c05ab32e20d27867": { "inputs": { "completion": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_aa4a276c10735bb8", "output": "completion" } ] }, "error": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_aa4a276c10735bb8", "output": "error" } ] } }, "name": "Repair the answer", "kind": "task", "task": { "inputs": [ { "jsonSchema": { "items": { "type": "object" }, "type": "array" }, "nullable": true, "handle": "messages", "value": null }, { "jsonSchema": { "items": { "type": "object" }, "minItems": 1, "type": "array" }, "nullable": false, "handle": "template", "value": [ { "role": "user", "content": "Instructions:\nReturn JSON only, as {\"states\":[{\"state\":\"name\",\"cities\":[{\"name\":\"city\",\"population\":123}]}]}. Include exactly five states and exactly three cities per state; population must be a number.\nCompletion:\n{{completion}}\nError:\n{{error}}\nPlease return only a corrected answer." } ] }, { "jsonSchema": { "type": "object" }, "nullable": false, "handle": "model", "value": { "model": "gpt-4o-mini-2024-07-18", "temperature": 0 } }, { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "completion" }, { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "error" } ], "outputs": [ { "description": "Generated response.", "jsonSchema": { "type": "string" }, "nullable": false, "handle": "output" } ], "name": "Repair the answer", "executor": { "kind": "llm", "mode": "chat" } } }, "n_51b1dbe8a9301438": { "inputs": { "text": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_c05ab32e20d27867", "output": "output" } ] } }, "name": "Validate repaired answer", "kind": "task", "task": { "inputs": [ { "jsonSchema": { "type": "string" }, "nullable": false, "handle": "text" } ], "outputs": [ { "jsonSchema": { "type": "object", "additionalProperties": true }, "nullable": false, "handle": "result" } ], "name": "Validate repaired answer", "moduleId": "n_739b42f33a5ef2e6", "capabilities": [ { "kind": "connector", "mode": "independent", "actions": [] } ] } }, "n_1ed5b2ea870e601a": { "name": "Return repaired answer", "kind": "outputs", "outputDefs": [ { "jsonSchema": { "type": "object", "additionalProperties": true }, "nullable": false, "handle": "result" } ], "outputMappings": { "result": { "kind": "sources", "sources": [ { "kind": "node", "nodeId": "n_51b1dbe8a9301438", "output": "result" }, { "kind": "node", "nodeId": "n_a6c5f038a9c03199", "output": "result" } ] } } } }, "edges": [ { "source": "n_afe7796060a8ec73", "target": "n_59bedbb766a42977" }, { "source": "n_59bedbb766a42977", "target": "n_aa4a276c10735bb8" }, { "source": "n_aa4a276c10735bb8", "target": "n_b1225dca3afa903b" }, { "source": "n_b1225dca3afa903b", "target": "n_a6c5f038a9c03199", "sourceHandle": "valid" }, { "source": "n_a6c5f038a9c03199", "target": "n_1ed5b2ea870e601a" }, { "source": "n_b1225dca3afa903b", "target": "n_c05ab32e20d27867", "sourceHandle": "invalid" }, { "source": "n_c05ab32e20d27867", "target": "n_51b1dbe8a9301438" }, { "source": "n_51b1dbe8a9301438", "target": "n_1ed5b2ea870e601a" } ] } }, "modules": { "n_e5678019b1109243": { "name": "Check the answer", "imports": [], "source": "export default function parse({text}) {\n let result, error = '';\n try {\n result = JSON.parse(text);\n if (!result || !Array.isArray(result.states) || result.states.length !== 5 || !result.states.every(s => s && typeof s.state === 'string' && Array.isArray(s.cities) && s.cities.length === 3 && s.cities.every(c => c && typeof c.name === 'string' && typeof c.population === 'number' && Number.isFinite(c.population)))) throw new Error('Expected five states with three cities, each with a name and numeric population.');\n } catch (e) { error = String(e.message); result = {states: []}; }\n return {valid: !error, result, error, completion: text};\n}\n" }, "n_a11dd88c5cc8f973": { "name": "Keep valid answer", "imports": [], "source": "export default function pass({result}) { return {result}; }\n" }, "n_739b42f33a5ef2e6": { "name": "Validate repaired answer", "imports": [], "source": "export default function confirm({text}) {\n const result = JSON.parse(text);\n if (!result || !Array.isArray(result.states) || result.states.length !== 5 || !result.states.every(s => s && typeof s.state === 'string' && Array.isArray(s.cities) && s.cities.length === 3 && s.cities.every(c => c && typeof c.name === 'string' && typeof c.population === 'number' && Number.isFinite(c.population)))) throw new Error('The repaired answer does not match the required format.');\n return {result};\n}\n" } } }, "presentation": { "designer": { "version": 1, "flow": { "direction": "vertical" } } } }