kailash 0.1.4__py3-none-any.whl → 0.1.5__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.
@@ -7,7 +7,64 @@ from kailash.nodes.base import Node, NodeParameter, register_node
7
7
 
8
8
  @register_node()
9
9
  class ChatAgent(Node):
10
- """Chat-based AI agent node."""
10
+ """
11
+ Chat-based AI agent node for conversational interactions.
12
+
13
+ This node provides a conversational AI interface that maintains context across
14
+ multiple message exchanges. It supports various LLM configurations and can be
15
+ customized with system prompts to create specialized conversational agents for
16
+ different domains and use cases.
17
+
18
+ Design Philosophy:
19
+ The ChatAgent embodies the principle of contextual conversation, maintaining
20
+ the full dialogue history to provide coherent and relevant responses. It
21
+ abstracts away the complexities of LLM APIs while providing a consistent
22
+ interface for chat-based interactions across different providers.
23
+
24
+ Upstream Dependencies:
25
+ - User interfaces or APIs providing conversation messages
26
+ - Context injection systems adding relevant information
27
+ - Authentication systems for user-specific interactions
28
+ - Workflow orchestrators managing conversation flow
29
+
30
+ Downstream Consumers:
31
+ - Response formatting nodes processing agent outputs
32
+ - Logging systems recording conversations
33
+ - Analytics nodes analyzing interaction patterns
34
+ - UI components displaying chat responses
35
+
36
+ Configuration:
37
+ The agent can be configured with different models, temperature settings,
38
+ and token limits. System prompts allow specialization for specific domains
39
+ or behaviors without code changes.
40
+
41
+ Implementation Details:
42
+ - Maintains conversation history with role-based messages
43
+ - Prepends system prompt to establish agent behavior
44
+ - Currently uses mock responses for testing (production would use LLM APIs)
45
+ - Supports streaming responses (when integrated with real LLMs)
46
+ - Implements token counting for cost management
47
+ - Thread-safe for concurrent conversations
48
+
49
+ Error Handling:
50
+ - Validates message format and required fields
51
+ - Handles empty or malformed conversations gracefully
52
+ - Returns appropriate responses for API failures
53
+ - Implements retry logic for transient errors
54
+
55
+ Side Effects:
56
+ - May log conversations for debugging (configurable)
57
+ - Updates internal conversation state
58
+ - May trigger external LLM API calls (in production)
59
+
60
+ Examples:
61
+ >>> # Test parameter structure without constructor validation
62
+ >>> agent = ChatAgent.__new__(ChatAgent)
63
+ >>> params = agent.get_parameters()
64
+ >>> assert "messages" in params
65
+ >>> assert "model" in params
66
+ >>> assert "temperature" in params
67
+ """
11
68
 
12
69
  def get_parameters(self) -> Dict[str, NodeParameter]:
13
70
  return {
@@ -97,7 +154,64 @@ class ChatAgent(Node):
97
154
 
98
155
  @register_node()
99
156
  class RetrievalAgent(Node):
100
- """Retrieval-augmented generation agent."""
157
+ """
158
+ Retrieval-augmented generation (RAG) agent for knowledge-based responses.
159
+
160
+ This node implements a RAG pipeline that retrieves relevant documents based on
161
+ a query and optionally generates answers using the retrieved context. It combines
162
+ information retrieval techniques with language generation to provide accurate,
163
+ grounded responses based on provided documents.
164
+
165
+ Design Philosophy:
166
+ The RetrievalAgent addresses the hallucination problem in LLMs by grounding
167
+ responses in retrieved documents. It implements a two-stage process: first
168
+ finding relevant information, then synthesizing an answer based only on that
169
+ information. This ensures factual accuracy and traceability.
170
+
171
+ Upstream Dependencies:
172
+ - Document ingestion nodes providing indexed content
173
+ - Query processing nodes enhancing user queries
174
+ - Embedding generation nodes (in production implementations)
175
+ - Vector databases or search indices
176
+
177
+ Downstream Consumers:
178
+ - Response formatting nodes presenting answers
179
+ - Citation generation nodes adding references
180
+ - Quality assessment nodes evaluating retrieval accuracy
181
+ - UI components displaying results with sources
182
+
183
+ Configuration:
184
+ The agent can be configured with retrieval parameters like top_k results
185
+ and similarity thresholds. Answer generation can be toggled based on use
186
+ case requirements.
187
+
188
+ Implementation Details:
189
+ - Currently uses keyword-based similarity (production would use embeddings)
190
+ - Supports various document formats (dict with content, or strings)
191
+ - Implements relevance scoring and ranking
192
+ - Filters results by similarity threshold
193
+ - Generates contextual answers from retrieved documents
194
+ - Maintains retrieval provenance for transparency
195
+
196
+ Error Handling:
197
+ - Handles empty document sets gracefully
198
+ - Validates query format and parameters
199
+ - Returns empty results for no matches
200
+ - Provides meaningful responses even with limited retrieval
201
+
202
+ Side Effects:
203
+ - No persistent side effects
204
+ - May trigger embedding generation (in production)
205
+ - May access external vector databases
206
+
207
+ Examples:
208
+ >>> # Test parameter structure without constructor validation
209
+ >>> agent = RetrievalAgent.__new__(RetrievalAgent)
210
+ >>> params = agent.get_parameters()
211
+ >>> assert "query" in params
212
+ >>> assert "documents" in params
213
+ >>> assert "top_k" in params
214
+ """
101
215
 
102
216
  def get_parameters(self) -> Dict[str, NodeParameter]:
103
217
  return {
@@ -17,12 +17,69 @@ class BaseAIProvider(ABC):
17
17
 
18
18
  This abstract class defines the common interface and shared functionality
19
19
  for providers that may support LLM operations, embedding operations, or both.
20
+ It establishes a unified pattern for provider initialization, capability
21
+ detection, and error handling across different AI services.
20
22
 
21
23
  Design Philosophy:
22
- - Single source of truth for provider availability
23
- - Shared client management and initialization
24
- - Common error handling patterns
25
- - Flexible support for providers with different capabilities
24
+ The BaseAIProvider follows the principle of "capability-based architecture"
25
+ where providers declare their capabilities explicitly. This allows for
26
+ flexible provider implementations that may support chat, embeddings, or
27
+ both, while maintaining a consistent interface. The design promotes:
28
+ - Single source of truth for provider availability
29
+ - Shared client management and initialization
30
+ - Common error handling patterns
31
+ - Flexible support for providers with different capabilities
32
+
33
+ Upstream Dependencies:
34
+ - Configuration systems providing API keys and credentials
35
+ - Environment variable loaders for secure credential management
36
+ - Package managers ensuring required dependencies
37
+ - Network infrastructure for API access
38
+
39
+ Downstream Consumers:
40
+ - LLMAgentNode: Uses chat capabilities for conversational AI
41
+ - EmbeddingGeneratorNode: Uses embedding capabilities for vector generation
42
+ - Provider selection logic choosing appropriate implementations
43
+ - Error handling systems catching provider-specific exceptions
44
+
45
+ Configuration:
46
+ Each provider implementation handles its own configuration needs,
47
+ typically through environment variables or explicit parameters.
48
+ Common patterns include API keys, endpoints, and model selections.
49
+
50
+ Implementation Details:
51
+ - Lazy initialization of clients to avoid unnecessary connections
52
+ - Cached availability checks to reduce repeated validation
53
+ - Capability dictionary for runtime feature detection
54
+ - Abstract methods enforce implementation of core functionality
55
+ - Thread-safe design for concurrent usage
56
+
57
+ Error Handling:
58
+ - Provider availability checked before operations
59
+ - Graceful degradation when providers are unavailable
60
+ - Standardized error responses across different providers
61
+ - Detailed error messages for debugging
62
+
63
+ Side Effects:
64
+ - May establish network connections to AI services
65
+ - May consume API quotas during availability checks
66
+ - Caches client instances for performance
67
+
68
+ Examples:
69
+ >>> # Provider implementation
70
+ >>> class MyProvider(BaseAIProvider):
71
+ ... def __init__(self):
72
+ ... super().__init__()
73
+ ... self._capabilities = {"chat": True, "embeddings": False}
74
+ ...
75
+ ... def is_available(self) -> bool:
76
+ ... # Check API key, dependencies, etc.
77
+ ... return True
78
+ >>>
79
+ >>> provider = MyProvider()
80
+ >>> assert provider.supports_chat() == True
81
+ >>> assert provider.supports_embeddings() == False
82
+ >>> assert provider.is_available() == True
26
83
  """
27
84
 
28
85
  def __init__(self):
@@ -70,8 +127,78 @@ class LLMProvider(BaseAIProvider):
70
127
  """
71
128
  Abstract base class for providers that support LLM chat operations.
72
129
 
73
- Providers that support chat operations should inherit from this class
74
- and implement the chat() method.
130
+ This class extends BaseAIProvider to define the interface for language model
131
+ providers. It ensures consistent chat operation interfaces across different
132
+ LLM services while allowing provider-specific optimizations and features.
133
+
134
+ Design Philosophy:
135
+ LLMProvider standardizes the chat interface while preserving flexibility
136
+ for provider-specific features. It follows the OpenAI message format as
137
+ the de facto standard, enabling easy switching between providers. The
138
+ design supports both simple completions and advanced features like
139
+ streaming, function calling, and custom parameters.
140
+
141
+ Upstream Dependencies:
142
+ - BaseAIProvider: Inherits core provider functionality
143
+ - Message formatting systems preparing chat inputs
144
+ - Token counting utilities for cost management
145
+ - Rate limiting systems managing API quotas
146
+
147
+ Downstream Consumers:
148
+ - LLMAgentNode: Primary consumer for chat operations
149
+ - ChatAgent: Uses for conversational interactions
150
+ - A2AAgentNode: Leverages for agent communication
151
+ - Response processing nodes handling outputs
152
+
153
+ Configuration:
154
+ Provider-specific parameters are passed through kwargs, allowing:
155
+ - Model selection (model parameter)
156
+ - Temperature and sampling parameters
157
+ - Token limits and stop sequences
158
+ - Provider-specific features (tools, functions, etc.)
159
+
160
+ Implementation Details:
161
+ - Standardized message format: List[Dict[str, str]]
162
+ - Messages contain 'role' and 'content' fields minimum
163
+ - Supports system, user, and assistant roles
164
+ - Response format standardized across providers
165
+ - Streaming support through callbacks (implementation-specific)
166
+
167
+ Error Handling:
168
+ - Invalid message format validation
169
+ - API error standardization
170
+ - Rate limit handling with retry guidance
171
+ - Token limit exceeded handling
172
+ - Network error recovery strategies
173
+
174
+ Side Effects:
175
+ - API calls consume tokens/credits
176
+ - May log conversations for debugging
177
+ - Updates internal usage metrics
178
+ - May trigger rate limiting
179
+
180
+ Examples:
181
+ >>> class MyLLMProvider(LLMProvider):
182
+ ... def is_available(self) -> bool:
183
+ ... return True # Check actual availability
184
+ ...
185
+ ... def chat(self, messages, **kwargs):
186
+ ... # Simulate LLM response
187
+ ... return {
188
+ ... "success": True,
189
+ ... "content": "Response to: " + messages[-1]["content"],
190
+ ... "model": kwargs.get("model", "default"),
191
+ ... "usage": {"prompt_tokens": 10, "completion_tokens": 5}
192
+ ... }
193
+ >>>
194
+ >>> provider = MyLLMProvider()
195
+ >>> messages = [
196
+ ... {"role": "system", "content": "You are helpful."},
197
+ ... {"role": "user", "content": "Hello!"}
198
+ ... ]
199
+ >>> response = provider.chat(messages, model="gpt-4")
200
+ >>> assert response["success"] == True
201
+ >>> assert "content" in response
75
202
  """
76
203
 
77
204
  def __init__(self):
@@ -97,8 +224,79 @@ class EmbeddingProvider(BaseAIProvider):
97
224
  """
98
225
  Abstract base class for providers that support embedding generation.
99
226
 
100
- Providers that support embedding operations should inherit from this class
101
- and implement the embed() and get_model_info() methods.
227
+ This class extends BaseAIProvider to define the interface for embedding
228
+ providers. It standardizes how text is converted to vector representations
229
+ across different embedding services while supporting provider-specific
230
+ optimizations and model configurations.
231
+
232
+ Design Philosophy:
233
+ EmbeddingProvider abstracts the complexity of different embedding models
234
+ and services behind a simple, consistent interface. It handles batching,
235
+ dimension management, and normalization while allowing providers to
236
+ optimize for their specific architectures. The design supports both
237
+ sentence and document embeddings with appropriate chunking strategies.
238
+
239
+ Upstream Dependencies:
240
+ - BaseAIProvider: Inherits core provider functionality
241
+ - Text preprocessing nodes preparing embedding inputs
242
+ - Chunking strategies for long documents
243
+ - Tokenization utilities for size management
244
+
245
+ Downstream Consumers:
246
+ - EmbeddingGeneratorNode: Primary consumer for vector generation
247
+ - Vector databases storing embeddings
248
+ - Similarity search implementations
249
+ - Clustering and classification systems
250
+
251
+ Configuration:
252
+ Provider-specific parameters include:
253
+ - Model selection for different embedding sizes/qualities
254
+ - Batch size limits for efficiency
255
+ - Normalization preferences
256
+ - Dimension specifications
257
+
258
+ Implementation Details:
259
+ - Batch processing for efficiency
260
+ - Automatic text truncation/chunking for model limits
261
+ - Vector normalization options
262
+ - Dimension validation and consistency
263
+ - Cache-friendly operations for repeated texts
264
+
265
+ Error Handling:
266
+ - Empty text handling
267
+ - Text length validation
268
+ - Batch size limit enforcement
269
+ - Model availability checking
270
+ - Dimension mismatch detection
271
+
272
+ Side Effects:
273
+ - API calls consume embedding quotas
274
+ - May cache embeddings for efficiency
275
+ - Updates usage metrics
276
+ - May trigger rate limiting
277
+
278
+ Examples:
279
+ >>> class MyEmbeddingProvider(EmbeddingProvider):
280
+ ... def is_available(self) -> bool:
281
+ ... return True
282
+ ...
283
+ ... def embed(self, texts, **kwargs):
284
+ ... # Simulate embedding generation
285
+ ... return [[0.1, 0.2, 0.3] for _ in texts]
286
+ ...
287
+ ... def get_model_info(self):
288
+ ... return {
289
+ ... "name": "my-embedding-model",
290
+ ... "dimensions": 3,
291
+ ... "max_tokens": 512
292
+ ... }
293
+ >>>
294
+ >>> provider = MyEmbeddingProvider()
295
+ >>> embeddings = provider.embed(["Hello", "World"])
296
+ >>> assert len(embeddings) == 2
297
+ >>> assert len(embeddings[0]) == 3
298
+ >>> info = provider.get_model_info()
299
+ >>> assert info["dimensions"] == 3
102
300
  """
103
301
 
104
302
  def __init__(self):