kailash 0.1.3__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.
- kailash/__init__.py +1 -1
- kailash/api/__init__.py +11 -1
- kailash/api/gateway.py +394 -0
- kailash/api/mcp_integration.py +478 -0
- kailash/api/workflow_api.py +29 -13
- kailash/nodes/ai/__init__.py +40 -4
- kailash/nodes/ai/a2a.py +1143 -0
- kailash/nodes/ai/agents.py +120 -6
- kailash/nodes/ai/ai_providers.py +224 -30
- kailash/nodes/ai/embedding_generator.py +34 -38
- kailash/nodes/ai/intelligent_agent_orchestrator.py +2114 -0
- kailash/nodes/ai/llm_agent.py +351 -356
- kailash/nodes/ai/self_organizing.py +1624 -0
- kailash/nodes/api/http.py +106 -25
- kailash/nodes/api/rest.py +116 -21
- kailash/nodes/base.py +60 -64
- kailash/nodes/code/python.py +61 -42
- kailash/nodes/data/__init__.py +10 -10
- kailash/nodes/data/readers.py +117 -66
- kailash/nodes/data/retrieval.py +1 -1
- kailash/nodes/data/sharepoint_graph.py +23 -25
- kailash/nodes/data/sql.py +24 -26
- kailash/nodes/data/writers.py +41 -44
- kailash/nodes/logic/__init__.py +9 -3
- kailash/nodes/logic/async_operations.py +60 -21
- kailash/nodes/logic/operations.py +43 -22
- kailash/nodes/logic/workflow.py +26 -18
- kailash/nodes/mcp/client.py +29 -33
- kailash/nodes/transform/__init__.py +8 -1
- kailash/nodes/transform/formatters.py +1 -1
- kailash/nodes/transform/processors.py +119 -4
- kailash/tracking/metrics_collector.py +6 -7
- kailash/utils/export.py +2 -2
- kailash/utils/templates.py +16 -16
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/METADATA +293 -29
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/RECORD +40 -35
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/WHEEL +0 -0
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/entry_points.txt +0 -0
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/licenses/LICENSE +0 -0
- {kailash-0.1.3.dist-info → kailash-0.1.5.dist-info}/top_level.txt +0 -0
kailash/nodes/ai/llm_agent.py
CHANGED
@@ -7,7 +7,7 @@ from kailash.nodes.base import Node, NodeParameter, register_node
|
|
7
7
|
|
8
8
|
|
9
9
|
@register_node()
|
10
|
-
class
|
10
|
+
class LLMAgentNode(Node):
|
11
11
|
"""
|
12
12
|
Advanced Large Language Model agent with LangChain integration and MCP
|
13
13
|
support.
|
@@ -64,63 +64,59 @@ class LLMAgent(Node):
|
|
64
64
|
- Logs agent interactions and performance metrics
|
65
65
|
|
66
66
|
Examples:
|
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
|
-
"args": ["-m", "compliance_mcp"]
|
121
|
-
}
|
122
|
-
]
|
123
|
-
)
|
67
|
+
>>> # Basic Q&A agent with OpenAI
|
68
|
+
>>> agent = LLMAgentNode()
|
69
|
+
>>> result = agent.run(
|
70
|
+
... provider="openai",
|
71
|
+
... model="gpt-4",
|
72
|
+
... messages=[
|
73
|
+
... {"role": "user", "content": "Analyze the customer data and provide insights"}
|
74
|
+
... ],
|
75
|
+
... system_prompt="You are a data analyst expert.",
|
76
|
+
... mcp_context=["data://customer_reports/*"]
|
77
|
+
... )
|
78
|
+
|
79
|
+
>>> # Tool-calling agent
|
80
|
+
>>> tool_agent = LLMAgentNode()
|
81
|
+
>>> result = tool_agent.run(
|
82
|
+
... provider="anthropic",
|
83
|
+
... model="claude-3-sonnet",
|
84
|
+
... messages=[{"role": "user", "content": "Create a report and email it"}],
|
85
|
+
... tools=[
|
86
|
+
... {
|
87
|
+
... "name": "create_report",
|
88
|
+
... "description": "Generate a data report",
|
89
|
+
... "parameters": {"type": "object", "properties": {"format": {"type": "string"}}}
|
90
|
+
... },
|
91
|
+
... {
|
92
|
+
... "name": "send_email",
|
93
|
+
... "description": "Send email with attachment",
|
94
|
+
... "parameters": {"type": "object", "properties": {"recipient": {"type": "string"}}}
|
95
|
+
... }
|
96
|
+
... ],
|
97
|
+
... conversation_id="report_session_123"
|
98
|
+
... )
|
99
|
+
|
100
|
+
>>> # RAG agent with MCP integration
|
101
|
+
>>> rag_agent = LLMAgentNode()
|
102
|
+
>>> result = rag_agent.run(
|
103
|
+
... provider="azure",
|
104
|
+
... model="gpt-4-turbo",
|
105
|
+
... messages=[{"role": "user", "content": "What are the compliance requirements?"}],
|
106
|
+
... rag_config={
|
107
|
+
... "enabled": True,
|
108
|
+
... "top_k": 5,
|
109
|
+
... "similarity_threshold": 0.8
|
110
|
+
... },
|
111
|
+
... mcp_servers=[
|
112
|
+
... {
|
113
|
+
... "name": "compliance-server",
|
114
|
+
... "transport": "stdio",
|
115
|
+
... "command": "python",
|
116
|
+
... "args": ["-m", "compliance_mcp"]
|
117
|
+
... }
|
118
|
+
... ]
|
119
|
+
... )
|
124
120
|
"""
|
125
121
|
|
126
122
|
def get_parameters(self) -> Dict[str, NodeParameter]:
|
@@ -262,190 +258,189 @@ class LLMAgent(Node):
|
|
262
258
|
|
263
259
|
Examples:
|
264
260
|
|
265
|
-
Basic usage with OpenAI
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
Using Ollama with custom model
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
With system prompt and conversation memory
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
With tool calling
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
With RAG (Retrieval Augmented Generation)
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
With MCP (Model Context Protocol) integration
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
Advanced configuration with all features
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
Error handling
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
print(f"- {suggestion}")
|
261
|
+
Basic usage with OpenAI:
|
262
|
+
|
263
|
+
>>> agent = LLMAgentNode()
|
264
|
+
>>> result = agent.run(
|
265
|
+
... provider="openai",
|
266
|
+
... model="gpt-4",
|
267
|
+
... messages=[
|
268
|
+
... {"role": "user", "content": "Explain quantum computing"}
|
269
|
+
... ],
|
270
|
+
... generation_config={
|
271
|
+
... "temperature": 0.7,
|
272
|
+
... "max_tokens": 500,
|
273
|
+
... "top_p": 0.9,
|
274
|
+
... "frequency_penalty": 0.0,
|
275
|
+
... "presence_penalty": 0.0
|
276
|
+
... }
|
277
|
+
... )
|
278
|
+
>>> print(result["response"]["content"]) # doctest: +SKIP
|
279
|
+
|
280
|
+
Using Ollama with custom model:
|
281
|
+
|
282
|
+
>>> result = agent.run(
|
283
|
+
... provider="ollama",
|
284
|
+
... model="llama3.1:8b-instruct-q8_0",
|
285
|
+
... messages=[
|
286
|
+
... {"role": "user", "content": "Write a Python function"}
|
287
|
+
... ],
|
288
|
+
... generation_config={
|
289
|
+
... "temperature": 0.5,
|
290
|
+
... "max_tokens": 1000,
|
291
|
+
... "top_p": 0.95,
|
292
|
+
... "seed": 42 # For reproducible outputs
|
293
|
+
... }
|
294
|
+
... ) # doctest: +SKIP
|
295
|
+
|
296
|
+
With system prompt and conversation memory:
|
297
|
+
|
298
|
+
>>> result = agent.run(
|
299
|
+
... provider="anthropic",
|
300
|
+
... model="claude-3-sonnet-20240229",
|
301
|
+
... system_prompt="You are a helpful coding assistant.",
|
302
|
+
... messages=[
|
303
|
+
... {"role": "user", "content": "Help me optimize this code"}
|
304
|
+
... ],
|
305
|
+
... conversation_id="coding-session-123",
|
306
|
+
... memory_config={
|
307
|
+
... "type": "buffer", # or "summary", "buffer_window"
|
308
|
+
... "max_tokens": 4000,
|
309
|
+
... "persistence": "memory" # or "disk", "database"
|
310
|
+
... }
|
311
|
+
... ) # doctest: +SKIP
|
312
|
+
|
313
|
+
With tool calling:
|
314
|
+
|
315
|
+
>>> result = agent.run(
|
316
|
+
... provider="openai",
|
317
|
+
... model="gpt-4-turbo",
|
318
|
+
... messages=[
|
319
|
+
... {"role": "user", "content": "Get the weather in NYC"}
|
320
|
+
... ],
|
321
|
+
... tools=[
|
322
|
+
... {
|
323
|
+
... "type": "function",
|
324
|
+
... "function": {
|
325
|
+
... "name": "get_weather",
|
326
|
+
... "description": "Get weather for a location",
|
327
|
+
... "parameters": {
|
328
|
+
... "type": "object",
|
329
|
+
... "properties": {
|
330
|
+
... "location": {"type": "string"},
|
331
|
+
... "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
|
332
|
+
... },
|
333
|
+
... "required": ["location"]
|
334
|
+
... }
|
335
|
+
... }
|
336
|
+
... }
|
337
|
+
... ],
|
338
|
+
... generation_config={
|
339
|
+
... "temperature": 0, # Use 0 for tool calling
|
340
|
+
... "tool_choice": "auto" # or "none", {"type": "function", "function": {"name": "get_weather"}}
|
341
|
+
... }
|
342
|
+
... ) # doctest: +SKIP
|
343
|
+
|
344
|
+
With RAG (Retrieval Augmented Generation):
|
345
|
+
|
346
|
+
>>> result = agent.run(
|
347
|
+
... provider="openai",
|
348
|
+
... model="gpt-4",
|
349
|
+
... messages=[
|
350
|
+
... {"role": "user", "content": "What is our refund policy?"}
|
351
|
+
... ],
|
352
|
+
... rag_config={
|
353
|
+
... "enabled": True,
|
354
|
+
... "top_k": 5, # Number of documents to retrieve
|
355
|
+
... "similarity_threshold": 0.7, # Minimum similarity score
|
356
|
+
... "embeddings": {
|
357
|
+
... "model": "text-embedding-ada-002",
|
358
|
+
... "dimension": 1536
|
359
|
+
... },
|
360
|
+
... "reranking": {
|
361
|
+
... "enabled": True,
|
362
|
+
... "model": "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
363
|
+
... }
|
364
|
+
... }
|
365
|
+
... ) # doctest: +SKIP
|
366
|
+
|
367
|
+
With MCP (Model Context Protocol) integration:
|
368
|
+
|
369
|
+
>>> result = agent.run(
|
370
|
+
... provider="anthropic",
|
371
|
+
... model="claude-3-opus-20240229",
|
372
|
+
... messages=[
|
373
|
+
... {"role": "user", "content": "Analyze the sales data"}
|
374
|
+
... ],
|
375
|
+
... mcp_servers=[
|
376
|
+
... {
|
377
|
+
... "name": "data-server",
|
378
|
+
... "transport": "stdio",
|
379
|
+
... "command": "python",
|
380
|
+
... "args": ["-m", "mcp_data_server"],
|
381
|
+
... "env": {"API_KEY": "secret"}
|
382
|
+
... }
|
383
|
+
... ],
|
384
|
+
... mcp_context=[
|
385
|
+
... "data://sales/2024/q4",
|
386
|
+
... "data://customers/segments",
|
387
|
+
... "resource://templates/analysis"
|
388
|
+
... ]
|
389
|
+
... ) # doctest: +SKIP
|
390
|
+
|
391
|
+
Advanced configuration with all features:
|
392
|
+
|
393
|
+
>>> result = agent.run(
|
394
|
+
... provider="openai",
|
395
|
+
... model="gpt-4-turbo",
|
396
|
+
... messages=[
|
397
|
+
... {"role": "user", "content": "Complex analysis request"}
|
398
|
+
... ],
|
399
|
+
... system_prompt="You are an expert data analyst.",
|
400
|
+
... conversation_id="analysis-session-456",
|
401
|
+
... memory_config={
|
402
|
+
... "type": "buffer_window",
|
403
|
+
... "max_tokens": 3000,
|
404
|
+
... "window_size": 10 # Keep last 10 exchanges
|
405
|
+
... },
|
406
|
+
... tools=[], # Tool definitions would go here
|
407
|
+
... rag_config={
|
408
|
+
... "enabled": True,
|
409
|
+
... "top_k": 3,
|
410
|
+
... "similarity_threshold": 0.8
|
411
|
+
... },
|
412
|
+
... mcp_servers=[], # MCP server configs would go here
|
413
|
+
... mcp_context=["data://reports/*"],
|
414
|
+
... generation_config={
|
415
|
+
... "temperature": 0.7,
|
416
|
+
... "max_tokens": 2000,
|
417
|
+
... "top_p": 0.9,
|
418
|
+
... "frequency_penalty": 0.1,
|
419
|
+
... "presence_penalty": 0.1,
|
420
|
+
... "stop": ["\\n\\n", "END"], # Stop sequences
|
421
|
+
... "logit_bias": {123: -100} # Token biases
|
422
|
+
... },
|
423
|
+
... streaming=False,
|
424
|
+
... timeout=120,
|
425
|
+
... max_retries=3
|
426
|
+
... ) # doctest: +SKIP
|
427
|
+
|
428
|
+
Error handling:
|
429
|
+
|
430
|
+
>>> result = agent.run(
|
431
|
+
... provider="openai",
|
432
|
+
... model="gpt-4",
|
433
|
+
... messages=[{"role": "user", "content": "Hello"}]
|
434
|
+
... )
|
435
|
+
>>> if result["success"]:
|
436
|
+
... print(f"Response: {result['response']['content']}")
|
437
|
+
... print(f"Tokens used: {result['usage']['total_tokens']}")
|
438
|
+
... print(f"Estimated cost: ${result['usage']['estimated_cost_usd']}")
|
439
|
+
... else:
|
440
|
+
... print(f"Error: {result['error']}")
|
441
|
+
... print(f"Type: {result['error_type']}")
|
442
|
+
... for suggestion in result['recovery_suggestions']:
|
443
|
+
... print(f"- {suggestion}") # doctest: +SKIP
|
449
444
|
"""
|
450
445
|
provider = kwargs["provider"]
|
451
446
|
model = kwargs["model"]
|
@@ -606,34 +601,34 @@ class LLMAgent(Node):
|
|
606
601
|
loaded_from (str): Source of the memory data
|
607
602
|
|
608
603
|
Examples:
|
609
|
-
Buffer memory (keep everything)
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
Window memory (keep last 5 exchanges)
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
Summary memory (summarize old content)
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
604
|
+
Buffer memory (keep everything):
|
605
|
+
|
606
|
+
>>> memory = self._load_conversation_memory(
|
607
|
+
... "chat-123",
|
608
|
+
... {"type": "buffer", "max_tokens": 4000}
|
609
|
+
... ) # doctest: +SKIP
|
610
|
+
|
611
|
+
Window memory (keep last 5 exchanges):
|
612
|
+
|
613
|
+
>>> memory = self._load_conversation_memory(
|
614
|
+
... "chat-456",
|
615
|
+
... {
|
616
|
+
... "type": "buffer_window",
|
617
|
+
... "window_size": 5,
|
618
|
+
... "max_tokens": 2000
|
619
|
+
... }
|
620
|
+
... ) # doctest: +SKIP
|
621
|
+
|
622
|
+
Summary memory (summarize old content):
|
623
|
+
|
624
|
+
>>> memory = self._load_conversation_memory(
|
625
|
+
... "chat-789",
|
626
|
+
... {
|
627
|
+
... "type": "summary",
|
628
|
+
... "max_tokens": 1000,
|
629
|
+
... "summary_method": "abstractive"
|
630
|
+
... }
|
631
|
+
... ) # doctest: +SKIP
|
637
632
|
"""
|
638
633
|
if not conversation_id:
|
639
634
|
return {"messages": [], "token_count": 0}
|
@@ -699,33 +694,33 @@ class LLMAgent(Node):
|
|
699
694
|
metadata (Dict): Additional resource metadata
|
700
695
|
|
701
696
|
Examples:
|
702
|
-
Connect to stdio MCP server
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
Connect to HTTP MCP server
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
697
|
+
Connect to stdio MCP server:
|
698
|
+
|
699
|
+
>>> context = self._retrieve_mcp_context(
|
700
|
+
... mcp_servers=[{
|
701
|
+
... "name": "data-server",
|
702
|
+
... "transport": "stdio",
|
703
|
+
... "command": "python",
|
704
|
+
... "args": ["-m", "mcp_data_server"],
|
705
|
+
... "env": {"API_KEY": "secret"}
|
706
|
+
... }],
|
707
|
+
... mcp_context=["data://sales/2024/q4"]
|
708
|
+
... ) # doctest: +SKIP
|
709
|
+
|
710
|
+
Connect to HTTP MCP server:
|
711
|
+
|
712
|
+
>>> context = self._retrieve_mcp_context(
|
713
|
+
... mcp_servers=[{
|
714
|
+
... "name": "api-server",
|
715
|
+
... "transport": "http",
|
716
|
+
... "url": "https://mcp.example.com",
|
717
|
+
... "headers": {"Authorization": "Bearer token"}
|
718
|
+
... }],
|
719
|
+
... mcp_context=[
|
720
|
+
... "resource://customers/segments",
|
721
|
+
... "prompt://analysis/financial"
|
722
|
+
... ]
|
723
|
+
... ) # doctest: +SKIP
|
729
724
|
"""
|
730
725
|
if not (mcp_servers or mcp_context):
|
731
726
|
return []
|
@@ -807,65 +802,65 @@ class LLMAgent(Node):
|
|
807
802
|
search_time_ms (float): Search duration
|
808
803
|
|
809
804
|
Examples:
|
810
|
-
Basic RAG retrieval
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
Advanced RAG with reranking
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
Hybrid search with filters
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
805
|
+
Basic RAG retrieval:
|
806
|
+
|
807
|
+
>>> rag_result = self._perform_rag_retrieval(
|
808
|
+
... messages=[{"role": "user", "content": "What is the refund policy?"}],
|
809
|
+
... rag_config={
|
810
|
+
... "enabled": True,
|
811
|
+
... "top_k": 5,
|
812
|
+
... "similarity_threshold": 0.7
|
813
|
+
... },
|
814
|
+
... mcp_context=[]
|
815
|
+
... ) # doctest: +SKIP
|
816
|
+
|
817
|
+
Advanced RAG with reranking:
|
818
|
+
|
819
|
+
>>> rag_result = self._perform_rag_retrieval(
|
820
|
+
... messages=[{"role": "user", "content": "Technical specifications"}],
|
821
|
+
... rag_config={
|
822
|
+
... "enabled": True,
|
823
|
+
... "top_k": 10,
|
824
|
+
... "similarity_threshold": 0.6,
|
825
|
+
... "embeddings": {
|
826
|
+
... "model": "text-embedding-ada-002",
|
827
|
+
... "dimension": 1536,
|
828
|
+
... "provider": "openai"
|
829
|
+
... },
|
830
|
+
... "reranking": {
|
831
|
+
... "enabled": True,
|
832
|
+
... "model": "cross-encoder/ms-marco-MiniLM-L-12-v2",
|
833
|
+
... "top_n": 3
|
834
|
+
... },
|
835
|
+
... "vector_store": {
|
836
|
+
... "type": "pinecone",
|
837
|
+
... "index_name": "products",
|
838
|
+
... "namespace": "technical-docs"
|
839
|
+
... }
|
840
|
+
... },
|
841
|
+
... mcp_context=[]
|
842
|
+
... ) # doctest: +SKIP
|
843
|
+
|
844
|
+
Hybrid search with filters:
|
845
|
+
|
846
|
+
>>> rag_result = self._perform_rag_retrieval(
|
847
|
+
... messages=[{"role": "user", "content": "Python tutorials"}],
|
848
|
+
... rag_config={
|
849
|
+
... "enabled": True,
|
850
|
+
... "top_k": 5,
|
851
|
+
... "similarity_threshold": 0.7,
|
852
|
+
... "filters": {
|
853
|
+
... "category": "tutorial",
|
854
|
+
... "language": "python",
|
855
|
+
... "level": ["beginner", "intermediate"]
|
856
|
+
... },
|
857
|
+
... "hybrid_search": {
|
858
|
+
... "enabled": True,
|
859
|
+
... "alpha": 0.7 # 70% vector, 30% keyword
|
860
|
+
... }
|
861
|
+
... },
|
862
|
+
... mcp_context=[]
|
863
|
+
... ) # doctest: +SKIP
|
869
864
|
"""
|
870
865
|
if not rag_config.get("enabled", False):
|
871
866
|
return {"documents": [], "scores": []}
|