|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Parse List of Docs Graph Template |
| 4 | +
|
| 5 | +This file defines the graph template for the simplified document processing workflow. |
| 6 | +It processes a list of documents from S3: list files → parse each PDF → upload each to S3. |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | +import aiohttp |
| 11 | +import os |
| 12 | + |
| 13 | +# Graph Template Definition |
| 14 | +PARSE_LIST_OF_DOCS_GRAPH_TEMPLATE = { |
| 15 | + "secrets": { |
| 16 | + "openai_api_key": "testkey", |
| 17 | + "aws_access_key_id": "testkey", |
| 18 | + "aws_secret_access_key": "testkey", |
| 19 | + "aws_region": "us-east-1" |
| 20 | + }, |
| 21 | + "nodes": [ |
| 22 | + { |
| 23 | + "node_name": "ListS3FilesNode", |
| 24 | + "namespace": "parse-list-of-docs", |
| 25 | + "identifier": "list_files", |
| 26 | + "inputs": { |
| 27 | + "bucket_name": "initial", |
| 28 | + "prefix": "initial", |
| 29 | + "files_only": "true", |
| 30 | + "recursive": "false" |
| 31 | + }, |
| 32 | + "next_nodes": ["parse_pdf"] |
| 33 | + }, |
| 34 | + { |
| 35 | + "node_name": "ParseSinglePDFNode", |
| 36 | + "namespace": "parse-list-of-docs", |
| 37 | + "identifier": "parse_pdf", |
| 38 | + "inputs": { |
| 39 | + "bucket_name": "initial", |
| 40 | + "key": "${{ list_files.outputs.key }}" |
| 41 | + }, |
| 42 | + "next_nodes": ["upload_to_s3"] |
| 43 | + }, |
| 44 | + |
| 45 | + { |
| 46 | + "node_name": "UploadToS3Node", |
| 47 | + "namespace": "parse-list-of-docs", |
| 48 | + "identifier": "upload_to_s3", |
| 49 | + "inputs": { |
| 50 | + "extracted_content": "${{ parse_pdf.outputs.extracted_content }}", |
| 51 | + "key": "${{ parse_pdf.outputs.key }}", |
| 52 | + "output_bucket": "processed-docs-bucket", |
| 53 | + "output_prefix": "processed-documents/" |
| 54 | + }, |
| 55 | + "next_nodes": [] |
| 56 | + } |
| 57 | + ] |
| 58 | +} |
| 59 | + |
| 60 | +async def create_graph_template(graph_name: str): |
| 61 | + """Create a graph template with simplified document processing nodes""" |
| 62 | + namespace = "parse-list-of-docs" |
| 63 | + api_key = os.getenv("STATE_MANAGER_API_KEY") # TODO: Replace with your actual API key |
| 64 | + |
| 65 | + graph_request = { |
| 66 | + "secrets": { |
| 67 | + "openai_api_key": "testkey", |
| 68 | + "aws_access_key_id": "testkey", |
| 69 | + "aws_secret_access_key": "testkey", |
| 70 | + "aws_region": "us-east-1" |
| 71 | + }, |
| 72 | + "nodes": PARSE_LIST_OF_DOCS_GRAPH_TEMPLATE["nodes"] |
| 73 | + } |
| 74 | + |
| 75 | + async with aiohttp.ClientSession() as session: |
| 76 | + # Create the graph template |
| 77 | + url = f"http://localhost:8000/v0/namespace/{namespace}/graph/{graph_name}" |
| 78 | + headers = {"X-API-Key": api_key, "Content-Type": "application/json"} |
| 79 | + |
| 80 | + try: |
| 81 | + async with session.put(url, json=graph_request, headers=headers) as response: |
| 82 | + if response.status == 201: |
| 83 | + print(f"Parse list of docs graph template created successfully: {graph_name}") |
| 84 | + return graph_name |
| 85 | + else: |
| 86 | + print(f"Failed to create graph template: {response.status}") |
| 87 | + print(await response.text()) |
| 88 | + return None |
| 89 | + except Exception as e: |
| 90 | + print(f"Error creating graph template: {e}") |
| 91 | + return None |
| 92 | + |
| 93 | +async def trigger_graph_execution(graph_name: str): |
| 94 | + """Trigger the first node using the new trigger graph API endpoint""" |
| 95 | + namespace = "parse-list-of-docs" |
| 96 | + api_key = "niki" # TODO: Replace with your actual API key |
| 97 | + |
| 98 | + # Trigger graph with the first node |
| 99 | + trigger_request = { |
| 100 | + "states": [ |
| 101 | + { |
| 102 | + "identifier": "list_files", |
| 103 | + "inputs": { |
| 104 | + "bucket_name": "my-documents-bucket", |
| 105 | + "prefix": "pdfs/" |
| 106 | + } |
| 107 | + } |
| 108 | + ] |
| 109 | + } |
| 110 | + |
| 111 | + async with aiohttp.ClientSession() as session: |
| 112 | + url = f"http://localhost:8000/v0/namespace/{namespace}/graph/{graph_name}/trigger" |
| 113 | + headers = {"X-API-Key": api_key, "Content-Type": "application/json"} |
| 114 | + |
| 115 | + try: |
| 116 | + async with session.post(url, json=trigger_request, headers=headers) as response: |
| 117 | + if response.status == 200: |
| 118 | + response_data = await response.json() |
| 119 | + print("✅ Parse list of docs graph triggered successfully!") |
| 120 | + print(f" - Graph: {graph_name}") |
| 121 | + print(f" - Run ID: {response_data['run_id']}") |
| 122 | + print(f" - State ID: {response_data['states'][0]['state_id']}") |
| 123 | + print(f" - Node: {response_data['states'][0]['node_name']}") |
| 124 | + print(f" - Status: {response_data['status']}") |
| 125 | + return response_data['states'][0]['state_id'], response_data['run_id'] |
| 126 | + else: |
| 127 | + print(f"❌ Failed to trigger graph: {response.status}") |
| 128 | + print(await response.text()) |
| 129 | + return None, None |
| 130 | + except Exception as e: |
| 131 | + print(f"❌ Error triggering graph: {e}") |
| 132 | + return None, None |
| 133 | + |
| 134 | + |
| 135 | +async def main(): |
| 136 | + """Main function to create graph and trigger workflow""" |
| 137 | + print("🚀 Starting Parse List of Docs workflow setup...") |
| 138 | + |
| 139 | + # Step 1: Create the graph template |
| 140 | + print("\n📋 Step 1: Creating parse list of docs graph template...") |
| 141 | + graph_name = "parse-list-of-docs-workflow" |
| 142 | + # await create_graph_template(graph_name) |
| 143 | + |
| 144 | + # Step 2: Trigger the graph execution |
| 145 | + print("\n🎯 Step 2: Triggering parse list of docs workflow...") |
| 146 | + state_id, run_id = await trigger_graph_execution(graph_name) |
| 147 | + |
| 148 | + if state_id and run_id: |
| 149 | + print("\n✅ Parse list of docs workflow initiated successfully!") |
| 150 | + print(" The workflow will now execute:") |
| 151 | + print(" 1. ListS3FilesNode (list_files) - List PDF files from S3") |
| 152 | + print(" 2. ParseSinglePDFNode (parse_pdf) - Parse each PDF file") |
| 153 | + print(" 3. UploadToS3Node (upload_to_s3) - Upload processed document to S3") |
| 154 | + print(f"\n State ID: {state_id}") |
| 155 | + print(f" Run ID: {run_id}") |
| 156 | + print(f" Graph: {graph_name}") |
| 157 | + else: |
| 158 | + print("❌ Failed to trigger parse list of docs workflow.") |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + # Run the async main function |
| 162 | + asyncio.run(main()) |
0 commit comments