kailash 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ """
2
+ Kailash API module for exposing workflows as REST APIs.
3
+ """
4
+
5
+ from .workflow_api import HierarchicalRAGAPI, WorkflowAPI, create_workflow_api
6
+
7
+ __all__ = ["WorkflowAPI", "HierarchicalRAGAPI", "create_workflow_api"]
@@ -0,0 +1,383 @@
1
+ """
2
+ Lean API wrapper for Kailash workflows using FastAPI.
3
+
4
+ This module provides a general-purpose API wrapper that can expose any Kailash
5
+ workflow as a REST API with minimal configuration.
6
+ """
7
+
8
+ import asyncio
9
+ from contextlib import asynccontextmanager
10
+ from enum import Enum
11
+ from typing import Any, Dict, List, Optional, Union
12
+
13
+ import uvicorn
14
+ from fastapi import BackgroundTasks, FastAPI, HTTPException
15
+ from fastapi.responses import StreamingResponse
16
+ from pydantic import BaseModel, Field
17
+
18
+ from kailash.runtime.local import LocalRuntime
19
+ from kailash.workflow.builder import WorkflowBuilder
20
+ from kailash.workflow.graph import Workflow
21
+
22
+
23
+ class ExecutionMode(str, Enum):
24
+ """Execution modes for workflow API."""
25
+
26
+ SYNC = "sync"
27
+ ASYNC = "async"
28
+ STREAM = "stream"
29
+
30
+
31
+ class WorkflowRequest(BaseModel):
32
+ """Base request model for workflow execution."""
33
+
34
+ inputs: Dict[str, Any] = Field(..., description="Input data for workflow nodes")
35
+ config: Optional[Dict[str, Any]] = Field(
36
+ None, description="Node configuration overrides"
37
+ )
38
+ mode: ExecutionMode = Field(ExecutionMode.SYNC, description="Execution mode")
39
+
40
+
41
+ class WorkflowResponse(BaseModel):
42
+ """Base response model for workflow execution."""
43
+
44
+ outputs: Dict[str, Any] = Field(..., description="Output data from workflow nodes")
45
+ execution_time: float = Field(..., description="Execution time in seconds")
46
+ workflow_id: str = Field(..., description="Workflow identifier")
47
+ version: str = Field(..., description="Workflow version")
48
+
49
+
50
+ class WorkflowAPI:
51
+ """
52
+ Lean API wrapper for Kailash workflows.
53
+
54
+ This class provides a minimal, efficient way to expose any Kailash workflow
55
+ as a REST API with support for synchronous, asynchronous, and streaming execution.
56
+
57
+ Example:
58
+ >>> # For any workflow
59
+ >>> from my_workflows import rag_workflow
60
+ >>> api = WorkflowAPI(rag_workflow)
61
+ >>> api.run(port=8000)
62
+ """
63
+
64
+ def __init__(
65
+ self,
66
+ workflow: Union[WorkflowBuilder, Workflow],
67
+ app_name: str = "Kailash Workflow API",
68
+ version: str = "1.0.0",
69
+ description: str = "API wrapper for Kailash workflow execution",
70
+ ):
71
+ """
72
+ Initialize the API wrapper.
73
+
74
+ Args:
75
+ workflow: The WorkflowBuilder or Workflow instance to expose
76
+ app_name: Name of the API application
77
+ version: API version
78
+ description: API description
79
+ """
80
+ if isinstance(workflow, WorkflowBuilder):
81
+ self.workflow = workflow
82
+ self.workflow_graph = workflow.build()
83
+ self.workflow_id = getattr(workflow, "workflow_id", "unnamed")
84
+ self.version = getattr(workflow, "version", "1.0.0")
85
+ else: # Workflow instance
86
+ self.workflow = workflow
87
+ self.workflow_graph = workflow
88
+ self.workflow_id = workflow.workflow_id
89
+ self.version = workflow.version
90
+
91
+ self.runtime = LocalRuntime()
92
+
93
+ # Create FastAPI app with lifespan management
94
+ self.app = FastAPI(
95
+ title=app_name,
96
+ version=version,
97
+ description=description,
98
+ lifespan=self._lifespan,
99
+ )
100
+
101
+ # Setup routes
102
+ self._setup_routes()
103
+
104
+ # Cache for async executions
105
+ self._execution_cache: Dict[str, Dict[str, Any]] = {}
106
+
107
+ @asynccontextmanager
108
+ async def _lifespan(self, app: FastAPI):
109
+ """Manage app lifecycle."""
110
+ # Startup
111
+ yield
112
+ # Shutdown - cleanup cache
113
+ self._execution_cache.clear()
114
+
115
+ def _setup_routes(self):
116
+ """Setup API routes dynamically based on workflow."""
117
+
118
+ # Main execution endpoint
119
+ @self.app.post("/execute", response_model=WorkflowResponse)
120
+ async def execute_workflow(
121
+ request: WorkflowRequest, background_tasks: BackgroundTasks
122
+ ):
123
+ """Execute the workflow with provided inputs."""
124
+
125
+ if request.mode == ExecutionMode.SYNC:
126
+ return await self._execute_sync(request)
127
+ elif request.mode == ExecutionMode.ASYNC:
128
+ return await self._execute_async(request, background_tasks)
129
+ else: # STREAM
130
+ return StreamingResponse(
131
+ self._execute_stream(request), media_type="application/json"
132
+ )
133
+
134
+ # Status endpoint for async executions
135
+ @self.app.get("/status/{execution_id}")
136
+ async def get_execution_status(execution_id: str):
137
+ """Get status of async execution."""
138
+ if execution_id not in self._execution_cache:
139
+ raise HTTPException(status_code=404, detail="Execution not found")
140
+ return self._execution_cache[execution_id]
141
+
142
+ # Workflow metadata endpoint
143
+ @self.app.get("/workflow/info")
144
+ async def get_workflow_info():
145
+ """Get workflow metadata and structure."""
146
+ graph_data = self.workflow_graph
147
+ return {
148
+ "id": self.workflow_id,
149
+ "version": self.version,
150
+ "nodes": list(graph_data.nodes()),
151
+ "edges": list(graph_data.edges()),
152
+ "input_nodes": [
153
+ n for n in graph_data.nodes() if graph_data.in_degree(n) == 0
154
+ ],
155
+ "output_nodes": [
156
+ n for n in graph_data.nodes() if graph_data.out_degree(n) == 0
157
+ ],
158
+ }
159
+
160
+ # Health check
161
+ @self.app.get("/health")
162
+ async def health_check():
163
+ """Check API health."""
164
+ return {"status": "healthy", "workflow": self.workflow_id}
165
+
166
+ async def _execute_sync(self, request: WorkflowRequest) -> WorkflowResponse:
167
+ """Execute workflow synchronously."""
168
+ import time
169
+
170
+ start_time = time.time()
171
+
172
+ try:
173
+ # Apply configuration overrides if provided
174
+ if request.config:
175
+ for node_id, config in request.config.items():
176
+ # This would need workflow builder enhancement to support
177
+ # dynamic config updates
178
+ pass
179
+
180
+ # Execute workflow with inputs
181
+ results = await asyncio.to_thread(
182
+ self.runtime.execute, self.workflow_graph, request.inputs
183
+ )
184
+
185
+ # Handle tuple return from runtime
186
+ if isinstance(results, tuple):
187
+ results = results[0] if results else {}
188
+
189
+ execution_time = time.time() - start_time
190
+
191
+ return WorkflowResponse(
192
+ outputs=results,
193
+ execution_time=execution_time,
194
+ workflow_id=self.workflow_id,
195
+ version=self.version,
196
+ )
197
+
198
+ except Exception as e:
199
+ raise HTTPException(status_code=500, detail=str(e))
200
+
201
+ async def _execute_async(
202
+ self, request: WorkflowRequest, background_tasks: BackgroundTasks
203
+ ):
204
+ """Execute workflow asynchronously."""
205
+ import uuid
206
+
207
+ execution_id = str(uuid.uuid4())
208
+
209
+ # Initialize cache entry
210
+ self._execution_cache[execution_id] = {
211
+ "status": "pending",
212
+ "workflow_id": self.workflow_id,
213
+ "version": self.version,
214
+ }
215
+
216
+ # Schedule background execution
217
+ background_tasks.add_task(self._run_async_execution, execution_id, request)
218
+
219
+ return {
220
+ "execution_id": execution_id,
221
+ "status": "pending",
222
+ "message": f"Execution started. Check status at /status/{execution_id}",
223
+ }
224
+
225
+ async def _run_async_execution(self, execution_id: str, request: WorkflowRequest):
226
+ """Run async execution in background."""
227
+ try:
228
+ self._execution_cache[execution_id]["status"] = "running"
229
+
230
+ result = await self._execute_sync(request)
231
+
232
+ self._execution_cache[execution_id].update(
233
+ {"status": "completed", "result": result.dict()}
234
+ )
235
+
236
+ except Exception as e:
237
+ self._execution_cache[execution_id].update(
238
+ {"status": "failed", "error": str(e)}
239
+ )
240
+
241
+ async def _execute_stream(self, request: WorkflowRequest):
242
+ """Execute workflow with streaming response."""
243
+ import json
244
+ import time
245
+
246
+ try:
247
+ # For streaming, we'd need workflow runner enhancement
248
+ # to support progress callbacks. For now, simulate with
249
+ # start/end events
250
+
251
+ yield json.dumps(
252
+ {
253
+ "event": "start",
254
+ "workflow_id": self.workflow_id,
255
+ "timestamp": time.time(),
256
+ }
257
+ ) + "\n"
258
+
259
+ result = await self._execute_sync(request)
260
+
261
+ yield json.dumps(
262
+ {"event": "complete", "result": result.dict(), "timestamp": time.time()}
263
+ ) + "\n"
264
+
265
+ except Exception as e:
266
+ yield json.dumps(
267
+ {"event": "error", "error": str(e), "timestamp": time.time()}
268
+ ) + "\n"
269
+
270
+ def run(self, host: str = "0.0.0.0", port: int = 8000, **kwargs):
271
+ """Run the API server."""
272
+ uvicorn.run(self.app, host=host, port=port, **kwargs)
273
+
274
+
275
+ # Specialized API wrapper for Hierarchical RAG workflows
276
+ class HierarchicalRAGAPI(WorkflowAPI):
277
+ """
278
+ Specialized API wrapper for Hierarchical RAG workflows.
279
+
280
+ Provides RAG-specific endpoints and models for better developer experience.
281
+ """
282
+
283
+ def __init__(self, workflow: WorkflowBuilder, **kwargs):
284
+ super().__init__(workflow, **kwargs)
285
+ self._setup_rag_routes()
286
+
287
+ def _setup_rag_routes(self):
288
+ """Setup RAG-specific routes."""
289
+
290
+ class Document(BaseModel):
291
+ id: str
292
+ title: str
293
+ content: str
294
+
295
+ class RAGQuery(BaseModel):
296
+ query: str
297
+ top_k: int = 3
298
+ similarity_method: str = "cosine"
299
+ temperature: float = 0.7
300
+ max_tokens: int = 500
301
+
302
+ class RAGResponse(BaseModel):
303
+ answer: str
304
+ sources: List[Dict[str, Any]]
305
+ query: str
306
+ execution_time: float
307
+
308
+ @self.app.post("/documents")
309
+ async def add_documents(documents: List[Document]):
310
+ """Add documents to the knowledge base."""
311
+ # This would integrate with document storage
312
+ return {"message": f"Added {len(documents)} documents"}
313
+
314
+ @self.app.post("/query", response_model=RAGResponse)
315
+ async def query_rag(request: RAGQuery):
316
+ """Query the RAG system."""
317
+ import time
318
+
319
+ start_time = time.time()
320
+
321
+ # Transform to workflow format
322
+ workflow_request = WorkflowRequest(
323
+ inputs={
324
+ "query": request.query,
325
+ "config": {
326
+ "relevance_scorer": {
327
+ "top_k": request.top_k,
328
+ "similarity_method": request.similarity_method,
329
+ },
330
+ "llm_agent": {
331
+ "temperature": request.temperature,
332
+ "max_tokens": request.max_tokens,
333
+ },
334
+ },
335
+ }
336
+ )
337
+
338
+ result = await self._execute_sync(workflow_request)
339
+
340
+ # Extract RAG-specific outputs
341
+ outputs = result.outputs
342
+ answer = (
343
+ outputs.get("llm_response", {})
344
+ .get("choices", [{}])[0]
345
+ .get("message", {})
346
+ .get("content", "")
347
+ )
348
+ sources = outputs.get("relevant_chunks", [])
349
+
350
+ return RAGResponse(
351
+ answer=answer,
352
+ sources=sources,
353
+ query=request.query,
354
+ execution_time=time.time() - start_time,
355
+ )
356
+
357
+
358
+ # Factory function for creating API wrappers
359
+ def create_workflow_api(
360
+ workflow: WorkflowBuilder, api_type: str = "generic", **kwargs
361
+ ) -> WorkflowAPI:
362
+ """
363
+ Factory function to create appropriate API wrapper.
364
+
365
+ Args:
366
+ workflow: The workflow to wrap
367
+ api_type: Type of API wrapper ("generic", "rag", etc.)
368
+ **kwargs: Additional arguments for API initialization
369
+
370
+ Returns:
371
+ Configured WorkflowAPI instance
372
+
373
+ Example:
374
+ >>> api = create_workflow_api(my_workflow, api_type="rag")
375
+ >>> api.run(port=8000)
376
+ """
377
+ api_classes = {
378
+ "generic": WorkflowAPI,
379
+ "rag": HierarchicalRAGAPI,
380
+ }
381
+
382
+ api_class = api_classes.get(api_type, WorkflowAPI)
383
+ return api_class(workflow, **kwargs)
kailash/nodes/api/http.py CHANGED
@@ -428,7 +428,6 @@ class HTTPRequestNode(Node):
428
428
  self.logger.info(f"Making {method} request to {url}")
429
429
 
430
430
  response = None
431
- last_error = None
432
431
 
433
432
  for attempt in range(retry_count + 1):
434
433
  if attempt > 0:
@@ -453,7 +452,6 @@ class HTTPRequestNode(Node):
453
452
  break
454
453
 
455
454
  except requests.RequestException as e:
456
- last_error = e
457
455
  self.logger.warning(f"Request failed: {str(e)}")
458
456
 
459
457
  # Last attempt, no more retries
@@ -779,7 +777,6 @@ class AsyncHTTPRequestNode(AsyncNode):
779
777
  self.logger.info(f"Making async {method} request to {url}")
780
778
 
781
779
  response = None
782
- last_error = None
783
780
 
784
781
  for attempt in range(retry_count + 1):
785
782
  if attempt > 0:
@@ -860,7 +857,6 @@ class AsyncHTTPRequestNode(AsyncNode):
860
857
  return result
861
858
 
862
859
  except (aiohttp.ClientError, asyncio.TimeoutError) as e:
863
- last_error = e
864
860
  self.logger.warning(f"Async request failed: {str(e)}")
865
861
 
866
862
  # Last attempt, no more retries
kailash/nodes/api/rest.py CHANGED
@@ -335,7 +335,7 @@ class RESTClientNode(Node):
335
335
 
336
336
  pagination_type = pagination_params.get("type", "page")
337
337
  items_path = pagination_params.get("items_path", "data")
338
- max_pages = pagination_params.get("max_pages", 10)
338
+ # max_pages = pagination_params.get("max_pages", 10) # TODO: Implement max pages limit
339
339
 
340
340
  # Extract items from initial response
341
341
  all_items = self._get_nested_value(initial_response, items_path, [])
kailash/nodes/data/sql.py CHANGED
@@ -192,10 +192,10 @@ class SQLDatabaseNode(Node):
192
192
  """
193
193
  connection_string = kwargs["connection_string"]
194
194
  query = kwargs["query"]
195
- parameters = kwargs.get("parameters", [])
195
+ # parameters = kwargs.get("parameters", []) # TODO: Implement parameterized queries
196
196
  result_format = kwargs.get("result_format", "dict")
197
- timeout = kwargs.get("timeout", 30)
198
- transaction_mode = kwargs.get("transaction_mode", "auto")
197
+ # timeout = kwargs.get("timeout", 30) # TODO: Implement query timeout
198
+ # transaction_mode = kwargs.get("transaction_mode", "auto") # TODO: Implement transaction handling
199
199
 
200
200
  # This is a placeholder implementation
201
201
  # In a real implementation, you would:
@@ -584,7 +584,7 @@ class VectorDatabaseNode(Node):
584
584
  """
585
585
  vectors = inputs.get("vectors", [])
586
586
  ids = inputs.get("ids", [])
587
- metadata = inputs.get("metadata", [])
587
+ # metadata = inputs.get("metadata", []) # TODO: Implement metadata storage
588
588
 
589
589
  if not vectors or not ids:
590
590
  raise ValueError("Vectors and IDs are required for upsert")
@@ -611,7 +611,7 @@ class VectorDatabaseNode(Node):
611
611
  """
612
612
  query_vector = inputs.get("query_vector")
613
613
  k = inputs.get("k", 10)
614
- filter_dict = inputs.get("filter", {})
614
+ # filter_dict = inputs.get("filter", {}) # TODO: Implement filter-based queries
615
615
 
616
616
  if not query_vector:
617
617
  raise ValueError("Query vector is required")
@@ -2,5 +2,6 @@
2
2
 
3
3
  from kailash.nodes.logic.async_operations import AsyncMerge, AsyncSwitch
4
4
  from kailash.nodes.logic.operations import Merge, Switch
5
+ from kailash.nodes.logic.workflow import WorkflowNode
5
6
 
6
- __all__ = ["Switch", "Merge", "AsyncSwitch", "AsyncMerge"]
7
+ __all__ = ["Switch", "Merge", "AsyncSwitch", "AsyncMerge", "WorkflowNode"]