-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenrouter_kernel.py
More file actions
executable file
·956 lines (791 loc) · 30.8 KB
/
openrouter_kernel.py
File metadata and controls
executable file
·956 lines (791 loc) · 30.8 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#!/usr/bin/env python3
"""
OpenRouter Kernel - Core agent kernel with robust function registry
An extensible kernel for the Llama4 agent system that provides:
- Protected kernel with safe dynamic extension
- Function registry with parameter validation
- Hot module reloading
- Connection to OpenRouter API for Llama 4 access
- Dynamic system prompt enhancement
- Automatic capability inference
"""
import os
import sys
import json
import inspect
import importlib
import time
from typing import Dict, List, Any, Optional, Tuple, Union, Callable
import requests
import aiohttp
import nest_asyncio
nest_asyncio.apply()
# Optional capability for dynamic prompt enhancement
try:
from modules.prompt_enhancer import enhance_system_prompt, PromptManager
PROMPT_ENHANCEMENT_AVAILABLE = True
except ImportError:
PROMPT_ENHANCEMENT_AVAILABLE = False
# === FUNCTION REGISTRY ===
class FunctionRegistry:
"""Manages functions that can be called by the agent"""
def __init__(self):
self.functions = {}
self.function_schemas = {}
def register(self, name: str, func: Callable, description: str,
parameter_schema: Optional[Dict] = None) -> None:
"""
Register a function with the kernel
Args:
name: Function name for the registry
func: Function to register
description: Function description
parameter_schema: Optional schema for parameters
"""
# Register the function
self.functions[name] = func
# Create schema if not provided
if parameter_schema is None:
parameter_schema = self._infer_schema(func)
# Store the schema
self.function_schemas[name] = {
"name": name,
"description": description,
"parameters": parameter_schema
}
def unregister(self, name: str) -> bool:
"""
Remove a function from the registry
Args:
name: Function name to remove
Returns:
Whether function was found and removed
"""
if name in self.functions:
del self.functions[name]
del self.function_schemas[name]
return True
return False
def get(self, name: str) -> Optional[Callable]:
"""
Get a function by name
Args:
name: Function name
Returns:
Function or None if not found
"""
return self.functions.get(name)
def get_schema(self, name: str) -> Optional[Dict]:
"""
Get function schema by name
Args:
name: Function name
Returns:
Schema or None if not found
"""
return self.function_schemas.get(name)
def list_functions(self) -> List[Dict]:
"""
Get list of all registered functions
Returns:
List of function schemas
"""
return list(self.function_schemas.values())
def _infer_schema(self, func: Callable) -> Dict:
"""
Infer parameter schema from function signature
Args:
func: Function to analyze
Returns:
Parameter schema
"""
# Get function signature
sig = inspect.signature(func)
schema = {}
# Process each parameter
for name, param in sig.parameters.items():
# Skip self parameter for methods
if name == 'self':
continue
param_schema = {
"type": "any",
"description": f"Parameter: {name}"
}
# Add default value if present
if param.default is not param.empty:
param_schema["default"] = param.default
# Mark as optional if has default
if param.default is not param.empty:
param_schema["optional"] = True
# Add to schema
schema[name] = param_schema
return schema
def call(self, name: str, **kwargs) -> Any:
"""
Call a registered function with validation
Args:
name: Function name
**kwargs: Parameters for the function
Returns:
Function result
"""
# Check if function exists
func = self.get(name)
if func is None:
return {
"status": "error",
"message": f"Function '{name}' not found"
}
# Validate parameters
schema = self.get_schema(name)
validation_result = self._validate_parameters(schema, kwargs)
if validation_result["valid"] is False:
return {
"status": "error",
"message": f"Parameter validation failed: {validation_result['message']}"
}
# Call the function
try:
result = func(**kwargs)
return result
except Exception as e:
return {
"status": "error",
"message": f"Function execution error: {str(e)}"
}
def _validate_parameters(self, schema: Dict, params: Dict) -> Dict:
"""
Validate parameters against schema
Args:
schema: Function schema
params: Parameters to validate
Returns:
Validation result
"""
if not schema or "parameters" not in schema:
# No schema to validate against
return {"valid": True}
# Check for missing required parameters
for param_name, param_schema in schema["parameters"].items():
if param_name not in params and not param_schema.get("optional", False):
return {
"valid": False,
"message": f"Missing required parameter: {param_name}"
}
# Check for unexpected parameters
for param_name in params:
if param_name not in schema["parameters"]:
return {
"valid": False,
"message": f"Unexpected parameter: {param_name}"
}
# All checks passed
return {"valid": True}
# === CODE VALIDATION ===
class CodeValidator:
"""Validates code for potential security issues"""
def __init__(self):
# Define patterns that should be blocked
self.blocked_patterns = [
"os.system", "subprocess.call", "subprocess.Popen",
"exec(", "eval(", "__import__(",
"open(", "file.write", "file.close"
]
# Define modules that should be blocked
self.blocked_modules = [
"subprocess", "pty", "socket", "ctypes"
]
def validate(self, code: str) -> Dict:
"""
Validate code for security issues
Args:
code: Python code to validate
Returns:
Dictionary with validation result
"""
# Check for blocked patterns
for pattern in self.blocked_patterns:
if pattern in code:
return {
"valid": False,
"message": f"Code contains blocked pattern: {pattern}"
}
# Check for blocked module imports
for module in self.blocked_modules:
if f"import {module}" in code or f"from {module}" in code:
return {
"valid": False,
"message": f"Code imports blocked module: {module}"
}
# Basic syntax check
try:
compile(code, "<string>", "exec")
except SyntaxError as e:
return {
"valid": False,
"message": f"Syntax error: {str(e)}"
}
# All checks passed
return {"valid": True}
# === MODULE MANAGER ===
class ModuleManager:
"""Manages dynamic module loading and hot reloading"""
def __init__(self, modules_dir: str = "modules"):
self.modules_dir = modules_dir
self.loaded_modules = {}
self.module_timestamps = {}
# Create modules directory if it doesn't exist
if not os.path.exists(modules_dir):
os.makedirs(modules_dir)
def load_module(self, module_name: str) -> Tuple[bool, Any]:
"""
Load a module by name
Args:
module_name: Name of module to load
Returns:
Tuple of (success, module/error)
"""
try:
# Check if module is in the modules directory
module_path = os.path.join(self.modules_dir, f"{module_name}.py")
if os.path.exists(module_path):
# Load from file
return self._load_from_file(module_name, module_path)
else:
# Try to import from Python path
return self._load_from_import(module_name)
except Exception as e:
return False, str(e)
def _load_from_file(self, module_name: str, module_path: str) -> Tuple[bool, Any]:
"""
Load module from file
Args:
module_name: Name of module
module_path: Path to module file
Returns:
Tuple of (success, module/error)
"""
try:
# Get file modification time
mod_time = os.path.getmtime(module_path)
# Check if we need to reload
if module_name in self.loaded_modules:
if self.module_timestamps.get(module_name) == mod_time:
# Module hasn't changed, return cached version
return True, self.loaded_modules[module_name]
# Load or reload the module
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Cache the module
self.loaded_modules[module_name] = module
self.module_timestamps[module_name] = mod_time
return True, module
except Exception as e:
return False, str(e)
def _load_from_import(self, module_name: str) -> Tuple[bool, Any]:
"""
Load module from Python path
Args:
module_name: Name of module
Returns:
Tuple of (success, module/error)
"""
try:
# Try to import the module
module = importlib.import_module(module_name)
# Cache the module
self.loaded_modules[module_name] = module
self.module_timestamps[module_name] = time.time()
return True, module
except ImportError as e:
return False, str(e)
def reload_module(self, module_name: str) -> Tuple[bool, Any]:
"""
Reload a module by name
Args:
module_name: Name of module to reload
Returns:
Tuple of (success, module/error)
"""
# Remove from cache
if module_name in self.loaded_modules:
del self.loaded_modules[module_name]
if module_name in self.module_timestamps:
del self.module_timestamps[module_name]
# Load the module again
return self.load_module(module_name)
def list_modules(self) -> List[str]:
"""
Get list of available modules
Returns:
List of module names
"""
if not os.path.exists(self.modules_dir):
return []
modules = []
# List Python files in modules directory
for file in os.listdir(self.modules_dir):
if file.endswith(".py") and not file.startswith("__"):
modules.append(file[:-3]) # Remove .py extension
return modules
# === OPENROUTER KERNEL ===
class OpenRouterKernel:
"""
Main kernel for OpenRouter-based agent system
"""
def __init__(self, api_key: Optional[str] = None, model: str = "meta-llama/llama-4-maverick"):
"""
Initialize the kernel
Args:
api_key: OpenRouter API key
model: Model to use
"""
self.api_key = api_key or os.environ.get("OPENROUTER_API_KEY")
self.model = model
self.api_base = "https://openrouter.ai/api/v1"
# Create function registry
self.function_registry = FunctionRegistry()
# Create code validator
self.code_validator = CodeValidator()
# Create module manager
self.module_manager = ModuleManager()
# Create prompt manager if available
self.prompt_manager = None
if PROMPT_ENHANCEMENT_AVAILABLE:
self.prompt_manager = PromptManager(self.default_system_prompt())
# Register built-in functions
self._register_built_ins()
def default_system_prompt(self) -> str:
"""
Get the default system prompt
Returns:
Default system prompt
"""
return """You are an advanced AI assistant powered by Llama 4 Maverick.
You have access to a variety of tools and capabilities that help you assist the user.
You can execute code, manage files, search for information, and much more.
Always be helpful, accurate, and responsive to the user's needs."""
def _register_built_ins(self) -> None:
"""Register built-in kernel functions"""
# Register kernel info function
self.function_registry.register(
"kernel_info",
self.get_kernel_info,
"Get information about the kernel",
{}
)
# Register list functions function
self.function_registry.register(
"list_functions",
self.list_functions,
"List all registered functions",
{}
)
# Register load module function
self.function_registry.register(
"load_module",
self.load_module,
"Load a module",
{
"module_name": {"type": "string", "description": "Name of module to load"}
}
)
# Register list modules function
self.function_registry.register(
"list_modules",
self.list_modules,
"List available modules",
{}
)
# Register enhance prompt function if available
if PROMPT_ENHANCEMENT_AVAILABLE:
self.function_registry.register(
"enhance_system_prompt",
self.enhance_system_prompt,
"Enhance the system prompt with capabilities",
{
"context": {"type": "string", "description": "Optional context for prompt", "optional": True}
}
)
def register_function(self, name: str, func: Callable, description: str,
parameter_schema: Optional[Dict] = None) -> None:
"""
Register a function with the kernel
Args:
name: Function name for the registry
func: Function to register
description: Function description
parameter_schema: Optional schema for parameters
"""
self.function_registry.register(name, func, description, parameter_schema)
def call_function(self, name: str, **kwargs) -> Any:
"""
Call a registered function
Args:
name: Function name
**kwargs: Parameters for the function
Returns:
Function result
"""
return self.function_registry.call(name, **kwargs)
def get_kernel_info(self) -> Dict:
"""
Get information about the kernel
Returns:
Dictionary with kernel info
"""
return {
"status": "success",
"model": self.model,
"functions": len(self.function_registry.list_functions()),
"modules": len(self.module_manager.list_modules()),
"prompt_enhancement": PROMPT_ENHANCEMENT_AVAILABLE
}
def list_functions(self) -> Dict:
"""
List all registered functions
Returns:
Dictionary with function list
"""
return {
"status": "success",
"functions": self.function_registry.list_functions()
}
def load_module(self, module_name: str) -> Dict:
"""
Load a module and register its functions
Args:
module_name: Name of module to load
Returns:
Dictionary with load result
"""
# Load the module
success, result = self.module_manager.load_module(module_name)
if not success:
return {
"status": "error",
"message": f"Failed to load module: {result}"
}
# Register functions with register_kernel_function decorator
functions_registered = 0
# Get all module attributes
for name, obj in inspect.getmembers(result):
# Skip private attributes
if name.startswith('_'):
continue
# Check if attribute has register_kernel_function metadata
if hasattr(obj, '_kernel_function'):
# Register the function
func_name = obj._kernel_function.get('name', name)
description = obj._kernel_function.get('description', '')
schema = obj._kernel_function.get('schema', None)
self.register_function(func_name, obj, description, schema)
functions_registered += 1
# Update prompt manager with new capabilities
if self.prompt_manager and PROMPT_ENHANCEMENT_AVAILABLE:
self.prompt_manager.scan_for_capabilities()
return {
"status": "success",
"module": module_name,
"functions_registered": functions_registered
}
def list_modules(self) -> Dict:
"""
List available modules
Returns:
Dictionary with module list
"""
modules = self.module_manager.list_modules()
return {
"status": "success",
"modules": modules
}
def enhance_system_prompt(self, context: Optional[str] = None) -> Dict:
"""
Enhance the system prompt with capabilities
Args:
context: Optional conversation context
Returns:
Dictionary with enhanced prompt
"""
if not PROMPT_ENHANCEMENT_AVAILABLE:
return {
"status": "error",
"message": "Prompt enhancement not available"
}
if self.prompt_manager:
# Scan for capabilities
self.prompt_manager.scan_for_capabilities()
# Generate enhanced prompt
enhanced_prompt = self.prompt_manager.generate_prompt(context)
return {
"status": "success",
"prompt": enhanced_prompt
}
else:
# Use the standalone function
try:
enhanced_prompt = enhance_system_prompt(
self.default_system_prompt(),
context
)
return {
"status": "success",
"prompt": enhanced_prompt
}
except Exception as e:
return {
"status": "error",
"message": f"Error enhancing prompt: {str(e)}"
}
def chat(self, prompt: str, system_prompt: Optional[str] = None,
functions: Optional[List[Dict]] = None) -> Dict:
"""
Send a chat request to the OpenRouter API
Args:
prompt: User prompt
system_prompt: Optional system prompt
functions: Optional function definitions
Returns:
OpenRouter API response
"""
if not self.api_key:
return {
"status": "error",
"message": "OpenRouter API key not set"
}
# Use enhanced system prompt if available
if system_prompt is None and self.prompt_manager and PROMPT_ENHANCEMENT_AVAILABLE:
system_prompt = self.prompt_manager.generate_prompt()
elif system_prompt is None:
system_prompt = self.default_system_prompt()
# Prepare request
url = f"{self.api_base}/chat/completions"
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
}
# Add functions if provided
if functions:
data["functions"] = functions
# Send request
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except Exception as e:
return {
"status": "error",
"message": f"API request failed: {str(e)}"
}
async def async_chat(self, prompt: str, system_prompt: Optional[str] = None,
functions: Optional[List[Dict]] = None) -> Dict:
"""
Send an async chat request to the OpenRouter API
Args:
prompt: User prompt
system_prompt: Optional system prompt
functions: Optional function definitions
Returns:
OpenRouter API response
"""
if not self.api_key:
return {
"status": "error",
"message": "OpenRouter API key not set"
}
# Use enhanced system prompt if available
if system_prompt is None and self.prompt_manager and PROMPT_ENHANCEMENT_AVAILABLE:
system_prompt = self.prompt_manager.generate_prompt()
elif system_prompt is None:
system_prompt = self.default_system_prompt()
# Prepare request
url = f"{self.api_base}/chat/completions"
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
}
# Add functions if provided
if functions:
data["functions"] = functions
# Send request
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
if response.status != 200:
return {
"status": "error",
"message": f"API request failed with status {response.status}"
}
return await response.json()
except Exception as e:
return {
"status": "error",
"message": f"API request failed: {str(e)}"
}
def stream_chat(self, prompt: str, system_prompt: Optional[str] = None,
functions: Optional[List[Dict]] = None):
"""
Stream a chat response from the OpenRouter API
Args:
prompt: User prompt
system_prompt: Optional system prompt
functions: Optional function definitions
Yields:
Response chunks
"""
if not self.api_key:
yield {
"type": "error",
"content": "OpenRouter API key not set"
}
return
# Use enhanced system prompt if available
if system_prompt is None and self.prompt_manager and PROMPT_ENHANCEMENT_AVAILABLE:
system_prompt = self.prompt_manager.generate_prompt()
elif system_prompt is None:
system_prompt = self.default_system_prompt()
# Prepare request
url = f"{self.api_base}/chat/completions"
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
"stream": True
}
# Add functions if provided
if functions:
data["functions"] = functions
# Send request
try:
response = requests.post(url, headers=headers, json=data, stream=True)
response.raise_for_status()
# Process streaming response
for line in response.iter_lines():
if line:
# Skip "data: " prefix
if line.startswith(b"data: "):
line = line[6:]
# Skip empty lines and "[DONE]"
if line and line != b"[DONE]":
try:
chunk = json.loads(line)
if "choices" in chunk and chunk["choices"]:
choice = chunk["choices"][0]
if "delta" in choice:
delta = choice["delta"]
# Handle content
if "content" in delta and delta["content"]:
yield {
"type": "content",
"content": delta["content"]
}
# Handle function call
if "function_call" in delta:
yield {
"type": "function_call",
"function_call": delta["function_call"]
}
except json.JSONDecodeError:
# Skip invalid JSON
pass
except Exception as e:
yield {
"type": "error",
"content": f"API request failed: {str(e)}"
}
# === FUNCTION DECORATOR ===
def register_kernel_function(name: Optional[str] = None,
description: Optional[str] = None,
schema: Optional[Dict] = None):
"""
Decorator to register a function with the kernel
Args:
name: Function name override
description: Function description
schema: Parameter schema
Returns:
Decorator function
"""
def decorator(func):
# Store kernel function metadata
func._kernel_function = {
'name': name or func.__name__,
'description': description or inspect.getdoc(func) or '',
'schema': schema
}
return func
return decorator
# === MAIN FUNCTION ===
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="OpenRouter Kernel")
parser.add_argument("--api-key", help="OpenRouter API key")
parser.add_argument("--model", default="meta-llama/llama-4-maverick", help="Model to use")
parser.add_argument("--prompt", help="Test prompt")
parser.add_argument("--system-prompt", help="System prompt file")
parser.add_argument("--load-module", help="Module to load")
parser.add_argument("--list-modules", action="store_true", help="List available modules")
parser.add_argument("--list-functions", action="store_true", help="List registered functions")
parser.add_argument("--enhance-prompt", action="store_true", help="Enhance system prompt")
args = parser.parse_args()
# Create kernel
kernel = OpenRouterKernel(api_key=args.api_key, model=args.model)
# Load module if specified
if args.load_module:
result = kernel.load_module(args.load_module)
print(json.dumps(result, indent=2))
# List modules if requested
if args.list_modules:
result = kernel.list_modules()
print(json.dumps(result, indent=2))
# List functions if requested
if args.list_functions:
result = kernel.list_functions()
print(json.dumps(result, indent=2))
# Enhance prompt if requested
if args.enhance_prompt:
result = kernel.enhance_system_prompt()
if result["status"] == "success":
print(result["prompt"])
else:
print(json.dumps(result, indent=2))
# Send test prompt if specified
if args.prompt:
system_prompt = None
# Load system prompt from file if specified
if args.system_prompt:
try:
with open(args.system_prompt, 'r') as f:
system_prompt = f.read()
except Exception as e:
print(f"Error reading system prompt: {e}")
sys.exit(1)
# Send the prompt
for chunk in kernel.stream_chat(args.prompt, system_prompt):
if chunk["type"] == "content":
print(chunk["content"], end="")
elif chunk["type"] == "error":
print(f"\nError: {chunk['content']}")
print() # Add newline at end