-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_graph.py
More file actions
134 lines (122 loc) · 4.44 KB
/
create_graph.py
File metadata and controls
134 lines (122 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
"""
Sync Document Processing Graph Template
This file defines the graph template for the sync document processing workflow.
It takes a CSV file with document paths, processes them one at a time using real-time API, and stores extracted information in a database.
"""
import asyncio
import os
from exospherehost import StateManager, GraphNodeModel, RetryPolicyModel, StoreConfigModel, RetryStrategyEnum
from dotenv import load_dotenv
load_dotenv()
# Environment variables
EXOSPHERE_STATE_MANAGER_URI = os.getenv("EXOSPHERE_STATE_MANAGER_URI", "http://localhost:8000")
EXOSPHERE_API_KEY = os.getenv("EXOSPHERE_API_KEY", "exosphere@123") # TODO: Replace with your actual API key
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "{{GEMINI_API_KEY}}")
DATABASE_URL = os.getenv("MONGODB_CONNECTION_STRING", "{{DATABASE_URL}}")
DATABASE_NAME = os.getenv("DATABASE_NAME", "sync_processed_docs")
async def create_graph():
"""Create a graph with sync document processing nodes using Exosphere Python SDK"""
state_manager = StateManager(
namespace="sync-process-docs",
state_manager_uri=EXOSPHERE_STATE_MANAGER_URI,
key=EXOSPHERE_API_KEY
)
graph_nodes = [
GraphNodeModel(
node_name="CSVInputNode",
namespace="sync-process-docs",
identifier="csv_input",
inputs={
"csv_file_path": "${{ store.csv_file_path }}"
},
next_nodes=["file_distribution"]
),
GraphNodeModel(
node_name="FileDistributionNode",
namespace="sync-process-docs",
identifier="file_distribution",
inputs={
"file_paths": "${{ csv_input.outputs.file_paths }}"
},
next_nodes=["sync_processing"]
),
GraphNodeModel(
node_name="SyncProcessingNode",
namespace="sync-process-docs",
identifier="sync_processing",
inputs={
"file_path": "${{ file_distribution.outputs.file_path }}",
"prompt": "${{ store.prompt }}"
},
next_nodes=["validation"]
),
GraphNodeModel(
node_name="ValidationNode",
namespace="sync-process-docs",
identifier="validation",
inputs={
"file_info": "${{ sync_processing.outputs.file_info }}"
},
next_nodes=["database_write", "failure_handling"]
),
GraphNodeModel(
node_name="DatabaseWriteNode",
namespace="sync-process-docs",
identifier="database_write",
inputs={
"validated_result": "${{ validation.outputs.validated_result }}",
"file_info": "${{ validation.outputs.file_info }}"
},
next_nodes=[]
),
GraphNodeModel(
node_name="FailureHandlingNode",
namespace="sync-process-docs",
identifier="failure_handling",
inputs={
"validated_result": "${{ validation.outputs.validated_result }}",
"file_info": "${{ validation.outputs.file_info }}"
},
next_nodes=[]
)
]
retry_policy = RetryPolicyModel(
max_retries=3,
strategy=RetryStrategyEnum.EXPONENTIAL,
backoff_factor=2000,
exponent=2
)
store_config = StoreConfigModel(
required_keys=["csv_file_path", "prompt"],
default_values={
"csv_file_path": "",
"prompt": "Extract key information from this document"
}
)
result = await state_manager.upsert_graph(
graph_name="parse-and-process-insync-docs",
graph_nodes=graph_nodes,
secrets={
"gemini_api_key": GEMINI_API_KEY,
"mongodb_connection_string": DATABASE_URL,
"database_name": DATABASE_NAME
},
retry_policy=retry_policy,
store_config=store_config
)
return result
async def main():
"""Main function to create the sync document processing graph"""
try:
result = await create_graph()
if result:
print("Sync document processing graph created successfully!")
print(f"Graph result: {result}")
else:
print("Failed to create graph")
except Exception as e:
print(f"Error creating graph: {e}")
if __name__ == "__main__":
# Run the async main function
asyncio.run(main())