kailash 0.1.1__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.
- kailash/api/__init__.py +7 -0
- kailash/api/workflow_api.py +383 -0
- kailash/nodes/__init__.py +2 -1
- kailash/nodes/ai/__init__.py +26 -0
- kailash/nodes/ai/ai_providers.py +1272 -0
- kailash/nodes/ai/embedding_generator.py +853 -0
- kailash/nodes/ai/llm_agent.py +1166 -0
- kailash/nodes/api/auth.py +3 -3
- kailash/nodes/api/graphql.py +2 -2
- kailash/nodes/api/http.py +391 -48
- kailash/nodes/api/rate_limiting.py +2 -2
- kailash/nodes/api/rest.py +465 -57
- kailash/nodes/base.py +71 -12
- kailash/nodes/code/python.py +2 -1
- kailash/nodes/data/__init__.py +7 -0
- kailash/nodes/data/readers.py +28 -26
- kailash/nodes/data/retrieval.py +178 -0
- kailash/nodes/data/sharepoint_graph.py +7 -7
- kailash/nodes/data/sources.py +65 -0
- kailash/nodes/data/sql.py +7 -5
- kailash/nodes/data/vector_db.py +2 -2
- kailash/nodes/data/writers.py +6 -3
- kailash/nodes/logic/__init__.py +2 -1
- kailash/nodes/logic/operations.py +2 -1
- kailash/nodes/logic/workflow.py +439 -0
- kailash/nodes/mcp/__init__.py +11 -0
- kailash/nodes/mcp/client.py +558 -0
- kailash/nodes/mcp/resource.py +682 -0
- kailash/nodes/mcp/server.py +577 -0
- kailash/nodes/transform/__init__.py +16 -1
- kailash/nodes/transform/chunkers.py +78 -0
- kailash/nodes/transform/formatters.py +96 -0
- kailash/nodes/transform/processors.py +5 -3
- kailash/runtime/docker.py +8 -6
- kailash/sdk_exceptions.py +24 -10
- kailash/tracking/metrics_collector.py +2 -1
- kailash/tracking/models.py +0 -20
- kailash/tracking/storage/database.py +4 -4
- kailash/tracking/storage/filesystem.py +0 -1
- kailash/utils/templates.py +6 -6
- kailash/visualization/performance.py +7 -7
- kailash/visualization/reports.py +1 -1
- kailash/workflow/graph.py +4 -4
- kailash/workflow/mock_registry.py +1 -1
- {kailash-0.1.1.dist-info → kailash-0.1.3.dist-info}/METADATA +441 -47
- kailash-0.1.3.dist-info/RECORD +83 -0
- kailash-0.1.1.dist-info/RECORD +0 -69
- {kailash-0.1.1.dist-info → kailash-0.1.3.dist-info}/WHEEL +0 -0
- {kailash-0.1.1.dist-info → kailash-0.1.3.dist-info}/entry_points.txt +0 -0
- {kailash-0.1.1.dist-info → kailash-0.1.3.dist-info}/licenses/LICENSE +0 -0
- {kailash-0.1.1.dist-info → kailash-0.1.3.dist-info}/top_level.txt +0 -0
kailash/api/__init__.py
ADDED
@@ -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/__init__.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
"""Node system for the Kailash SDK."""
|
2
2
|
|
3
3
|
# Import all node modules to ensure registration
|
4
|
-
from kailash.nodes import ai, api, code, data, logic, transform
|
4
|
+
from kailash.nodes import ai, api, code, data, logic, mcp, transform
|
5
5
|
from kailash.nodes.base import Node, NodeParameter, NodeRegistry, register_node
|
6
6
|
from kailash.nodes.base_async import AsyncNode
|
7
7
|
from kailash.nodes.code import PythonCodeNode
|
@@ -19,5 +19,6 @@ __all__ = [
|
|
19
19
|
"code",
|
20
20
|
"data",
|
21
21
|
"logic",
|
22
|
+
"mcp",
|
22
23
|
"transform",
|
23
24
|
]
|
kailash/nodes/ai/__init__.py
CHANGED
@@ -1,6 +1,20 @@
|
|
1
1
|
"""AI and ML nodes for the Kailash SDK."""
|
2
2
|
|
3
3
|
from .agents import ChatAgent, FunctionCallingAgent, PlanningAgent, RetrievalAgent
|
4
|
+
|
5
|
+
# Import from unified ai_providers module
|
6
|
+
from .ai_providers import (
|
7
|
+
PROVIDERS,
|
8
|
+
AnthropicProvider,
|
9
|
+
LLMProvider,
|
10
|
+
MockProvider,
|
11
|
+
OllamaProvider,
|
12
|
+
OpenAIProvider,
|
13
|
+
get_available_providers,
|
14
|
+
get_provider,
|
15
|
+
)
|
16
|
+
from .embedding_generator import EmbeddingGenerator
|
17
|
+
from .llm_agent import LLMAgent
|
4
18
|
from .models import (
|
5
19
|
ModelPredictor,
|
6
20
|
NamedEntityRecognizer,
|
@@ -16,6 +30,18 @@ __all__ = [
|
|
16
30
|
"RetrievalAgent",
|
17
31
|
"FunctionCallingAgent",
|
18
32
|
"PlanningAgent",
|
33
|
+
"LLMAgent",
|
34
|
+
# Embedding and Vector Operations
|
35
|
+
"EmbeddingGenerator",
|
36
|
+
# Provider Infrastructure
|
37
|
+
"LLMProvider",
|
38
|
+
"OllamaProvider",
|
39
|
+
"OpenAIProvider",
|
40
|
+
"AnthropicProvider",
|
41
|
+
"MockProvider",
|
42
|
+
"get_provider",
|
43
|
+
"get_available_providers",
|
44
|
+
"PROVIDERS",
|
19
45
|
# Models
|
20
46
|
"TextClassifier",
|
21
47
|
"TextEmbedder",
|