agentikernel 0.0.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.
- agentikernel-0.0.1/.gitignore +13 -0
- agentikernel-0.0.1/LICENSE-2.0.txt +202 -0
- agentikernel-0.0.1/PKG-INFO +103 -0
- agentikernel-0.0.1/README.md +89 -0
- agentikernel-0.0.1/agentikernel/__init__.py +6 -0
- agentikernel-0.0.1/agentikernel/__main__.py +5 -0
- agentikernel-0.0.1/agentikernel/kernel.py +366 -0
- agentikernel-0.0.1/hatch_build.py +44 -0
- agentikernel-0.0.1/pyproject.toml +34 -0
|
@@ -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,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentikernel
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Jupyter Kernel containing an agent that can access to other kernels
|
|
5
|
+
Project-URL: Repository, https://github.com/mariusgarenaux/agentikernel
|
|
6
|
+
Author-email: Marius Garénaux Gruau <marius.garenaux-gruau@irisa.fr>
|
|
7
|
+
Maintainer-email: Marius Garénaux Gruau <marius.garenaux-gruau@irisa.fr>
|
|
8
|
+
License-File: LICENSE-2.0.txt
|
|
9
|
+
Keywords: agent,ai,jupyter,kernel,pydantic-ai
|
|
10
|
+
Requires-Python: >=3.13
|
|
11
|
+
Requires-Dist: jupyter-client>=8.8.0
|
|
12
|
+
Requires-Dist: pydantic-ai-kernel>=1.2.6
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Agentikernel
|
|
16
|
+
|
|
17
|
+
> BETA version
|
|
18
|
+
|
|
19
|
+
This is an AI agent, wrapped in a jupyter kernel.
|
|
20
|
+
|
|
21
|
+
It is made to interact with other kernels, through different levels of authorization :
|
|
22
|
+
|
|
23
|
+
- read kernel history,
|
|
24
|
+
|
|
25
|
+
(- send code to kernel with user validation) # not yet implemented
|
|
26
|
+
|
|
27
|
+
- send code to kernel without user validation.
|
|
28
|
+
|
|
29
|
+
This is a subclass of [pydantic-ai-kernel](https://github.com/mariusgarenaux/pydantic-ai-kernel).
|
|
30
|
+
|
|
31
|
+
## Getting Started
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install agentikernel
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then any jupyter front-end could access the kernel, for example :
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install jupyter-console
|
|
41
|
+
jupyter console --kernel agentikernel
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
for command line interface. But a jupyter notebook would also work.
|
|
45
|
+
|
|
46
|
+
## Quick start
|
|
47
|
+
|
|
48
|
+
Create a config file following the scheme [declared here](https://github.com/mariusgarenaux/pydantic-ai-kernel). For example :
|
|
49
|
+
|
|
50
|
+
```yaml
|
|
51
|
+
agent_name: agentik
|
|
52
|
+
system_prompt: You are a specialist in code, always ready to analyze code from jupyter kernels.
|
|
53
|
+
model:
|
|
54
|
+
model_name: qwen3:1.7b
|
|
55
|
+
model_type: openai
|
|
56
|
+
model_provider:
|
|
57
|
+
name: ollama
|
|
58
|
+
params:
|
|
59
|
+
base_url: http://localhost:11434/v1
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
> a permanent config file can be placed in `~/.jupyter/jupyter_agentikernel_config.yaml`.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
Then start the kernel `jupyter console --kernel agentikernel`, and send :
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
/load_config path_to_config_file.yaml
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The kernel is now initialized with an inference provider.
|
|
72
|
+
|
|
73
|
+
In an other window, start any jupyter kernel (default is python) :
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
jupyter console --ConnectionFileMixin.connection_file ./kernel_connection_file_test.json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
> It creates a connection file named `kernel_connection_file_test.json`
|
|
80
|
+
|
|
81
|
+
Within the agentikernel, run :
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
/add_kernel kernel_connection_file_test.json
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Then, the agentikernel can use the python kernel, and you can access the variables, ... from any jupyter frontend connected to the python kernel.
|
|
88
|
+
|
|
89
|
+
## Commands
|
|
90
|
+
|
|
91
|
+
All commands from [pydantic-ai-kernel](https://github.com/mariusgarenaux/pydantic-ai-kernel) are inherited (`/load_config`, ...).
|
|
92
|
+
|
|
93
|
+
We've added the following ones :
|
|
94
|
+
|
|
95
|
+
- `/add_kernel path_to_kernel_connection_file --label tool_label`
|
|
96
|
+
|
|
97
|
+
> Declares a tool to the pydantic-ai agent, that allows it to run code on the kernel. The kernel must already be running. The kernel connection file is created when you start any jupyter kernel, see [jupyter_client](https://jupyter-client.readthedocs.io/en/latest/kernels.html#connection-files). They are stored in a runtime directory, that you can access by runnning `jupyter --paths` [ref](https://docs.jupyter.org/en/stable/use/jupyter-directories.html#runtime-files)
|
|
98
|
+
|
|
99
|
+
> By default, the agent has read-only access to the kernel. But it can be set to write, by specifying : `--mode=write`.
|
|
100
|
+
|
|
101
|
+
- `/remove_kernel tool_label`
|
|
102
|
+
|
|
103
|
+
> Removes the connection with the kernel that has label 'tool_label'. And removes the tool to call it for the agent. The kernel is not stopped, we just undeclare it to the agent. Connection can be made again by running /add_kernel.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Agentikernel
|
|
2
|
+
|
|
3
|
+
> BETA version
|
|
4
|
+
|
|
5
|
+
This is an AI agent, wrapped in a jupyter kernel.
|
|
6
|
+
|
|
7
|
+
It is made to interact with other kernels, through different levels of authorization :
|
|
8
|
+
|
|
9
|
+
- read kernel history,
|
|
10
|
+
|
|
11
|
+
(- send code to kernel with user validation) # not yet implemented
|
|
12
|
+
|
|
13
|
+
- send code to kernel without user validation.
|
|
14
|
+
|
|
15
|
+
This is a subclass of [pydantic-ai-kernel](https://github.com/mariusgarenaux/pydantic-ai-kernel).
|
|
16
|
+
|
|
17
|
+
## Getting Started
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install agentikernel
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then any jupyter front-end could access the kernel, for example :
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install jupyter-console
|
|
27
|
+
jupyter console --kernel agentikernel
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
for command line interface. But a jupyter notebook would also work.
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
Create a config file following the scheme [declared here](https://github.com/mariusgarenaux/pydantic-ai-kernel). For example :
|
|
35
|
+
|
|
36
|
+
```yaml
|
|
37
|
+
agent_name: agentik
|
|
38
|
+
system_prompt: You are a specialist in code, always ready to analyze code from jupyter kernels.
|
|
39
|
+
model:
|
|
40
|
+
model_name: qwen3:1.7b
|
|
41
|
+
model_type: openai
|
|
42
|
+
model_provider:
|
|
43
|
+
name: ollama
|
|
44
|
+
params:
|
|
45
|
+
base_url: http://localhost:11434/v1
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> a permanent config file can be placed in `~/.jupyter/jupyter_agentikernel_config.yaml`.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Then start the kernel `jupyter console --kernel agentikernel`, and send :
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
/load_config path_to_config_file.yaml
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The kernel is now initialized with an inference provider.
|
|
58
|
+
|
|
59
|
+
In an other window, start any jupyter kernel (default is python) :
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
jupyter console --ConnectionFileMixin.connection_file ./kernel_connection_file_test.json
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
> It creates a connection file named `kernel_connection_file_test.json`
|
|
66
|
+
|
|
67
|
+
Within the agentikernel, run :
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
/add_kernel kernel_connection_file_test.json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Then, the agentikernel can use the python kernel, and you can access the variables, ... from any jupyter frontend connected to the python kernel.
|
|
74
|
+
|
|
75
|
+
## Commands
|
|
76
|
+
|
|
77
|
+
All commands from [pydantic-ai-kernel](https://github.com/mariusgarenaux/pydantic-ai-kernel) are inherited (`/load_config`, ...).
|
|
78
|
+
|
|
79
|
+
We've added the following ones :
|
|
80
|
+
|
|
81
|
+
- `/add_kernel path_to_kernel_connection_file --label tool_label`
|
|
82
|
+
|
|
83
|
+
> Declares a tool to the pydantic-ai agent, that allows it to run code on the kernel. The kernel must already be running. The kernel connection file is created when you start any jupyter kernel, see [jupyter_client](https://jupyter-client.readthedocs.io/en/latest/kernels.html#connection-files). They are stored in a runtime directory, that you can access by runnning `jupyter --paths` [ref](https://docs.jupyter.org/en/stable/use/jupyter-directories.html#runtime-files)
|
|
84
|
+
|
|
85
|
+
> By default, the agent has read-only access to the kernel. But it can be set to write, by specifying : `--mode=write`.
|
|
86
|
+
|
|
87
|
+
- `/remove_kernel tool_label`
|
|
88
|
+
|
|
89
|
+
> Removes the connection with the kernel that has label 'tool_label'. And removes the tool to call it for the agent. The kernel is not stopped, we just undeclare it to the agent. Connection can be made again by running /add_kernel.
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
from pydantic_ai_kernel import PydanticAIBaseKernel
|
|
2
|
+
from pydantic_ai_kernel.kernel import Command
|
|
3
|
+
from statikomand import KomandParser
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from jupyter_client.blocking.client import BlockingKernelClient
|
|
7
|
+
from pydantic_ai import Tool, ModelRetry
|
|
8
|
+
from typing import Literal
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
ALL_KERNELS_LABELS: list[str] = [
|
|
13
|
+
"lama",
|
|
14
|
+
"loup",
|
|
15
|
+
"kaki",
|
|
16
|
+
"baba",
|
|
17
|
+
"yack",
|
|
18
|
+
"blob",
|
|
19
|
+
"flan",
|
|
20
|
+
"kiwi",
|
|
21
|
+
"taco",
|
|
22
|
+
"rose",
|
|
23
|
+
"thym",
|
|
24
|
+
"miel",
|
|
25
|
+
"lion",
|
|
26
|
+
"pneu",
|
|
27
|
+
"lune",
|
|
28
|
+
"ciel",
|
|
29
|
+
"coco",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class Kernel:
|
|
35
|
+
label: str
|
|
36
|
+
connection_file: str
|
|
37
|
+
tool: Tool
|
|
38
|
+
kernel_client: BlockingKernelClient
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Agentikernel(PydanticAIBaseKernel):
|
|
42
|
+
|
|
43
|
+
def __init__(self, **kwargs):
|
|
44
|
+
super().__init__(kernel_name="agentikernel", **kwargs)
|
|
45
|
+
|
|
46
|
+
self.add_commands()
|
|
47
|
+
|
|
48
|
+
self.all_kernels: dict[str, Kernel] = {}
|
|
49
|
+
|
|
50
|
+
self.kernel_label_rank = 0
|
|
51
|
+
|
|
52
|
+
def add_commands(self):
|
|
53
|
+
add_kernel_parser = KomandParser(prog="add_kernel")
|
|
54
|
+
add_kernel_parser.add_argument(
|
|
55
|
+
"connection_file", completer=self.add_kernel_cmd_completer
|
|
56
|
+
)
|
|
57
|
+
add_kernel_parser.add_argument("--label", "-l", dest="label")
|
|
58
|
+
add_kernel_parser.add_argument("--mode", "-m", dest="mode")
|
|
59
|
+
add_kernel_cmd = Command(self.add_kernel_cmd_handler, add_kernel_parser)
|
|
60
|
+
self.all_cmds["/add_kernel"] = add_kernel_cmd
|
|
61
|
+
|
|
62
|
+
remove_kernel_parser = KomandParser(prog="remove_kernel")
|
|
63
|
+
remove_kernel_parser.add_argument(
|
|
64
|
+
"label", completer=self.remove_kernel_completer
|
|
65
|
+
)
|
|
66
|
+
remove_kernel_cmd = Command(
|
|
67
|
+
self.remove_kernel_cmd_handler, remove_kernel_parser
|
|
68
|
+
)
|
|
69
|
+
self.all_cmds["/remove_kernel"] = remove_kernel_cmd
|
|
70
|
+
|
|
71
|
+
tools_parser = KomandParser(prog="tools")
|
|
72
|
+
tools_cmd = Command(self.tools_cmd_handler, tools_parser)
|
|
73
|
+
self.all_cmds["/tools"] = tools_cmd
|
|
74
|
+
|
|
75
|
+
def tools_cmd_handler(self, args):
|
|
76
|
+
return [each_tool.name for each_tool in self.tools]
|
|
77
|
+
|
|
78
|
+
def send_code_to_kernel(self, kernel_label: str, code: str) -> str:
|
|
79
|
+
"""
|
|
80
|
+
Executes code on Jupyter Kernel, and returns execution result.
|
|
81
|
+
Used as tool for each new kernel added.
|
|
82
|
+
|
|
83
|
+
Parameters:
|
|
84
|
+
---
|
|
85
|
+
- kernel_label (str): the label of the kernel
|
|
86
|
+
- code (str): the code which will be sent for execution
|
|
87
|
+
to kernel
|
|
88
|
+
|
|
89
|
+
Returns :
|
|
90
|
+
---
|
|
91
|
+
Execution result of the kernel.
|
|
92
|
+
"""
|
|
93
|
+
self.logger.debug(f"Running code : `{code}` on kernel `{kernel_label}`.")
|
|
94
|
+
if kernel_label not in self.all_kernels:
|
|
95
|
+
raise KeyError(f"Unknown key {kernel_label} in all_kernels list.")
|
|
96
|
+
|
|
97
|
+
client = self.all_kernels[kernel_label].kernel_client
|
|
98
|
+
|
|
99
|
+
self.logger.debug(f"Client channels : {client.channels_running}")
|
|
100
|
+
# Start communication channels
|
|
101
|
+
output = None
|
|
102
|
+
# Send execute request
|
|
103
|
+
msg_id = client.execute(code)
|
|
104
|
+
while True:
|
|
105
|
+
msg = client.get_iopub_msg(timeout=5)
|
|
106
|
+
|
|
107
|
+
if msg["parent_header"].get("msg_id") != msg_id:
|
|
108
|
+
continue
|
|
109
|
+
|
|
110
|
+
msg_type = msg["header"]["msg_type"]
|
|
111
|
+
|
|
112
|
+
if msg_type == "stream":
|
|
113
|
+
output = msg["content"]["text"]
|
|
114
|
+
break
|
|
115
|
+
|
|
116
|
+
elif msg_type == "execute_result":
|
|
117
|
+
output = msg["content"]["data"].get("text/plain", "")
|
|
118
|
+
break
|
|
119
|
+
|
|
120
|
+
elif msg_type == "error":
|
|
121
|
+
raise ModelRetry(f"Error when sending code to kernel : {msg}")
|
|
122
|
+
|
|
123
|
+
elif msg_type == "status":
|
|
124
|
+
if msg["content"]["execution_state"] == "idle":
|
|
125
|
+
break
|
|
126
|
+
|
|
127
|
+
if output is None:
|
|
128
|
+
return ""
|
|
129
|
+
return output
|
|
130
|
+
|
|
131
|
+
def add_kernel_cmd_completer(self, word: str, rank: int | None) -> list[str]:
|
|
132
|
+
return self.complete_path(word)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def add_read_kernel_history_tool(self, kernel_info: str, kernel_label: str):
|
|
136
|
+
tool_desc = f"""
|
|
137
|
+
This tools allows you retrieve information from a jupyter kernel.
|
|
138
|
+
You can access to all the code that was executed on the kernel, as well as the output.
|
|
139
|
+
|
|
140
|
+
You can of course propose (if the user asks for it) example of code snippets to answer user's queries.
|
|
141
|
+
|
|
142
|
+
Here is the standardized description of the kernel, including what is the language,
|
|
143
|
+
language version, ... :
|
|
144
|
+
|
|
145
|
+
{json.dumps(kernel_info)}
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
tool_label = f"read_history_of_{kernel_label}"
|
|
149
|
+
tool = Tool.from_schema(
|
|
150
|
+
function=lambda :self.read_kernel_history(kernel_label=kernel_label),
|
|
151
|
+
name=tool_label,
|
|
152
|
+
description=tool_desc,
|
|
153
|
+
json_schema={
|
|
154
|
+
"additionalProperties": False,
|
|
155
|
+
# "properties": {
|
|
156
|
+
# "code": {
|
|
157
|
+
# "description": "the code which will be executed on the kernel",
|
|
158
|
+
# "type": "str",
|
|
159
|
+
# },
|
|
160
|
+
# },
|
|
161
|
+
# "required": ["code"],
|
|
162
|
+
# "type": "object",
|
|
163
|
+
},
|
|
164
|
+
takes_ctx=False,
|
|
165
|
+
)
|
|
166
|
+
self.kernel_label_rank += 1
|
|
167
|
+
return tool
|
|
168
|
+
|
|
169
|
+
def read_kernel_history(self, kernel_label):
|
|
170
|
+
self.logger.debug(f"Accessing history of kernel `{kernel_label}`.")
|
|
171
|
+
if kernel_label not in self.all_kernels:
|
|
172
|
+
raise KeyError(f"Unknown key {kernel_label} in all_kernels list.")
|
|
173
|
+
|
|
174
|
+
client = self.all_kernels[kernel_label].kernel_client
|
|
175
|
+
|
|
176
|
+
self.logger.debug(f"Client channels : {client.channels_running}")
|
|
177
|
+
|
|
178
|
+
# Send history request
|
|
179
|
+
msg_id = client.history(
|
|
180
|
+
raw=True,
|
|
181
|
+
output=True,
|
|
182
|
+
hist_access_type="range",
|
|
183
|
+
session=0,
|
|
184
|
+
start=1,
|
|
185
|
+
stop=1000,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# Wait for reply
|
|
189
|
+
while True:
|
|
190
|
+
msg = client.get_shell_msg()
|
|
191
|
+
if msg["parent_header"].get("msg_id") != msg_id:
|
|
192
|
+
continue
|
|
193
|
+
|
|
194
|
+
if msg["msg_type"] == "history_reply":
|
|
195
|
+
# history = msg["content"]["history"]
|
|
196
|
+
self.logger.debug(f"Kernel history : {msg['content']}")
|
|
197
|
+
msg_history = msg["content"]["history"]
|
|
198
|
+
break
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
string_history = ""
|
|
202
|
+
for session, line, code in msg_history:
|
|
203
|
+
inp, out = code
|
|
204
|
+
string_history += f"In [{line}]: {inp}\n"
|
|
205
|
+
string_history += f"Out[{line}]: {out}\n"
|
|
206
|
+
|
|
207
|
+
return string_history
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def add_run_code_on_kernel_tool(self, kernel_info, kernel_label):
|
|
211
|
+
tool_desc = f"""
|
|
212
|
+
This tools allows you to send code to a jupyter kernel; and retrieve the execution result.
|
|
213
|
+
The only argument you can give to the tool is the raw_code (string) that needs to be sent
|
|
214
|
+
to the kernel.
|
|
215
|
+
|
|
216
|
+
What is inside the code depends on the language of the kernel. For example, it can be python,
|
|
217
|
+
R, octave, ... It can also be just any request in raw text (for example for a kernel that treats
|
|
218
|
+
natural language !)
|
|
219
|
+
|
|
220
|
+
Here is the standardized description of the kernel; to help you construct the code you will send
|
|
221
|
+
to this kernel :
|
|
222
|
+
{json.dumps(kernel_info)}
|
|
223
|
+
"""
|
|
224
|
+
|
|
225
|
+
tool_name = f"send_code_to_{kernel_label}"
|
|
226
|
+
tool = Tool.from_schema(
|
|
227
|
+
function=lambda code: self.send_code_to_kernel(
|
|
228
|
+
kernel_label=kernel_label, code=code
|
|
229
|
+
),
|
|
230
|
+
name=tool_name,
|
|
231
|
+
description=tool_desc,
|
|
232
|
+
json_schema={
|
|
233
|
+
"additionalProperties": False,
|
|
234
|
+
"properties": {
|
|
235
|
+
"code": {
|
|
236
|
+
"description": "the code which will be executed on the kernel",
|
|
237
|
+
"type": "str",
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
"required": ["code"],
|
|
241
|
+
"type": "object",
|
|
242
|
+
},
|
|
243
|
+
takes_ctx=False,
|
|
244
|
+
)
|
|
245
|
+
self.kernel_label_rank += 1
|
|
246
|
+
return tool
|
|
247
|
+
|
|
248
|
+
def add_kernel_cmd_handler(self, args):
|
|
249
|
+
"""
|
|
250
|
+
Adds a kernel to the tool of the agent. Making possible for the agent
|
|
251
|
+
to run code on this kernel, and retrieve the results.
|
|
252
|
+
"""
|
|
253
|
+
if self.agent_config is None:
|
|
254
|
+
raise ValueError(
|
|
255
|
+
"Please load a configuration file for the agent before adding tools. Run /load_config to do so."
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
path = args.connection_file
|
|
259
|
+
|
|
260
|
+
new_kernel_client = BlockingKernelClient(connection_file=path)
|
|
261
|
+
new_kernel_client.load_connection_file()
|
|
262
|
+
self.logger.info(f"Loaded connection file located at {path}")
|
|
263
|
+
|
|
264
|
+
new_kernel_client.start_channels()
|
|
265
|
+
kernel_info = None
|
|
266
|
+
|
|
267
|
+
msg_id = new_kernel_client.kernel_info()
|
|
268
|
+
self.logger.debug(f"Kernel info message id : `{msg_id}`")
|
|
269
|
+
# Send kernel_info_request
|
|
270
|
+
while True:
|
|
271
|
+
msg = new_kernel_client.get_shell_msg(timeout=5)
|
|
272
|
+
self.logger.debug(f"Message from shell socket : `{msg}`")
|
|
273
|
+
|
|
274
|
+
if msg["parent_header"].get("msg_id") != msg_id:
|
|
275
|
+
continue
|
|
276
|
+
|
|
277
|
+
msg_type = msg["header"]["msg_type"]
|
|
278
|
+
|
|
279
|
+
if msg_type == "kernel_info_reply":
|
|
280
|
+
kernel_info = msg["content"]
|
|
281
|
+
break
|
|
282
|
+
if msg_type == "status":
|
|
283
|
+
if msg["content"]["execution_state"] == "idle":
|
|
284
|
+
break
|
|
285
|
+
|
|
286
|
+
self.logger.debug(f"Retrieved kernel informations : `{kernel_info}`")
|
|
287
|
+
if kernel_info is None:
|
|
288
|
+
self.logger.warning(
|
|
289
|
+
f"Could not retrieve information from kernel connection file {path}"
|
|
290
|
+
)
|
|
291
|
+
return
|
|
292
|
+
|
|
293
|
+
kernel_label = args.label
|
|
294
|
+
|
|
295
|
+
if kernel_label is None:
|
|
296
|
+
if self.kernel_label_rank >= len(ALL_KERNELS_LABELS):
|
|
297
|
+
raise Exception("Too much kernel-tools for this agent.")
|
|
298
|
+
|
|
299
|
+
kernel_label = ALL_KERNELS_LABELS[self.kernel_label_rank]
|
|
300
|
+
if args.mode == "write":
|
|
301
|
+
write_tool = self.add_run_code_on_kernel_tool(kernel_info, kernel_label)
|
|
302
|
+
self.tools.append(write_tool)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
read_tool = self.add_read_kernel_history_tool(kernel_info, kernel_label)
|
|
306
|
+
self.tools.append(read_tool)
|
|
307
|
+
|
|
308
|
+
try:
|
|
309
|
+
self.agent = self.create_agent() # updates agent
|
|
310
|
+
except ValueError as e:
|
|
311
|
+
new_kernel_client.stop_channels()
|
|
312
|
+
raise ValueError(
|
|
313
|
+
"Please load a configuration file for the agent before adding tools. Run /load_config to do so."
|
|
314
|
+
) from e
|
|
315
|
+
|
|
316
|
+
self.all_kernels[kernel_label] = Kernel(
|
|
317
|
+
label=kernel_label,
|
|
318
|
+
connection_file=path,
|
|
319
|
+
tool=read_tool,
|
|
320
|
+
kernel_client=new_kernel_client,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
self.logger.info(
|
|
324
|
+
f"Added kernel {kernel_label}."
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
def remove_kernel_cmd_handler(self, args):
|
|
328
|
+
label = args.label
|
|
329
|
+
kernel = self.all_kernels.get(label, None)
|
|
330
|
+
if kernel is None:
|
|
331
|
+
raise KeyError(f"Could not find any kernel with label `{label}`")
|
|
332
|
+
|
|
333
|
+
kernel.kernel_client.stop_channels()
|
|
334
|
+
del self.all_kernels[label]
|
|
335
|
+
|
|
336
|
+
tool_idx = None
|
|
337
|
+
for k, each_tool in enumerate(self.tools):
|
|
338
|
+
if each_tool.name == label:
|
|
339
|
+
tool_idx = k
|
|
340
|
+
|
|
341
|
+
self.logger.debug(f"Tool index : `{tool_idx}`.")
|
|
342
|
+
if tool_idx is not None and 0 <= tool_idx <= len(self.tools) - 1:
|
|
343
|
+
self.logger.debug(f"Deleting tool : `{label}`")
|
|
344
|
+
del self.tools[tool_idx]
|
|
345
|
+
|
|
346
|
+
self.agent = self.create_agent() # reinitializes agent
|
|
347
|
+
self.logger.debug(f"Stopped channel with kernel `{label}`")
|
|
348
|
+
self.logger.debug(f"List of tools : `{self.tools}`")
|
|
349
|
+
|
|
350
|
+
def remove_kernel_completer(self, word: str, rank: int | None):
|
|
351
|
+
all_labels = self.all_kernels.keys()
|
|
352
|
+
all_matches = []
|
|
353
|
+
for each_label in all_labels:
|
|
354
|
+
if len(each_label) < len(word):
|
|
355
|
+
continue
|
|
356
|
+
potential_match = each_label[: len(word)]
|
|
357
|
+
if potential_match == word:
|
|
358
|
+
all_matches.append(each_label)
|
|
359
|
+
return all_matches
|
|
360
|
+
|
|
361
|
+
def do_shutdown(self, restart):
|
|
362
|
+
for each_kernel in self.all_kernels:
|
|
363
|
+
self.all_kernels[each_kernel].kernel_client.stop_channels()
|
|
364
|
+
self.logger.debug(f"Stopped channels for kernel {each_kernel}")
|
|
365
|
+
|
|
366
|
+
return super().do_shutdown(restart)
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
|
|
13
|
+
# use 'python' executable for portable wheels
|
|
14
|
+
#
|
|
15
|
+
kernel_json = {
|
|
16
|
+
"argv": ["python", "-m", "agentikernel", "-f", "{connection_file}"],
|
|
17
|
+
"display_name": "Agentik",
|
|
18
|
+
"language": "text",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CustomHook(BuildHookInterface):
|
|
23
|
+
def initialize(self, version, build_data):
|
|
24
|
+
here = os.path.abspath(os.path.dirname(__file__))
|
|
25
|
+
sys.path.insert(0, here)
|
|
26
|
+
prefix = os.path.join(here, "data_kernelspec")
|
|
27
|
+
|
|
28
|
+
with TemporaryDirectory() as td:
|
|
29
|
+
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
|
30
|
+
with open(os.path.join(td, "kernel.json"), "w") as f:
|
|
31
|
+
json.dump(kernel_json, f, sort_keys=True)
|
|
32
|
+
print("Installing Jupyter kernel spec")
|
|
33
|
+
|
|
34
|
+
# Requires logo files in kernel root directory
|
|
35
|
+
cur_path = os.path.dirname(os.path.realpath(__file__))
|
|
36
|
+
for logo in ["logo-32x32.png", "logo-64x64.png"]:
|
|
37
|
+
try:
|
|
38
|
+
shutil.copy(os.path.join(cur_path, logo), td)
|
|
39
|
+
except FileNotFoundError:
|
|
40
|
+
print("Custom logo files not found. Default logos will be used.")
|
|
41
|
+
|
|
42
|
+
KernelSpecManager().install_kernel_spec(
|
|
43
|
+
td, "agentikernel", user=False, prefix=prefix
|
|
44
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.10.0", "ipykernel", "jupyter-client"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "agentikernel"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Jupyter Kernel containing an agent that can access to other kernels"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.13"
|
|
11
|
+
dependencies = ["jupyter-client>=8.8.0", "pydantic-ai-kernel>=1.2.6"]
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Marius Garénaux Gruau", email = "marius.garenaux-gruau@irisa.fr" },
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{ name = "Marius Garénaux Gruau", email = "marius.garenaux-gruau@irisa.fr" },
|
|
17
|
+
]
|
|
18
|
+
keywords = ["ai", "kernel", "jupyter", "pydantic-ai", "agent"]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Repository = "https://github.com/mariusgarenaux/agentikernel"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
[tool.hatch.version]
|
|
25
|
+
path = "agentikernel/__init__.py"
|
|
26
|
+
|
|
27
|
+
# Used to call hatch_build.py
|
|
28
|
+
[tool.hatch.build.hooks.custom]
|
|
29
|
+
|
|
30
|
+
[tool.hatch.build.targets.sdist]
|
|
31
|
+
include = ["/agentikernel"]
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.wheel.shared-data]
|
|
34
|
+
"data_kernelspec/share" = "share"
|