silik-kernel 1.1__tar.gz

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,11 @@
1
+ .venv
2
+ brouillon.py
3
+ config.yaml
4
+ config_.yaml
5
+ example_messages.json
6
+ kernel-*
7
+ **/dist
8
+ **/**.egg-info
9
+ **/build
10
+ **/data_kernelspec
11
+ octave-workspace
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,284 @@
1
+ Metadata-Version: 2.4
2
+ Name: silik-kernel
3
+ Version: 1.1
4
+ Summary: Multi-kernel Manager
5
+ Author-email: Marius Garénaux-Gruau <marius.garenaux-gruau@irisa.fr>
6
+ License:
7
+ Apache License
8
+ Version 2.0, January 2004
9
+ http://www.apache.org/licenses/
10
+
11
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
12
+
13
+ 1. Definitions.
14
+
15
+ "License" shall mean the terms and conditions for use, reproduction,
16
+ and distribution as defined by Sections 1 through 9 of this document.
17
+
18
+ "Licensor" shall mean the copyright owner or entity authorized by
19
+ the copyright owner that is granting the License.
20
+
21
+ "Legal Entity" shall mean the union of the acting entity and all
22
+ other entities that control, are controlled by, or are under common
23
+ control with that entity. For the purposes of this definition,
24
+ "control" means (i) the power, direct or indirect, to cause the
25
+ direction or management of such entity, whether by contract or
26
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
27
+ outstanding shares, or (iii) beneficial ownership of such entity.
28
+
29
+ "You" (or "Your") shall mean an individual or Legal Entity
30
+ exercising permissions granted by this License.
31
+
32
+ "Source" form shall mean the preferred form for making modifications,
33
+ including but not limited to software source code, documentation
34
+ source, and configuration files.
35
+
36
+ "Object" form shall mean any form resulting from mechanical
37
+ transformation or translation of a Source form, including but
38
+ not limited to compiled object code, generated documentation,
39
+ and conversions to other media types.
40
+
41
+ "Work" shall mean the work of authorship, whether in Source or
42
+ Object form, made available under the License, as indicated by a
43
+ copyright notice that is included in or attached to the work
44
+ (an example is provided in the Appendix below).
45
+
46
+ "Derivative Works" shall mean any work, whether in Source or Object
47
+ form, that is based on (or derived from) the Work and for which the
48
+ editorial revisions, annotations, elaborations, or other modifications
49
+ represent, as a whole, an original work of authorship. For the purposes
50
+ of this License, Derivative Works shall not include works that remain
51
+ separable from, or merely link (or bind by name) to the interfaces of,
52
+ the Work and Derivative Works thereof.
53
+
54
+ "Contribution" shall mean any work of authorship, including
55
+ the original version of the Work and any modifications or additions
56
+ to that Work or Derivative Works thereof, that is intentionally
57
+ submitted to Licensor for inclusion in the Work by the copyright owner
58
+ or by an individual or Legal Entity authorized to submit on behalf of
59
+ the copyright owner. For the purposes of this definition, "submitted"
60
+ means any form of electronic, verbal, or written communication sent
61
+ to the Licensor or its representatives, including but not limited to
62
+ communication on electronic mailing lists, source code control systems,
63
+ and issue tracking systems that are managed by, or on behalf of, the
64
+ Licensor for the purpose of discussing and improving the Work, but
65
+ excluding communication that is conspicuously marked or otherwise
66
+ designated in writing by the copyright owner as "Not a Contribution."
67
+
68
+ "Contributor" shall mean Licensor and any individual or Legal Entity
69
+ on behalf of whom a Contribution has been received by Licensor and
70
+ subsequently incorporated within the Work.
71
+
72
+ 2. Grant of Copyright License. Subject to the terms and conditions of
73
+ this License, each Contributor hereby grants to You a perpetual,
74
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
75
+ copyright license to reproduce, prepare Derivative Works of,
76
+ publicly display, publicly perform, sublicense, and distribute the
77
+ Work and such Derivative Works in Source or Object form.
78
+
79
+ 3. Grant of Patent License. Subject to the terms and conditions of
80
+ this License, each Contributor hereby grants to You a perpetual,
81
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
82
+ (except as stated in this section) patent license to make, have made,
83
+ use, offer to sell, sell, import, and otherwise transfer the Work,
84
+ where such license applies only to those patent claims licensable
85
+ by such Contributor that are necessarily infringed by their
86
+ Contribution(s) alone or by combination of their Contribution(s)
87
+ with the Work to which such Contribution(s) was submitted. If You
88
+ institute patent litigation against any entity (including a
89
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
90
+ or a Contribution incorporated within the Work constitutes direct
91
+ or contributory patent infringement, then any patent licenses
92
+ granted to You under this License for that Work shall terminate
93
+ as of the date such litigation is filed.
94
+
95
+ 4. Redistribution. You may reproduce and distribute copies of the
96
+ Work or Derivative Works thereof in any medium, with or without
97
+ modifications, and in Source or Object form, provided that You
98
+ meet the following conditions:
99
+
100
+ (a) You must give any other recipients of the Work or
101
+ Derivative Works a copy of this License; and
102
+
103
+ (b) You must cause any modified files to carry prominent notices
104
+ stating that You changed the files; and
105
+
106
+ (c) You must retain, in the Source form of any Derivative Works
107
+ that You distribute, all copyright, patent, trademark, and
108
+ attribution notices from the Source form of the Work,
109
+ excluding those notices that do not pertain to any part of
110
+ the Derivative Works; and
111
+
112
+ (d) If the Work includes a "NOTICE" text file as part of its
113
+ distribution, then any Derivative Works that You distribute must
114
+ include a readable copy of the attribution notices contained
115
+ within such NOTICE file, excluding those notices that do not
116
+ pertain to any part of the Derivative Works, in at least one
117
+ of the following places: within a NOTICE text file distributed
118
+ as part of the Derivative Works; within the Source form or
119
+ documentation, if provided along with the Derivative Works; or,
120
+ within a display generated by the Derivative Works, if and
121
+ wherever such third-party notices normally appear. The contents
122
+ of the NOTICE file are for informational purposes only and
123
+ do not modify the License. You may add Your own attribution
124
+ notices within Derivative Works that You distribute, alongside
125
+ or as an addendum to the NOTICE text from the Work, provided
126
+ that such additional attribution notices cannot be construed
127
+ as modifying the License.
128
+
129
+ You may add Your own copyright statement to Your modifications and
130
+ may provide additional or different license terms and conditions
131
+ for use, reproduction, or distribution of Your modifications, or
132
+ for any such Derivative Works as a whole, provided Your use,
133
+ reproduction, and distribution of the Work otherwise complies with
134
+ the conditions stated in this License.
135
+
136
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
137
+ any Contribution intentionally submitted for inclusion in the Work
138
+ by You to the Licensor shall be under the terms and conditions of
139
+ this License, without any additional terms or conditions.
140
+ Notwithstanding the above, nothing herein shall supersede or modify
141
+ the terms of any separate license agreement you may have executed
142
+ with Licensor regarding such Contributions.
143
+
144
+ 6. Trademarks. This License does not grant permission to use the trade
145
+ names, trademarks, service marks, or product names of the Licensor,
146
+ except as required for reasonable and customary use in describing the
147
+ origin of the Work and reproducing the content of the NOTICE file.
148
+
149
+ 7. Disclaimer of Warranty. Unless required by applicable law or
150
+ agreed to in writing, Licensor provides the Work (and each
151
+ Contributor provides its Contributions) on an "AS IS" BASIS,
152
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
153
+ implied, including, without limitation, any warranties or conditions
154
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
155
+ PARTICULAR PURPOSE. You are solely responsible for determining the
156
+ appropriateness of using or redistributing the Work and assume any
157
+ risks associated with Your exercise of permissions under this License.
158
+
159
+ 8. Limitation of Liability. In no event and under no legal theory,
160
+ whether in tort (including negligence), contract, or otherwise,
161
+ unless required by applicable law (such as deliberate and grossly
162
+ negligent acts) or agreed to in writing, shall any Contributor be
163
+ liable to You for damages, including any direct, indirect, special,
164
+ incidental, or consequential damages of any character arising as a
165
+ result of this License or out of the use or inability to use the
166
+ Work (including but not limited to damages for loss of goodwill,
167
+ work stoppage, computer failure or malfunction, or any and all
168
+ other commercial damages or losses), even if such Contributor
169
+ has been advised of the possibility of such damages.
170
+
171
+ 9. Accepting Warranty or Additional Liability. While redistributing
172
+ the Work or Derivative Works thereof, You may choose to offer,
173
+ and charge a fee for, acceptance of support, warranty, indemnity,
174
+ or other liability obligations and/or rights consistent with this
175
+ License. However, in accepting such obligations, You may act only
176
+ on Your own behalf and on Your sole responsibility, not on behalf
177
+ of any other Contributor, and only if You agree to indemnify,
178
+ defend, and hold each Contributor harmless for any liability
179
+ incurred by, or claims asserted against, such Contributor by reason
180
+ of your accepting any such warranty or additional liability.
181
+
182
+ END OF TERMS AND CONDITIONS
183
+
184
+ APPENDIX: How to apply the Apache License to your work.
185
+
186
+ To apply the Apache License to your work, attach the following
187
+ boilerplate notice, with the fields enclosed by brackets "[]"
188
+ replaced with your own identifying information. (Don't include
189
+ the brackets!) The text should be enclosed in the appropriate
190
+ comment syntax for the file format. We also recommend that a
191
+ file or class name and description of purpose be included on the
192
+ same "printed page" as the copyright notice for easier
193
+ identification within third-party archives.
194
+
195
+ Copyright [yyyy] [name of copyright owner]
196
+
197
+ Licensed under the Apache License, Version 2.0 (the "License");
198
+ you may not use this file except in compliance with the License.
199
+ You may obtain a copy of the License at
200
+
201
+ http://www.apache.org/licenses/LICENSE-2.0
202
+
203
+ Unless required by applicable law or agreed to in writing, software
204
+ distributed under the License is distributed on an "AS IS" BASIS,
205
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
206
+ See the License for the specific language governing permissions and
207
+ limitations under the License.
208
+ License-File: LICENSE-2.0.txt
209
+ Classifier: Programming Language :: Python :: 3
210
+ Requires-Python: >=3.12
211
+ Requires-Dist: ipykernel>=7.1.0
212
+ Requires-Dist: jupyter-client>=8.6.3
213
+ Description-Content-Type: text/markdown
214
+
215
+ # Silik Kernel
216
+
217
+ This is a jupyter kernel that allows to interface with multiple kernels, you can:
218
+
219
+ - start, stop and restart kernels,
220
+
221
+ - switch between kernels,
222
+
223
+ - list available kernels.
224
+
225
+ As a jupyter kernel, it takes text as input, transfer it to appropriate sub-kernel; and returns the result in a cell output. It gives a **single context** that is shared between kernels. The cell history is shared with sub-kernels within the 'metadata' attribute of execution messages.
226
+
227
+ > **Any kernel can be plugged to silik**
228
+
229
+ ![](silik_console.png)
230
+
231
+ ## Getting started
232
+
233
+ ```bash
234
+ pip install silik-kernel
235
+ ```
236
+
237
+ The kernel is then installed on the current python venv.
238
+
239
+ Any jupyter frontend should be able to access the kernel, for example :
240
+
241
+ • **Notebook** (you might need to restart the IDE) : select 'silik' on top right of the notebook
242
+
243
+ • **CLI** : Install jupyter-console (`pip install jupyter-console`); and run `jupyter console --kernel silik`
244
+
245
+ • **Silik Signal Messaging** : Access the kernel through Signal Message Application.
246
+
247
+ To use diverse kernels through silik, you can install some example kernels (on the same python venv): [chatbot](./chatbot_kernel/), [rudi](./rudi_kernel/). You can also create new agent-based kernel by subclassing [pydantic-ai base kernel](./pydantic_ai_base_kernel/).
248
+
249
+ > You can list the available kernels by running `jupyter kernelspec list` in a terminal.
250
+
251
+ ## Usage
252
+
253
+ Once the kernel is started, you can :
254
+
255
+ - send commands :
256
+ - `!start <kernel_type>` : starts a kernel; it will be assigned a label. Per example, `!start python3` starts and connect to a python3 kernel.
257
+ - `!restart <kernel_label>` : restart a kernel with its label.
258
+ - `!ls` : list started kernels.
259
+ - `!select <kernel_label>`: switch a started kernel with its label
260
+
261
+ - run code :
262
+ - if you run `!ls`, you'll see which kernel you are on.
263
+ - all cells you send will be executed in this kernel, and the result will be given in the cell output. Silik kernel acts as a gateway for the sub-kernels.
264
+
265
+ ## Retrieving cells history with a custom kernel
266
+
267
+ If you want to retrieve the history of the silik kernel within your custom kernel, you just have to access the 'metadata' attribute of the current message. For example (assuming you use subclass the ipykernel, and self is your kernel instance) :
268
+
269
+ ```python
270
+ parent = self.get_parent()
271
+ metadata = parent.get("metadata", {})
272
+ if isinstance(metadata, dict) and "message_history" in metadata:
273
+ print(metadata["message_history"])
274
+ ```
275
+
276
+ The attribute 'message_history' of the metadata is a list of dict, each with :
277
+
278
+ ```python
279
+ {
280
+ "role": "user or assistant; user for cell input, assistant for cell output",
281
+ "content": "Input Code if user, output if assistant",
282
+ "uid": "uuidv4"
283
+ }
284
+ ```
@@ -0,0 +1,70 @@
1
+ # Silik Kernel
2
+
3
+ This is a jupyter kernel that allows to interface with multiple kernels, you can:
4
+
5
+ - start, stop and restart kernels,
6
+
7
+ - switch between kernels,
8
+
9
+ - list available kernels.
10
+
11
+ As a jupyter kernel, it takes text as input, transfer it to appropriate sub-kernel; and returns the result in a cell output. It gives a **single context** that is shared between kernels. The cell history is shared with sub-kernels within the 'metadata' attribute of execution messages.
12
+
13
+ > **Any kernel can be plugged to silik**
14
+
15
+ ![](silik_console.png)
16
+
17
+ ## Getting started
18
+
19
+ ```bash
20
+ pip install silik-kernel
21
+ ```
22
+
23
+ The kernel is then installed on the current python venv.
24
+
25
+ Any jupyter frontend should be able to access the kernel, for example :
26
+
27
+ • **Notebook** (you might need to restart the IDE) : select 'silik' on top right of the notebook
28
+
29
+ • **CLI** : Install jupyter-console (`pip install jupyter-console`); and run `jupyter console --kernel silik`
30
+
31
+ • **Silik Signal Messaging** : Access the kernel through Signal Message Application.
32
+
33
+ To use diverse kernels through silik, you can install some example kernels (on the same python venv): [chatbot](./chatbot_kernel/), [rudi](./rudi_kernel/). You can also create new agent-based kernel by subclassing [pydantic-ai base kernel](./pydantic_ai_base_kernel/).
34
+
35
+ > You can list the available kernels by running `jupyter kernelspec list` in a terminal.
36
+
37
+ ## Usage
38
+
39
+ Once the kernel is started, you can :
40
+
41
+ - send commands :
42
+ - `!start <kernel_type>` : starts a kernel; it will be assigned a label. Per example, `!start python3` starts and connect to a python3 kernel.
43
+ - `!restart <kernel_label>` : restart a kernel with its label.
44
+ - `!ls` : list started kernels.
45
+ - `!select <kernel_label>`: switch a started kernel with its label
46
+
47
+ - run code :
48
+ - if you run `!ls`, you'll see which kernel you are on.
49
+ - all cells you send will be executed in this kernel, and the result will be given in the cell output. Silik kernel acts as a gateway for the sub-kernels.
50
+
51
+ ## Retrieving cells history with a custom kernel
52
+
53
+ If you want to retrieve the history of the silik kernel within your custom kernel, you just have to access the 'metadata' attribute of the current message. For example (assuming you use subclass the ipykernel, and self is your kernel instance) :
54
+
55
+ ```python
56
+ parent = self.get_parent()
57
+ metadata = parent.get("metadata", {})
58
+ if isinstance(metadata, dict) and "message_history" in metadata:
59
+ print(metadata["message_history"])
60
+ ```
61
+
62
+ The attribute 'message_history' of the metadata is a list of dict, each with :
63
+
64
+ ```python
65
+ {
66
+ "role": "user or assistant; user for cell input, assistant for cell output",
67
+ "content": "Input Code if user, output if assistant",
68
+ "uid": "uuidv4"
69
+ }
70
+ ```
@@ -0,0 +1,43 @@
1
+ import os
2
+ import sys
3
+ from hatchling.builders.hooks.plugin.interface import BuildHookInterface
4
+
5
+
6
+ import json
7
+ import shutil
8
+
9
+ from jupyter_client.kernelspec import KernelSpecManager
10
+ from tempfile import TemporaryDirectory
11
+
12
+ # use 'python' executable for portable wheels
13
+ #
14
+ kernel_json = {
15
+ "argv": ["python", "-m", "silik", "-f", "{connection_file}"],
16
+ "display_name": "Silik Base Kernel",
17
+ "language": "text",
18
+ }
19
+
20
+
21
+ class CustomHook(BuildHookInterface):
22
+ def initialize(self, version, build_data):
23
+ here = os.path.abspath(os.path.dirname(__file__))
24
+ sys.path.insert(0, here)
25
+ prefix = os.path.join(here, "data_kernelspec")
26
+
27
+ with TemporaryDirectory() as td:
28
+ os.chmod(td, 0o755) # Starts off as 700, not user readable
29
+ with open(os.path.join(td, "kernel.json"), "w") as f:
30
+ json.dump(kernel_json, f, sort_keys=True)
31
+ print("Installing Jupyter kernel spec")
32
+
33
+ # Requires logo files in kernel root directory
34
+ cur_path = os.path.dirname(os.path.realpath(__file__))
35
+ for logo in ["logo-32x32.png", "logo-64x64.png"]:
36
+ try:
37
+ shutil.copy(os.path.join(cur_path, logo), td)
38
+ except FileNotFoundError:
39
+ print("Custom logo files not found. Default logos will be used.")
40
+
41
+ KernelSpecManager().install_kernel_spec(
42
+ td, "silik", user=False, prefix=prefix
43
+ )
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.10.0", "ipykernel"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "silik-kernel"
7
+ dynamic = ["version"]
8
+ description = "Multi-kernel Manager"
9
+ readme = "README.md"
10
+ license = { file = "LICENSE-2.0.txt" }
11
+ authors = [
12
+ { name = "Marius Garénaux-Gruau ", email = "marius.garenaux-gruau@irisa.fr" },
13
+ ]
14
+ classifiers = ["Programming Language :: Python :: 3"]
15
+ requires-python = ">=3.12"
16
+ dependencies = ["ipykernel>=7.1.0", "jupyter-client>=8.6.3"]
17
+
18
+
19
+ # [project.urls]
20
+ # Homepage = "https://github.com/jupyter/echo_kernel"
21
+
22
+ [tool.hatch.version]
23
+ path = "silik_kernel/__init__.py"
24
+
25
+ # Used to call hatch_build.py
26
+ [tool.hatch.build.hooks.custom]
27
+
28
+ [tool.hatch.build.targets.sdist]
29
+ include = ["/silik_kernel"]
30
+
31
+ [tool.hatch.build.targets.wheel.shared-data]
32
+ "data_kernelspec/share" = "share"
@@ -0,0 +1,5 @@
1
+ """Silik Base Kernel : Multi Kernel Interaction"""
2
+
3
+ __version__ = "1.1"
4
+
5
+ from .kernel import SilikBaseKernel # noqa: F401
@@ -0,0 +1,4 @@
1
+ from ipykernel.kernelapp import IPKernelApp
2
+ from . import SilikBaseKernel
3
+
4
+ IPKernelApp.launch_instance(kernel_class=SilikBaseKernel)
@@ -0,0 +1,539 @@
1
+ # Base python dependencies
2
+ import re
3
+ import os
4
+ from dataclasses import dataclass, asdict
5
+ from uuid import uuid4
6
+ import random
7
+ from pathlib import Path
8
+ import logging
9
+
10
+ # External dependencies
11
+ from ipykernel.kernelbase import Kernel
12
+ from jupyter_client.multikernelmanager import AsyncMultiKernelManager
13
+
14
+
15
+ ALL_KERNELS_LABELS = [
16
+ "lion",
17
+ "tiger",
18
+ "bear",
19
+ "wolf",
20
+ "deer",
21
+ "fox",
22
+ "cat",
23
+ "dog",
24
+ "bird",
25
+ "fish",
26
+ "horse",
27
+ "cow",
28
+ "pig",
29
+ "sheep",
30
+ "goat",
31
+ "duck",
32
+ "chicken",
33
+ "turkey",
34
+ "frog",
35
+ "toad",
36
+ "snake",
37
+ "lizard",
38
+ "turtle",
39
+ "rabbit",
40
+ "mouse",
41
+ "rat",
42
+ "bat",
43
+ "frog",
44
+ "fern",
45
+ "ivy",
46
+ "lily",
47
+ "rose",
48
+ "sage",
49
+ "thyme",
50
+ "mint",
51
+ "basil",
52
+ "parsley",
53
+ "daisy",
54
+ "sunflower",
55
+ "violet",
56
+ "poppy",
57
+ "orchid",
58
+ "jasmine",
59
+ "heather",
60
+ "lavender",
61
+ "geranium",
62
+ "begonia",
63
+ "camellia",
64
+ "azalea",
65
+ "gardenia",
66
+ "hibiscus",
67
+ "magnolia",
68
+ "petunia",
69
+ "primrose",
70
+ "snapdragon",
71
+ "sweetpea",
72
+ "tulip",
73
+ "veronica",
74
+ "oak",
75
+ "pine",
76
+ "maple",
77
+ "elm",
78
+ "ash",
79
+ "beech",
80
+ "birch",
81
+ "cherry",
82
+ "cypress",
83
+ "ebony",
84
+ "fir",
85
+ "hazel",
86
+ "juniper",
87
+ "larch",
88
+ "mahogany",
89
+ "mulberry",
90
+ "olive",
91
+ "palm",
92
+ "pecan",
93
+ "poplar",
94
+ "quaking",
95
+ "redwood",
96
+ "spruce",
97
+ "sycamore",
98
+ "teak",
99
+ "walnut",
100
+ "willow",
101
+ "yew",
102
+ "bee",
103
+ "ant",
104
+ "fly",
105
+ "moth",
106
+ "wasp",
107
+ "bug",
108
+ "flea",
109
+ "tick",
110
+ "mite",
111
+ "louse",
112
+ "roach",
113
+ "cricket",
114
+ "grasshopper",
115
+ "cicada",
116
+ "katydid",
117
+ "earwig",
118
+ "silverfish",
119
+ "spider",
120
+ "scorpion",
121
+ "centipede",
122
+ "millipede",
123
+ "dragonfly",
124
+ "damselfly",
125
+ "butterfly",
126
+ "ladybug",
127
+ "firefly",
128
+ "stick",
129
+ "aphid",
130
+ "termite",
131
+ "beet",
132
+ "carrot",
133
+ "potato",
134
+ "onion",
135
+ "garlic",
136
+ "leek",
137
+ "radish",
138
+ "turnip",
139
+ "parsnip",
140
+ "celery",
141
+ "kale",
142
+ "spinach",
143
+ "peas",
144
+ "cucumber",
145
+ "eggplant",
146
+ "okra",
147
+ "squash",
148
+ "pumpkin",
149
+ "corn",
150
+ "broccoli",
151
+ "cauliflower",
152
+ "mushroom",
153
+ "asparagus",
154
+ "fennel",
155
+ "rhubarb",
156
+ "zucchini",
157
+ "yam",
158
+ "ginger",
159
+ "horseradish",
160
+ "apple",
161
+ "banana",
162
+ "cherry",
163
+ "date",
164
+ "elderberry",
165
+ "fig",
166
+ "grape",
167
+ "guava",
168
+ "jackfruit",
169
+ "kiwi",
170
+ "lemon",
171
+ "mango",
172
+ "melon",
173
+ "nectarine",
174
+ "orange",
175
+ "papaya",
176
+ "pear",
177
+ "pineapple",
178
+ "pomegranate",
179
+ "quince",
180
+ "raspberry",
181
+ "strawberry",
182
+ "tangerine",
183
+ "ugli",
184
+ "victoria",
185
+ "watermelon",
186
+ "xigua",
187
+ "yuzu",
188
+ "ziziphus",
189
+ ]
190
+
191
+
192
+ def setup_kernel_logger(name, log_dir="~/.silik_logs"):
193
+ """
194
+ Creates a logger for the kernel. Set up SILIK_KERNEL_LOG environment
195
+ variable to True before running the kernel, and create the following
196
+ dir : ~/.silik_logs
197
+ """
198
+ log_dir = Path(log_dir).expanduser()
199
+ if not os.path.isdir(log_dir):
200
+ raise Exception(f"Please create a dir for kernel logs at {log_dir}")
201
+
202
+ logger = logging.getLogger(name)
203
+ logger.setLevel(logging.DEBUG)
204
+ logger.propagate = False
205
+
206
+ if not logger.handlers:
207
+ fh = logging.FileHandler(log_dir / f"{name}.log", encoding="utf-8")
208
+ fmt = logging.Formatter("%(asctime)s | %(levelname)s | %(name)s | %(message)s")
209
+ fh.setFormatter(fmt)
210
+ logger.addHandler(fh)
211
+
212
+ return logger
213
+
214
+
215
+ @dataclass
216
+ class KernelMetadata:
217
+ """
218
+ Custom dataclass used to describe kernels
219
+ """
220
+
221
+ label: str
222
+ type: str
223
+ id: str
224
+
225
+
226
+ class SilikBaseKernel(Kernel):
227
+ """
228
+ Base Kernel for Silik, is used as a gateway to distribute
229
+ code cells towards different kernels, e.g. :
230
+
231
+ - octave
232
+ - pydantic ai agent based kernel
233
+ - python
234
+ - ...
235
+
236
+ See https://github.com/Tariqve/jupyter-kernels for available
237
+ kernels.
238
+ """
239
+
240
+ implementation = "Silik"
241
+ implementation_version = "1.0"
242
+ language = "no-op"
243
+ language_version = "0.1"
244
+ language_info = {
245
+ "name": "silik",
246
+ "mimetype": "text/plain",
247
+ "file_extension": ".txt",
248
+ }
249
+ banner = "Silik Kernel - Multikernel Manager - Send !help for commands"
250
+ active_kernel: KernelMetadata | None = None
251
+ all_kernels: list[KernelMetadata] = []
252
+ all_kernels_labels: list[str] = ALL_KERNELS_LABELS
253
+ mkm: AsyncMultiKernelManager = AsyncMultiKernelManager()
254
+ message_history: list[dict] = []
255
+
256
+ def __init__(self, **kwargs):
257
+ super().__init__(**kwargs)
258
+
259
+ should_custom_log = os.environ.get("SILIK_KERNEL_LOG", "False")
260
+ should_custom_log = (
261
+ True if should_custom_log in ["True", "true", "1"] else False
262
+ )
263
+
264
+ if should_custom_log:
265
+ logger = setup_kernel_logger(__name__)
266
+ logger.debug("Started kernel and initalized logger")
267
+ self.logger = logger
268
+ else:
269
+ self.logger = self.log
270
+
271
+ def get_kernel_with_id(self, kernel_id: str) -> KernelMetadata | None:
272
+ """
273
+ Returns a kernel metadata from its id.
274
+
275
+ Parameters :
276
+ ---
277
+ - kernel_id : the uuidv4 (str) of the wanted kernel
278
+
279
+ Returns :
280
+ ---
281
+ The matching kernel metadata if found, else None.
282
+ """
283
+ for each_kernel in self.all_kernels:
284
+ if each_kernel.id == kernel_id:
285
+ return each_kernel
286
+
287
+ def get_kernel_with_label(self, kernel_label: str) -> KernelMetadata | None:
288
+ """
289
+ Returns a kernel metadata from its label.
290
+
291
+ Parameters :
292
+ ---
293
+ - kernel_label : the label (str) of the wanted kernel
294
+
295
+ Returns :
296
+ ---
297
+ The matching kernel metadata if found, else None.
298
+ """
299
+ for each_kernel in self.all_kernels:
300
+ if each_kernel.label == kernel_label:
301
+ return each_kernel
302
+
303
+ async def _do_execute(
304
+ self, code, silent, store_history, user_expressions, allow_stdin
305
+ ):
306
+ """
307
+ Custom do_execute function that :
308
+
309
+ - retrieve the uuid of the living kernel
310
+ - pass the code to it, and displays its result
311
+ """
312
+ self.logger.debug(f"Active kernel : {self.active_kernel}")
313
+ if self.active_kernel is None:
314
+ self.send_response(
315
+ self.iopub_socket,
316
+ "display_data",
317
+ {
318
+ "data": {
319
+ "text/plain": "Please start a kernel. Run '!start <kernel_type>'"
320
+ },
321
+ "metadata": {},
322
+ },
323
+ )
324
+ return {
325
+ "status": "ok",
326
+ "execution_count": self.execution_count,
327
+ "payload": [],
328
+ "user_expressions": {},
329
+ }
330
+
331
+ km = self.mkm.get_kernel(self.active_kernel.id)
332
+ kc = km.client()
333
+
334
+ # synchronous call
335
+ kc.start_channels()
336
+
337
+ # msg_id = kc.execute(code)
338
+ content = {
339
+ "code": code,
340
+ "silent": silent,
341
+ "store_history": store_history,
342
+ "user_expressions": user_expressions,
343
+ "allow_stdin": allow_stdin,
344
+ "stop_on_error": True,
345
+ }
346
+ msg = kc.session.msg(
347
+ "execute_request",
348
+ content,
349
+ metadata={"message_history": self.message_history},
350
+ )
351
+ kc.shell_channel.send(msg)
352
+ msg_id = msg["header"]["msg_id"]
353
+ output = []
354
+
355
+ while True:
356
+ msg = await kc._async_get_iopub_msg()
357
+ if msg["parent_header"].get("msg_id") != msg_id:
358
+ continue
359
+
360
+ msg_type = msg["msg_type"]
361
+
362
+ if msg_type == "stream":
363
+ output.append(msg["content"]["text"])
364
+
365
+ elif msg_type == "execute_result":
366
+ output.append(msg["content"]["data"]["text/plain"])
367
+
368
+ elif msg_type == "error":
369
+ output.append("\n".join(msg["content"]["traceback"]))
370
+ self.logger.debug(f"Error : {msg}")
371
+ return {
372
+ "status": "error",
373
+ "ename": msg["content"]["ename"],
374
+ "evalue": msg["content"]["evalue"],
375
+ "traceback": msg["content"]["traceback"],
376
+ }
377
+ elif msg_type == "status" and msg["content"]["execution_state"] == "idle":
378
+ break
379
+
380
+ # synchronous call
381
+ kc.stop_channels()
382
+
383
+ if not silent and output:
384
+ self.send_response(
385
+ self.iopub_socket,
386
+ "display_data",
387
+ {
388
+ "data": {"text/plain": output[0]},
389
+ "metadata": {},
390
+ },
391
+ )
392
+
393
+ self.message_history.append(
394
+ {"role": "user", "content": code, "uid": str(uuid4())}
395
+ )
396
+ self.message_history.append(
397
+ {"role": "assistant", "content": output[0], "uid": str(uuid4())}
398
+ )
399
+
400
+ return {
401
+ "status": "ok",
402
+ "execution_count": self.execution_count,
403
+ "payload": [],
404
+ "user_expressions": {},
405
+ }
406
+
407
+ def select_kernel(self, kernel_label: str) -> KernelMetadata | None:
408
+ """
409
+ Moves the selected kernel to kernel_label.
410
+ """
411
+ found_kernel = self.get_kernel_with_label(kernel_label)
412
+ if found_kernel is not None:
413
+ self.active_kernel = found_kernel
414
+ return found_kernel
415
+
416
+ async def start_kernel(self, kernel_name: str) -> KernelMetadata | None:
417
+ """
418
+ Finds a living kernel and returns its ID. If it does not exists,
419
+ starts it.
420
+
421
+ Returns :
422
+ ---
423
+ KernelMetadata or None
424
+ """
425
+ self.logger.debug(f"Starting new kernel of type : {kernel_name}")
426
+ kernel_id = await self.mkm.start_kernel(kernel_name=kernel_name)
427
+ kernel_label = random.choice(self.all_kernels_labels)
428
+ new_kernel = KernelMetadata(label=kernel_label, type=kernel_name, id=kernel_id)
429
+ self.active_kernel = new_kernel
430
+ self.logger.debug(f"Successfully started kernel {new_kernel}")
431
+ self.all_kernels.append(new_kernel)
432
+ self.logger.debug(f"No kernel with label {kernel_name} is available.")
433
+ return new_kernel
434
+
435
+ async def do_execute( # pyright: ignore
436
+ self,
437
+ code: str,
438
+ silent,
439
+ store_history=True,
440
+ user_expressions=None,
441
+ allow_stdin=False,
442
+ ):
443
+ try:
444
+ if not silent:
445
+ out = self.parse_command(code)
446
+ self.logger.debug(f"Parsed {out} from {code}")
447
+ if out is None:
448
+ result = await self._do_execute(
449
+ code, silent, store_history, user_expressions, allow_stdin
450
+ )
451
+ return result
452
+ cmd, arg = out
453
+ match cmd:
454
+ case "start":
455
+ found_kernel = await self.start_kernel(arg)
456
+ if found_kernel is None:
457
+ content = f"Could not create kernel with name {arg}"
458
+ else:
459
+ content = f"Connected to kernel {found_kernel}. Now, cells are executed on this kernel."
460
+ case "select":
461
+ found_kernel = self.select_kernel(arg)
462
+ if found_kernel is None:
463
+ content = f"Could not find kernel with name {arg}"
464
+ else:
465
+ content = f"Connected to kernel {found_kernel}. Now, cells are executed on this kernel."
466
+ case "restart":
467
+ found_kernel = self.get_kernel_with_label(arg)
468
+ if found_kernel is None:
469
+ content = f"Could not find kernel with label {arg}"
470
+ else:
471
+ await self.mkm.restart_kernel(found_kernel.id)
472
+ content = f"Restarted kernel {found_kernel}"
473
+ case "ls":
474
+ content = "silik\n"
475
+ for k in range(len(self.all_kernels)):
476
+ knl = self.all_kernels[k]
477
+ label = (
478
+ f">> {knl.label} <<"
479
+ if knl == self.active_kernel
480
+ else knl.label
481
+ )
482
+ dec = "╰── " if k == len(self.all_kernels) - 1 else "├── "
483
+ content += f"{dec}{label} [{knl.type}]\n"
484
+ case "pwd":
485
+ if self.active_kernel is None:
486
+ content = "No kernel is running. Start one with `!start <kernel_name>`."
487
+ else:
488
+ content = asdict(self.active_kernel)
489
+ case "help":
490
+ content = "• !ls : prints living kernels\n• !start <kernel_type> : starts a kernel\n• !restart <kernel_label> : restart a kernel with its label\n• !select <kernel_label> : moves the selected kernel to the one with this label - nexts cells will be executed on this kernel"
491
+ case _:
492
+ content = f"Unknown command {cmd}."
493
+
494
+ self.send_response(
495
+ self.iopub_socket,
496
+ "execute_result",
497
+ {
498
+ "execution_count": self.execution_count,
499
+ "data": {"text/plain": content},
500
+ "metadata": {},
501
+ },
502
+ )
503
+ except Exception as e:
504
+ # TODO : send to error socket
505
+ content = str(e)
506
+ self.send_response(
507
+ self.iopub_socket,
508
+ "execute_result",
509
+ {
510
+ "execution_count": self.execution_count,
511
+ "data": {"text/plain": content},
512
+ "metadata": {},
513
+ },
514
+ )
515
+
516
+ return {
517
+ "status": "ok",
518
+ "execution_count": self.execution_count,
519
+ "payload": [],
520
+ "user_expressions": {},
521
+ }
522
+
523
+ def parse_command(self, cell_input: str):
524
+ """
525
+ Parses the text to find a command. A command
526
+ must start with !.
527
+ """
528
+ matched = re.match(r"^!(\w+)(?: ([\w.]+))?$", cell_input)
529
+ try:
530
+ cmd_name = matched.group(1) # Extract the command name # pyright: ignore
531
+ argument = matched.group(2) # Extract the argument # pyright: ignore
532
+ return cmd_name, argument
533
+ except Exception as e:
534
+ self.logger.debug(f"Could not parse info on {cell_input}, because {e}")
535
+ return
536
+
537
+ def do_shutdown(self, restart):
538
+ self.mkm.shutdown_all()
539
+ return super().do_shutdown(restart)