diff --git a/docs/source/contributors/index.md b/docs/source/contributors/index.md index 6ba6b054..da164dec 100644 --- a/docs/source/contributors/index.md +++ b/docs/source/contributors/index.md @@ -2,6 +2,8 @@ This page is intended for people interested in building new or modified functionality for Jupyter AI. +If you would like to build applications that enhance Jupyter AI, please see the {doc}`developer's guide `. + ## Design principles Maintainers of Jupyter AI have adopted principles that contributors should also follow. These principles, which build on top of [the Zen of Python](https://peps.python.org/pep-0020/), are intended to earn users' trust by keeping their data under their control. The following list is non-exhaustive; maintainers have discretion to interpret and revise these principles. diff --git a/docs/source/developers/index.md b/docs/source/developers/index.md new file mode 100644 index 00000000..123315f7 --- /dev/null +++ b/docs/source/developers/index.md @@ -0,0 +1,18 @@ +# Developers + +The developer documentation is for authors who want to enhance +the functionality of Jupyter AI. + +If you are interested in contributing to Jupyter AI, +please see our {doc}`contributor's guide `. + +## Pydantic compatibility + +Jupyter AI is fully compatible with Python environments using Pydantic v1 +or Pydantic v2. Jupyter AI imports Pydantic classes from the +`langchain.pydantic_v1` module. Developers should do the same when they extend +Jupyter AI classes. + +For more details about using `langchain.pydantic_v1` in an environment with +Pydantic v2 installed, see the +[LangChain documentation on Pydantic compatibility](https://python.langchain.com/docs/guides/pydantic_compatibility). diff --git a/docs/source/index.md b/docs/source/index.md index b0c14106..28d094e9 100644 --- a/docs/source/index.md +++ b/docs/source/index.md @@ -34,4 +34,5 @@ maxdepth: 2 users/index contributors/index +developers/index ``` diff --git a/docs/source/users/index.md b/docs/source/users/index.md index 3b8eba05..d9bc6142 100644 --- a/docs/source/users/index.md +++ b/docs/source/users/index.md @@ -5,6 +5,9 @@ Welcome to the user documentation for Jupyter AI. If you are interested in contributing to Jupyter AI, please see our {doc}`contributor's guide `. +If you would like to build applications that enhance Jupyter AI, +please see the {doc}`developer's guide `. + ## Prerequisites You can run Jupyter AI on any system that can run a supported Python version @@ -45,6 +48,13 @@ does not depend on JupyterLab or `jupyter_ai`. You can install If you have both `jupyter_ai_magics` and `jupyter_ai` installed, you should have the same version of each, to avoid errors. +Jupyter AI internally uses Pydantic v1 and should work with either Pydantic +version 1 or version 2. For compatibility, developers using Pydantic V2 +should import classes using the `pydantic.v1` package. See the +[LangChain Pydantic migration plan](https://python.langchain.com/docs/guides/pydantic_compatibility) +for advice about how developers should use `v1` to avoid mixing v1 and v2 +classes in their code. + ## Installation ### Installation via `pip` diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py index 7ee7334f..7dcda78b 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py @@ -14,7 +14,7 @@ HuggingFaceHubEmbeddings, OpenAIEmbeddings, ) -from pydantic import BaseModel, Extra +from langchain.pydantic_v1 import BaseModel, Extra class BaseEmbeddingsProvider(BaseModel): diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py index cadd41f4..168eece6 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py @@ -2,7 +2,7 @@ from typing import Literal, Optional, get_args import click -from pydantic import BaseModel +from langchain.pydantic_v1 import BaseModel FORMAT_CHOICES_TYPE = Literal[ "code", "html", "image", "json", "markdown", "math", "md", "text" diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py index 865deb0e..8155c2f2 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/providers.py @@ -39,9 +39,9 @@ from langchain.llms.sagemaker_endpoint import LLMContentHandler from langchain.llms.utils import enforce_stop_tokens from langchain.prompts import PromptTemplate +from langchain.pydantic_v1 import BaseModel, Extra, root_validator from langchain.schema import LLMResult from langchain.utils import get_from_dict_or_env -from pydantic import BaseModel, Extra, root_validator class EnvAuthStrategy(BaseModel): diff --git a/packages/jupyter-ai-magics/pyproject.toml b/packages/jupyter-ai-magics/pyproject.toml index 60e23104..bb7a6dc5 100644 --- a/packages/jupyter-ai-magics/pyproject.toml +++ b/packages/jupyter-ai-magics/pyproject.toml @@ -22,7 +22,6 @@ dynamic = ["version", "description", "authors", "urls", "keywords"] dependencies = [ "ipython", - "pydantic~=1.0", "importlib_metadata>=5.2.0", "langchain==0.0.318", "typing_extensions>=4.5.0", diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py index 9b66cfae..ca18becc 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py @@ -13,8 +13,8 @@ from langchain.llms import BaseLLM from langchain.output_parsers import PydanticOutputParser from langchain.prompts import PromptTemplate +from langchain.pydantic_v1 import BaseModel from langchain.schema.output_parser import BaseOutputParser -from pydantic import BaseModel class OutlineSection(BaseModel): diff --git a/packages/jupyter-ai/jupyter_ai/handlers.py b/packages/jupyter-ai/jupyter_ai/handlers.py index fcafa10a..170ae400 100644 --- a/packages/jupyter-ai/jupyter_ai/handlers.py +++ b/packages/jupyter-ai/jupyter_ai/handlers.py @@ -11,7 +11,7 @@ from jupyter_ai.config_manager import ConfigManager, KeyEmptyError, WriteConflictError from jupyter_server.base.handlers import APIHandler as BaseAPIHandler from jupyter_server.base.handlers import JupyterHandler -from pydantic import ValidationError +from langchain.pydantic_v1 import ValidationError from tornado import web, websocket from tornado.web import HTTPError diff --git a/packages/jupyter-ai/jupyter_ai/models.py b/packages/jupyter-ai/jupyter_ai/models.py index efe50192..41509a74 100644 --- a/packages/jupyter-ai/jupyter_ai/models.py +++ b/packages/jupyter-ai/jupyter_ai/models.py @@ -1,7 +1,7 @@ from typing import Any, Dict, List, Literal, Optional, Union from jupyter_ai_magics.providers import AuthStrategy, Field -from pydantic import BaseModel, validator +from langchain.pydantic_v1 import BaseModel, validator DEFAULT_CHUNK_SIZE = 2000 DEFAULT_CHUNK_OVERLAP = 100 diff --git a/packages/jupyter-ai/jupyter_ai/tests/test_config_manager.py b/packages/jupyter-ai/jupyter_ai/tests/test_config_manager.py index 8fa59ab6..dca79393 100644 --- a/packages/jupyter-ai/jupyter_ai/tests/test_config_manager.py +++ b/packages/jupyter-ai/jupyter_ai/tests/test_config_manager.py @@ -12,7 +12,7 @@ ) from jupyter_ai.models import DescribeConfigResponse, GlobalConfig, UpdateConfigRequest from jupyter_ai_magics.utils import get_em_providers, get_lm_providers -from pydantic import ValidationError +from langchain.pydantic_v1 import ValidationError @pytest.fixture diff --git a/packages/jupyter-ai/pyproject.toml b/packages/jupyter-ai/pyproject.toml index 29177570..6c150eba 100644 --- a/packages/jupyter-ai/pyproject.toml +++ b/packages/jupyter-ai/pyproject.toml @@ -24,7 +24,6 @@ classifiers = [ dependencies = [ "jupyter_server>=1.6,<3", "jupyterlab>=3.5,<4", - "pydantic~=1.0", "openai~=0.26", "aiosqlite>=0.18", "importlib_metadata>=5.2.0",