Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ ICortex is a [Jupyter kernel](https://jupyter-client.readthedocs.io/en/latest/ke

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

TODO: Prompts are given using the %prompt magic now, update the video accordingly

It is ...

- a drop-in replacement for the IPython kernel. Prompts start with a forward slash `/`—otherwise the line is treated as regular Python code.
- 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.
- 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.
- interactive—install missing packages directly, decide whether to execute the generated code or not, and so on, directly in the Jupyter Notebook cell.
- 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).
Expand Down Expand Up @@ -40,7 +42,7 @@ icortex init
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)):

```
//init
%icortex init
```

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

### Executing prompts

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:
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:

```
/print Hello World. Then print the Fibonacci numbers till 100
%p print Hello World. Then print the Fibonacci numbers till 100
```

Depending on the response, you should see an output similar to the following:
Expand All @@ -87,7 +89,7 @@ Hello World.
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:

```
/help
%help
```

### Using ICortex CLI
Expand All @@ -106,7 +108,7 @@ icortex service help

### Accessing ICortex CLI inside Jupyter

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:
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:

```
icortex service set huggingface
Expand All @@ -115,7 +117,7 @@ icortex service set huggingface
To do the same in a Jupyter Notebook, you can run

```
//service set huggingface
%icortex service set huggingface
```

in a cell, which initializes and switches to the new service directly in your Jupyter session.
Expand Down
5 changes: 5 additions & 0 deletions docs/source/specification.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

ICortex Specification
=====================

Some stuff
2 changes: 1 addition & 1 deletion icortex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from icortex.kernel import ICortexKernel, print_help, get_icortex_kernel
from icortex.kernel import ICortexKernel, print_service_help, get_icortex_kernel

import icortex.services
import importlib.metadata
Expand Down
22 changes: 11 additions & 11 deletions icortex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_parser(prog=None):
# Initialize ICortex #
######################

# //init
# icortex init
parser_init = subparsers.add_parser(
"init",
help="Initialize ICortex configuration in the current directory",
Expand All @@ -42,7 +42,7 @@ def get_parser(prog=None):
# Shell related commands #
##########################

# //shell
# icortex shell
parser_shell = subparsers.add_parser(
"shell",
help="Start ICortex shell",
Expand All @@ -53,7 +53,7 @@ def get_parser(prog=None):
# Help #
########

# //help
# icortex help
parser_help = subparsers.add_parser(
"help",
help="Print help",
Expand All @@ -64,7 +64,7 @@ def get_parser(prog=None):
# Service related commands #
############################

# //service
# icortex service
parser_service = subparsers.add_parser(
"service",
help="Set and configure code generation services",
Expand All @@ -75,7 +75,7 @@ def get_parser(prog=None):
required=True,
)

# //service set <service_name>
# icortex service set <service_name>
parser_service_commands_set = parser_service_commands.add_parser(
"set",
help="Set the service to be used for code generation",
Expand All @@ -87,21 +87,21 @@ def get_parser(prog=None):
help="Name of the service to be used for code generation",
)

# //service show <service_name>
# icortex service show <service_name>
parser_service_commands_show = parser_service_commands.add_parser(
"show",
help="Show current service",
add_help=False,
)

# //service help
# icortex service help
parser_service_commands_help = parser_service_commands.add_parser(
"help",
help="Print help for //service",
help="Print help for the current service",
add_help=False,
)

# //service set-var <variable_name> <variable_value>
# icortex service set-var <variable_name> <variable_value>
parser_service_commands_set_var = parser_service_commands.add_parser(
"set-var",
help="Set a variable for the current service",
Expand All @@ -117,7 +117,7 @@ def get_parser(prog=None):
help="New value for the variable",
)

# //service init <service_name>
# icortex service init <service_name>
# Used to re-spawn the config dialog if some config for the service
# already exists
parser_service_commands_init = parser_service_commands.add_parser(
Expand Down Expand Up @@ -198,7 +198,7 @@ def main(argv=None, prog=None, kernel=None):
def eval_cli(prompt: str):
argv = shlex.split(prompt)
try:
return main(argv=argv, prog="//")
return main(argv=argv, prog=r"%icortex")
except SystemExit:
return

Expand Down
8 changes: 6 additions & 2 deletions icortex/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def _check_init(self):

self._dict = self.scope[DEFAULT_HISTORY_VAR]

def get_dict(self):
return deepcopy(self._dict)
def get_dict(self, omit_last_cell=False):
ret = deepcopy(self._dict)
if omit_last_cell:
if len(ret["cells"]) > 0:
del ret["cells"][-1]
return ret

def add_code(self, code: str, outputs: t.List[t.Any]):
self._check_init()
Expand Down
18 changes: 13 additions & 5 deletions icortex/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ def unescape(s) -> str:


def is_prompt(input: str) -> bool:
return input.strip()[0] == "/"
return input.strip().split()[0] in [
r"%p",
r"%prompt",
r"%%prompt",
r"%%prompt",
]


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


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


def extract_prompt(input: str) -> str:
return input.strip()[1:].strip()
tokens = input.strip().split(" ", 1)
if len(tokens) == 1:
return ""
else:
return tokens[1]


def extract_cli(input: str) -> str:
return input.strip()[2:].strip()
extract_cli = extract_prompt


def yes_no_input(message: str, default=True) -> bool:
Expand Down
Loading