Skip to content

Commit

Permalink
Use pre-commit (jupyterlab#237)
Browse files Browse the repository at this point in the history
* add precommit config

* add pre-commit to Python dev dependency groups

* precommit initial pass

* fix precommit
  • Loading branch information
dlqqq authored Jun 23, 2023
1 parent 1bc264e commit 785b900
Show file tree
Hide file tree
Showing 59 changed files with 1,140 additions and 863 deletions.
58 changes: 58 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: requirements-txt-fixer
- id: check-added-large-files
- id: check-case-conflict
- id: check-toml
exclude: ^packages/jupyter-ai-module-cookiecutter
- id: check-yaml
exclude: ^packages/jupyter-ai-module-cookiecutter
- id: debug-statements
- id: forbid-new-submodules
- id: check-builtin-literals
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]
files: \.py$

- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
[
"flake8-bugbear==20.1.4",
"flake8-logging-format==0.6.0",
"flake8-implicit-str-concat==0.2.0",
]
stages: [manual]

- repo: https://github.com/sirosen/check-jsonschema
rev: 0.23.1
hooks:
- id: check-jsonschema
name: "Check GitHub Workflows"
files: ^\.github/workflows/
types: [yaml]
args: ["--schemafile", "https://json.schemastore.org/github-workflow"]
stages: [manual]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Jupyter AI

Welcome to Jupyter AI, which brings generative AI to Jupyter. Jupyter AI provides a user-friendly
Welcome to Jupyter AI, which brings generative AI to Jupyter. Jupyter AI provides a user-friendly
and powerful way to explore generative AI models in notebooks and improve your productivity
in JupyterLab and the Jupyter Notebook. More specifically, Jupyter AI offers:

Expand Down Expand Up @@ -41,7 +41,7 @@ First, install [conda](https://conda.io/projects/conda/en/latest/user-guide/inst

If you are using an Apple Silicon-based Mac (M1, M1 Pro, M2, etc.), you need to uninstall the `pip` provided version of `grpcio` and install the version provided by `conda` instead.

$ pip uninstall grpcio; conda install grpcio
$ pip uninstall grpcio; conda install grpcio

If you are not using JupyterLab and you only want to install the Jupyter AI `%%ai` magic, skip the `pip install jupyter_ai` step above, and instead, run:

Expand All @@ -59,7 +59,7 @@ Once you have installed the `%%ai` magic, you can enable it in any notebook or t
or:

%load_ext jupyter_ai

The screenshots below are from notebooks in the `examples/` directory of this package.

Then, you can use the `%%ai` magic command to specify a model and natural language prompt:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/_static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ img.screenshot {
/* Copied from div.admonition */
box-shadow: 0 .2rem .5rem var(--pst-color-shadow),0 0 .0625rem var(--pst-color-shadow);
border-color: var(--pst-color-info);
}
}
12 changes: 6 additions & 6 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'Jupyter AI'
copyright = '2023, Project Jupyter'
author = 'Project Jupyter'
project = "Jupyter AI"
copyright = "2023, Project Jupyter"
author = "Project Jupyter"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["myst_parser"]
myst_enable_extensions = ["colon_fence"]

templates_path = ['_templates']
templates_path = ["_templates"]
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "pydata_sphinx_theme"
html_static_path = ['_static']
html_static_path = ["_static"]

html_css_files = [
'css/custom.css',
"css/custom.css",
]

# -- Jupyter theming -------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/contributors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Due to a compatibility issue with Webpack, Node.js 18.15.0 does not work with Ju
:::

## Development install
After you have installed the prerequisites, create a new conda environment and activate it.
After you have installed the prerequisites, create a new conda environment and activate it.

```
conda create -n jupyter-ai python=3.10
Expand Down
4 changes: 2 additions & 2 deletions docs/source/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Jupyter AI

Welcome to Jupyter AI, which brings generative AI to Jupyter. Jupyter AI provides a user-friendly
Welcome to Jupyter AI, which brings generative AI to Jupyter. Jupyter AI provides a user-friendly
and powerful way to explore generative AI models in notebooks and improve your productivity
in JupyterLab and the Jupyter Notebook. More specifically, Jupyter AI offers:

Expand All @@ -19,4 +19,4 @@ maxdepth: 2
users/index
contributors/index
```
```
8 changes: 4 additions & 4 deletions docs/source/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Welcome to the user documentation for Jupyter AI.

If you are interested in contributing to Jupyter AI,
If you are interested in contributing to Jupyter AI,
please see our {doc}`contributor's guide </contributors/index>`.

## Prerequisites
Expand Down Expand Up @@ -101,7 +101,7 @@ First, install [conda](https://conda.io/projects/conda/en/latest/user-guide/inst

If you are using an Apple Silicon-based Mac (M1, M1 Pro, M2, etc.), you need to uninstall the `pip` provided version of `grpcio` and install the version provided by `conda` instead.

$ pip uninstall grpcio; conda install grpcio
$ pip uninstall grpcio; conda install grpcio

If you are not using JupyterLab and you only want to install the Jupyter AI `%%ai` magic, you can run:

Expand All @@ -127,7 +127,7 @@ or

## The chat interface

The easiest way to get started with Jupyter AI is to use the chat interface.
The easiest way to get started with Jupyter AI is to use the chat interface.

:::{attention}
:name: open-ai-privacy-cost
Expand Down Expand Up @@ -449,7 +449,7 @@ will look like properly typeset equations.
Generate the 2D heat equation in LaTeX surrounded by `$$`. Do not include an explanation.
```

This prompt will produce output as a code cell below the input cell.
This prompt will produce output as a code cell below the input cell.

:::{warning}
:name: run-code
Expand Down
23 changes: 13 additions & 10 deletions packages/jupyter-ai-magics/jupyter_ai_magics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
from ._version import __version__

# expose embedding model providers on the package root
from .embedding_providers import (
CohereEmbeddingsProvider,
HfHubEmbeddingsProvider,
OpenAIEmbeddingsProvider,
)
from .exception import store_exception
from .magics import AiMagics

# expose model providers on the package root
from .providers import (
AI21Provider,
AnthropicProvider,
BaseProvider,
ChatOpenAINewProvider,
ChatOpenAIProvider,
CohereProvider,
HfHubProvider,
OpenAIProvider,
ChatOpenAIProvider,
ChatOpenAINewProvider,
SmEndpointProvider
)
# expose embedding model providers on the package root
from .embedding_providers import (
OpenAIEmbeddingsProvider,
CohereEmbeddingsProvider,
HfHubEmbeddingsProvider
SmEndpointProvider,
)
from .providers import BaseProvider


def load_ipython_extension(ipython):
ipython.register_magics(AiMagics)
ipython.set_custom_exc((BaseException,), store_exception)


def unload_ipython_extension(ipython):
ipython.set_custom_exc((BaseException,), ipython.CustomTB)
2 changes: 1 addition & 1 deletion packages/jupyter-ai-magics/jupyter_ai_magics/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"gpt3": "openai:text-davinci-003",
"chatgpt": "openai-chat:gpt-3.5-turbo",
"gpt4": "openai-chat:gpt-4",
}
}
23 changes: 11 additions & 12 deletions packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from typing import ClassVar, List, Type

from jupyter_ai_magics.providers import AuthStrategy, EnvAuthStrategy, Field
from pydantic import BaseModel, Extra
from langchain.embeddings import OpenAIEmbeddings, CohereEmbeddings, HuggingFaceHubEmbeddings
from langchain.embeddings import (
CohereEmbeddings,
HuggingFaceHubEmbeddings,
OpenAIEmbeddings,
)
from langchain.embeddings.base import Embeddings
from pydantic import BaseModel, Extra


class BaseEmbeddingsProvider(BaseModel):
Expand Down Expand Up @@ -33,22 +38,20 @@ class Config:

model_id: str

provider_klass: ClassVar[Type[Embeddings]]
provider_klass: ClassVar[Type[Embeddings]]

registry: ClassVar[bool] = False
"""Whether this provider is a registry provider."""

fields: ClassVar[List[Field]] = []
"""Fields expected by this provider in its constructor. Each `Field` `f`
should be passed as a keyword argument, keyed by `f.key`."""


class OpenAIEmbeddingsProvider(BaseEmbeddingsProvider):
id = "openai"
name = "OpenAI"
models = [
"text-embedding-ada-002"
]
models = ["text-embedding-ada-002"]
model_id_key = "model"
pypi_package_deps = ["openai"]
auth_strategy = EnvAuthStrategy(name="OPENAI_API_KEY")
Expand All @@ -58,11 +61,7 @@ class OpenAIEmbeddingsProvider(BaseEmbeddingsProvider):
class CohereEmbeddingsProvider(BaseEmbeddingsProvider):
id = "cohere"
name = "Cohere"
models = [
'large',
'multilingual-22-12',
'small'
]
models = ["large", "multilingual-22-12", "small"]
model_id_key = "model"
pypi_package_deps = ["cohere"]
auth_strategy = EnvAuthStrategy(name="COHERE_API_KEY")
Expand Down
15 changes: 9 additions & 6 deletions packages/jupyter-ai-magics/jupyter_ai_magics/exception.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from IPython.core.magic import register_line_magic
import traceback

from IPython.core.getipython import get_ipython
from IPython.core.magic import register_line_magic
from IPython.core.ultratb import ListTB

import traceback

def store_exception(shell, etype, evalue, tb, tb_offset=None):
# A structured traceback (a list of strings) or None

if issubclass(etype, SyntaxError):
# Disable ANSI color strings
shell.SyntaxTB.color_toggle()
Expand All @@ -18,7 +19,9 @@ def store_exception(shell, etype, evalue, tb, tb_offset=None):
else:
# Disable ANSI color strings
shell.InteractiveTB.color_toggle()
stb = shell.InteractiveTB.structured_traceback(etype, evalue, tb, tb_offset=tb_offset)
stb = shell.InteractiveTB.structured_traceback(
etype, evalue, tb, tb_offset=tb_offset
)
stb_text = shell.InteractiveTB.stb2text(stb)
# Re-enable ANSI color strings
shell.InteractiveTB.color_toggle()
Expand All @@ -31,6 +34,6 @@ def store_exception(shell, etype, evalue, tb, tb_offset=None):
err = shell.user_ns.get("Err", {})
err[prompt_number] = styled_exception
shell.user_ns["Err"] = err
# Return

# Return
return etraceback
Loading

0 comments on commit 785b900

Please sign in to comment.