Skip to content

Commit da53ae7

Browse files
committed
initial upgrade to langchain~=0.3, pydantic~=2.0
1 parent 29adef7 commit da53ae7

File tree

16 files changed

+43
-80
lines changed

16 files changed

+43
-80
lines changed

packages/jupyter-ai-magics/jupyter_ai_magics/embedding_providers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Field,
77
MultiEnvAuthStrategy,
88
)
9-
from langchain.pydantic_v1 import BaseModel, Extra
9+
from pydantic import BaseModel, Extra
1010
from langchain_community.embeddings import (
1111
GPT4AllEmbeddings,
1212
HuggingFaceHubEmbeddings,

packages/jupyter-ai-magics/jupyter_ai_magics/models/completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Literal, Optional
22

3-
from langchain.pydantic_v1 import BaseModel
3+
from pydantic import BaseModel
44

55

66
class InlineCompletionRequest(BaseModel):

packages/jupyter-ai-magics/jupyter_ai_magics/models/persona.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from langchain.pydantic_v1 import BaseModel
1+
from pydantic import BaseModel
22

33

44
class Persona(BaseModel):

packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Literal, Optional, get_args
33

44
import click
5-
from langchain.pydantic_v1 import BaseModel
5+
from pydantic import BaseModel
66

77
FORMAT_CHOICES_TYPE = Literal[
88
"code", "html", "image", "json", "markdown", "math", "md", "text"

packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/openrouter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from jupyter_ai_magics import BaseProvider
44
from jupyter_ai_magics.providers import EnvAuthStrategy, TextField
5-
from langchain_core.pydantic_v1 import root_validator
5+
from pydantic import model_validator
66
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
77
from langchain_openai import ChatOpenAI
88

@@ -42,7 +42,7 @@ def __init__(self, **kwargs):
4242
**kwargs,
4343
)
4444

45-
@root_validator(pre=False, skip_on_failure=True, allow_reuse=True)
45+
@model_validator(mode="after")
4646
def validate_environment(cls, values: Dict) -> Dict:
4747
"""Validate that api key and python package exists in environment."""
4848
values["openai_api_key"] = convert_to_secret_str(

packages/jupyter-ai-magics/jupyter_ai_magics/providers.py

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
PromptTemplate,
2525
SystemMessagePromptTemplate,
2626
)
27-
from langchain.pydantic_v1 import BaseModel, Extra
27+
from pydantic import BaseModel, ConfigDict
2828
from langchain.schema import LLMResult
2929
from langchain.schema.output_parser import StrOutputParser
3030
from langchain.schema.runnable import Runnable
@@ -33,13 +33,6 @@
3333
from langchain_core.language_models.chat_models import BaseChatModel
3434
from langchain_core.language_models.llms import BaseLLM
3535

36-
# this is necessary because `langchain.pydantic_v1.main` does not include
37-
# `ModelMetaclass`, as it is not listed in `__all__` by the `pydantic.main`
38-
# subpackage.
39-
try:
40-
from pydantic.v1.main import ModelMetaclass
41-
except:
42-
from pydantic.main import ModelMetaclass
4336

4437
from . import completion_utils as completion
4538
from .models.completion import (
@@ -122,7 +115,7 @@ class EnvAuthStrategy(BaseModel):
122115
name: str
123116
"""The name of the environment variable, e.g. `'ANTHROPIC_API_KEY'`."""
124117

125-
keyword_param: Optional[str]
118+
keyword_param: Optional[str] = None
126119
"""
127120
If unset (default), the authentication token is provided as a keyword
128121
argument with the parameter equal to the environment variable name in
@@ -177,51 +170,10 @@ class IntegerField(BaseModel):
177170
Field = Union[TextField, MultilineTextField, IntegerField]
178171

179172

180-
class ProviderMetaclass(ModelMetaclass):
181-
"""
182-
A metaclass that ensures all class attributes defined inline within the
183-
class definition are accessible and included in `Class.__dict__`.
184-
185-
This is necessary because Pydantic drops any ClassVars that are defined as
186-
an instance field by a parent class, even if they are defined inline within
187-
the class definition. We encountered this case when `langchain` added a
188-
`name` attribute to a parent class shared by all `Provider`s, which caused
189-
`Provider.name` to be inaccessible. See #558 for more info.
190-
"""
191-
192-
def __new__(mcs, name, bases, namespace, **kwargs):
193-
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
194-
for key in namespace:
195-
# skip private class attributes
196-
if key.startswith("_"):
197-
continue
198-
# skip class attributes already listed in `cls.__dict__`
199-
if key in cls.__dict__:
200-
continue
201-
202-
setattr(cls, key, namespace[key])
203-
204-
return cls
205-
206-
@property
207-
def server_settings(cls):
208-
return cls._server_settings
209-
210-
@server_settings.setter
211-
def server_settings(cls, value):
212-
if cls._server_settings is not None:
213-
raise AttributeError("'server_settings' attribute was already set")
214-
cls._server_settings = value
215-
216-
_server_settings = None
217-
218-
219-
class BaseProvider(BaseModel, metaclass=ProviderMetaclass):
220-
#
221-
# pydantic config
222-
#
223-
class Config:
224-
extra = Extra.allow
173+
class BaseProvider(BaseModel):
174+
# pydantic v2 model config
175+
# upstream docs: https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.extra
176+
model_config = ConfigDict(extra="allow")
225177

226178
#
227179
# class attrs
@@ -236,15 +188,25 @@ class Config:
236188
"""List of supported models by their IDs. For registry providers, this will
237189
be just ["*"]."""
238190

239-
help: ClassVar[str] = None
191+
help: ClassVar[Optional[str]] = None
240192
"""Text to display in lieu of a model list for a registry provider that does
241193
not provide a list of models."""
242194

243-
model_id_key: ClassVar[str] = ...
244-
"""Kwarg expected by the upstream LangChain provider."""
195+
model_id_key: ClassVar[Optional[str]] = None
196+
"""
197+
Optional field which specifies the key under which `model_id` is passed to
198+
the parent LangChain class.
245199
246-
model_id_label: ClassVar[str] = ""
247-
"""Human-readable label of the model ID."""
200+
If unset, this defaults to "model_id".
201+
"""
202+
203+
model_id_label: ClassVar[Optional[str]] = None
204+
"""
205+
Optional field which sets the label shown in the UI allowing users to
206+
select/type a model ID.
207+
208+
If unset, the label shown in the UI defaults to "Model ID".
209+
"""
248210

249211
pypi_package_deps: ClassVar[List[str]] = []
250212
"""List of PyPi package dependencies."""
@@ -586,7 +548,6 @@ def __init__(self, **kwargs):
586548

587549
id = "gpt4all"
588550
name = "GPT4All"
589-
docs = "https://docs.gpt4all.io/gpt4all_python.html"
590551
models = [
591552
"ggml-gpt4all-j-v1.2-jazzy",
592553
"ggml-gpt4all-j-v1.3-groovy",

packages/jupyter-ai-magics/pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ dynamic = ["version", "description", "authors", "urls", "keywords"]
2424
dependencies = [
2525
"ipython",
2626
"importlib_metadata>=5.2.0",
27-
"langchain>=0.2.17,<0.3.0",
28-
"langchain_community>=0.2.19,<0.3.0",
27+
"langchain>=0.3.0,<0.4.0",
28+
"langchain_community>=0.3.0,<0.4.0",
29+
"pydantic~=2.0",
2930
"typing_extensions>=4.5.0",
3031
"click~=8.0",
3132
"jsonpath-ng>=1.5.3,<2",

packages/jupyter-ai/jupyter_ai/chat_handlers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
)
3737
from jupyter_ai_magics import Persona
3838
from jupyter_ai_magics.providers import BaseProvider
39-
from langchain.pydantic_v1 import BaseModel
39+
from pydantic import BaseModel
4040
from langchain_core.messages import AIMessageChunk
4141
from langchain_core.runnables import Runnable
4242
from langchain_core.runnables.config import RunnableConfig

packages/jupyter-ai/jupyter_ai/chat_handlers/generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from langchain.chains import LLMChain
1414
from langchain.llms import BaseLLM
1515
from langchain.output_parsers import PydanticOutputParser
16-
from langchain.pydantic_v1 import BaseModel
16+
from pydantic import BaseModel
1717
from langchain.schema.output_parser import BaseOutputParser
1818
from langchain_core.prompts import PromptTemplate
1919

packages/jupyter-ai/jupyter_ai/completions/handlers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
InlineCompletionStreamChunk,
1515
)
1616
from jupyter_server.base.handlers import JupyterHandler
17-
from langchain.pydantic_v1 import ValidationError
17+
from pydantic import ValidationError
1818

1919

2020
class BaseInlineCompletionHandler(

0 commit comments

Comments
 (0)