Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 13357bb

Browse files
authored
Merging to release 0.1.0 (#19)
* Minor improvements (#16) * Starting work on Jupyterlite build of icortex * Changed metadata * Minor * Currently trying to fix jupyter_client import * Kernel resources get installed * Removed JupyterLite work for now because it's not as straightforward as expected * Bumped to version 0.1.0 to conform standards * Migrate prompt parsing to magic commands (#18) * Replaced / and // with magic commands %p and %icortex * Execute generated code with InteractiveShell.run_cell() * Cell outputs are saved to ICortexHistory * Minor
1 parent 35f87fc commit 13357bb

19 files changed

+410
-1034
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,4 @@ lite/jupyterlite-icortex/style/icortex.png
153153
lite/jupyterlite-icortex/LICENSE
154154
node_modules/
155155
*.tsbuildinfo
156+
*_backup

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ ICortex is a [Jupyter kernel](https://jupyter-client.readthedocs.io/en/latest/ke
88

99
https://user-images.githubusercontent.com/2453968/196814906-1a0de2a1-27a7-4aec-a960-0eb21fbe2879.mp4
1010

11+
TODO: Prompts are given using the %prompt magic now, update the video accordingly
12+
1113
It is ...
1214

13-
- a drop-in replacement for the IPython kernel. Prompts start with a forward slash `/`—otherwise the line is treated as regular Python code.
15+
- a drop-in replacement for the IPython kernel. Prompts can be executed with the [magic commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) `%prompt` or `%p` for short.
1416
- an interface for [Natural Language Programming](https://en.wikipedia.org/wiki/Natural-language_programming) interface—prompts written in plain English automatically generate Python code which can then be executed globally.
1517
- interactive—install missing packages directly, decide whether to execute the generated code or not, and so on, directly in the Jupyter Notebook cell.
1618
- open source and fully extensible—if you think we are missing a model or an API, you can request it by creating an issue, or implement it yourself by subclassing `ServiceBase` under [`icortex/services`](icortex/services).
@@ -40,7 +42,7 @@ icortex init
4042
Alternatively, you can initialize directly in a Jupyter Notebook ([instructions on how to start JupyterLab](https://jupyterlab.readthedocs.io/en/stable/getting_started/starting.html)):
4143

4244
```
43-
//init
45+
%icortex init
4446
```
4547

4648
The shell will then instruct you step by step and create a configuration file `icortex.toml` in the current directory.
@@ -65,10 +67,10 @@ You can also try out different services e.g. OpenAI's Codex API, if you have acc
6567

6668
### Executing prompts
6769

68-
To execute a prompt with ICortex, use the `/` character (forward slash, also used to denote division) as a prefix. Copy and paste the following prompt into a cell and try to run it:
70+
To execute a prompt with ICortex, use the `%prompt` [magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html) (or `%p` for short) as a prefix. Copy and paste the following prompt into a cell and try to run it:
6971

7072
```
71-
/print Hello World. Then print the Fibonacci numbers till 100
73+
%p print Hello World. Then print the Fibonacci numbers till 100
7274
```
7375

7476
Depending on the response, you should see an output similar to the following:
@@ -87,7 +89,7 @@ Hello World.
8789
You can also specify variables or options with command line flags, e.g. to auto-install packages, auto-execute the returned code and so on. To see the complete list of variables for your chosen service, run:
8890

8991
```
90-
/help
92+
%help
9193
```
9294

9395
### Using ICortex CLI
@@ -106,7 +108,7 @@ icortex service help
106108

107109
### Accessing ICortex CLI inside Jupyter
108110

109-
You can still access the `icortex` CLI in a Jupyter Notebook or shell by using the prefix `//`. For example running the following in the terminal switches to a local HuggingFace model:
111+
You can still access the `icortex` CLI in a Jupyter Notebook or shell by using the magic command `%icortex`. For example running the following in the terminal switches to a local HuggingFace model:
110112

111113
```
112114
icortex service set huggingface
@@ -115,7 +117,7 @@ icortex service set huggingface
115117
To do the same in a Jupyter Notebook, you can run
116118

117119
```
118-
//service set huggingface
120+
%icortex service set huggingface
119121
```
120122

121123
in a cell, which initializes and switches to the new service directly in your Jupyter session.

docs/source/specification.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
ICortex Specification
3+
=====================
4+
5+
Some stuff

icortex/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from icortex.kernel import ICortexKernel, print_help, get_icortex_kernel
1+
from icortex.kernel import ICortexKernel, print_service_help, get_icortex_kernel
22

3-
# from icortex.cli import set_icortex_service, eval_cli
43
import icortex.services
54
import importlib.metadata
65

icortex/cli.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
import shlex
33
import sys
44
import argparse
5-
from jupyter_console.app import ZMQTerminalIPythonApp
65
from icortex.services import get_available_services
76
from icortex.defaults import DEFAULT_ICORTEX_CONFIG_PATH
8-
from icortex.install import is_kernel_installed, main as install_kernel
7+
from icortex.kernel.install import is_kernel_installed, main as install_kernel
98
from icortex.config import ICortexConfig
109

11-
# Jupyter devs did not make this easy
12-
# TODO: Make less hacky
13-
class ZMQTerminalICortexApp(ZMQTerminalIPythonApp):
14-
def parse_command_line(self, argv=None):
15-
argv = ["--kernel", "icortex"]
16-
super(ZMQTerminalIPythonApp, self).parse_command_line(argv)
17-
self.build_kernel_argv(self.extra_args)
18-
1910

2011
def get_parser(prog=None):
2112
service_names = get_available_services()
@@ -29,7 +20,7 @@ def get_parser(prog=None):
2920
# Initialize ICortex #
3021
######################
3122

32-
# //init
23+
# icortex init
3324
parser_init = subparsers.add_parser(
3425
"init",
3526
help="Initialize ICortex configuration in the current directory",
@@ -51,7 +42,7 @@ def get_parser(prog=None):
5142
# Shell related commands #
5243
##########################
5344

54-
# //shell
45+
# icortex shell
5546
parser_shell = subparsers.add_parser(
5647
"shell",
5748
help="Start ICortex shell",
@@ -62,7 +53,7 @@ def get_parser(prog=None):
6253
# Help #
6354
########
6455

65-
# //help
56+
# icortex help
6657
parser_help = subparsers.add_parser(
6758
"help",
6859
help="Print help",
@@ -73,7 +64,7 @@ def get_parser(prog=None):
7364
# Service related commands #
7465
############################
7566

76-
# //service
67+
# icortex service
7768
parser_service = subparsers.add_parser(
7869
"service",
7970
help="Set and configure code generation services",
@@ -84,7 +75,7 @@ def get_parser(prog=None):
8475
required=True,
8576
)
8677

87-
# //service set <service_name>
78+
# icortex service set <service_name>
8879
parser_service_commands_set = parser_service_commands.add_parser(
8980
"set",
9081
help="Set the service to be used for code generation",
@@ -96,21 +87,21 @@ def get_parser(prog=None):
9687
help="Name of the service to be used for code generation",
9788
)
9889

99-
# //service show <service_name>
90+
# icortex service show <service_name>
10091
parser_service_commands_show = parser_service_commands.add_parser(
10192
"show",
10293
help="Show current service",
10394
add_help=False,
10495
)
10596

106-
# //service help
97+
# icortex service help
10798
parser_service_commands_help = parser_service_commands.add_parser(
10899
"help",
109-
help="Print help for //service",
100+
help="Print help for the current service",
110101
add_help=False,
111102
)
112103

113-
# //service set-var <variable_name> <variable_value>
104+
# icortex service set-var <variable_name> <variable_value>
114105
parser_service_commands_set_var = parser_service_commands.add_parser(
115106
"set-var",
116107
help="Set a variable for the current service",
@@ -126,7 +117,7 @@ def get_parser(prog=None):
126117
help="New value for the variable",
127118
)
128119

129-
# //service init <service_name>
120+
# icortex service init <service_name>
130121
# Used to re-spawn the config dialog if some config for the service
131122
# already exists
132123
parser_service_commands_init = parser_service_commands.add_parser(
@@ -150,7 +141,6 @@ def get_parser(prog=None):
150141

151142

152143
# def set_icortex_service(kernel, config_path=DEFAULT_ICORTEX_CONFIG_PATH):
153-
154144
# if kernel is not None:
155145
# return ICortexConfig(DEFAULT_ICORTEX_CONFIG_PATH).set_service()
156146
# return False
@@ -195,6 +185,7 @@ def main(argv=None, prog=None, kernel=None):
195185
parser.print_help()
196186
elif args.command == "shell" or args.command is None:
197187
from icortex.kernel import get_icortex_kernel
188+
from icortex.kernel.app import ZMQTerminalICortexApp
198189

199190
kernel = get_icortex_kernel()
200191
if kernel is None:
@@ -207,7 +198,7 @@ def main(argv=None, prog=None, kernel=None):
207198
def eval_cli(prompt: str):
208199
argv = shlex.split(prompt)
209200
try:
210-
return main(argv=argv, prog="//")
201+
return main(argv=argv, prog=r"%icortex")
211202
except SystemExit:
212203
return
213204

icortex/context.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
"metadata": {
1111
"kernelspec": {
1212
"display_name": "ICortex (Python 3)",
13-
"language": "icortex",
13+
"language": "python",
1414
"name": "icortex",
1515
},
1616
"language_info": {
17-
"pygments_lexer": "icortex",
18-
# "codemirror_mode": {"name": "ipython", "version": 3},
19-
"file_extension": ".icx",
20-
"mimetype": "text/x-icortex",
17+
"pygments_lexer": "ipython3",
18+
"codemirror_mode": {"name": "ipython", "version": 3},
19+
"file_extension": ".py",
20+
"mimetype": "text/x-python",
2121
"name": "icortex",
22-
# "nbconvert_exporter": "python",
23-
"pygments_lexer": "icortex",
22+
"nbconvert_exporter": "python",
23+
"pygments_lexer": "ipython3",
2424
"version": icortex_version,
2525
"python_version": platform.python_version(),
2626
},
@@ -48,8 +48,12 @@ def _check_init(self):
4848

4949
self._dict = self.scope[DEFAULT_HISTORY_VAR]
5050

51-
def get_dict(self):
52-
return deepcopy(self._dict)
51+
def get_dict(self, omit_last_cell=False):
52+
ret = deepcopy(self._dict)
53+
if omit_last_cell:
54+
if len(ret["cells"]) > 0:
55+
del ret["cells"][-1]
56+
return ret
5357

5458
def add_code(self, code: str, outputs: t.List[t.Any]):
5559
self._check_init()

icortex/helper.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@ def unescape(s) -> str:
88

99

1010
def is_prompt(input: str) -> bool:
11-
return input.strip()[0] == "/"
11+
return input.strip().split()[0] in [
12+
r"%p",
13+
r"%prompt",
14+
r"%%prompt",
15+
r"%%prompt",
16+
]
1217

1318

1419
def is_cli(input: str) -> bool:
15-
return input.strip()[:2] == "//"
20+
return input.strip().split()[0] == r"%icortex"
1621

1722

1823
def escape_quotes(s: str) -> str:
1924
return s.replace('"', r"\"").replace("'", r"\'")
2025

2126

2227
def extract_prompt(input: str) -> str:
23-
return input.strip()[1:].strip()
28+
tokens = input.strip().split(" ", 1)
29+
if len(tokens) == 1:
30+
return ""
31+
else:
32+
return tokens[1]
2433

2534

26-
def extract_cli(input: str) -> str:
27-
return input.strip()[2:].strip()
35+
extract_cli = extract_prompt
2836

2937

3038
def yes_no_input(message: str, default=True) -> bool:

0 commit comments

Comments
 (0)