Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 10, 2024
1 parent b931852 commit 04c985a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Field,
MultiEnvAuthStrategy,
)
from langchain.pydantic_v1 import BaseModel, Extra
from langchain_community.embeddings import (
BedrockEmbeddings,
CohereEmbeddings,
Expand All @@ -15,7 +16,6 @@
OpenAIEmbeddings,
QianfanEmbeddingsEndpoint,
)
from langchain.pydantic_v1 import BaseModel, Extra


class BaseEmbeddingsProvider(BaseModel):
Expand Down
33 changes: 13 additions & 20 deletions packages/jupyter-ai-magics/jupyter_ai_magics/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@
import io
import json
from concurrent.futures import ThreadPoolExecutor
from typing import (
Any,
ClassVar,
Coroutine,
Dict,
List,
Literal,
Optional,
Union,
)
from typing import Any, ClassVar, Coroutine, Dict, List, Literal, Optional, Union

from jsonpath_ng import parse
from langchain.chat_models.base import BaseChatModel
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 langchain_community.chat_models import (
AzureChatOpenAI,
BedrockChat,
ChatAnthropic,
ChatOpenAI,
QianfanChatEndpoint,
)
from langchain.chat_models.base import BaseChatModel
from langchain_community.llms import (
AI21,
Anthropic,
Expand All @@ -34,13 +32,6 @@
OpenAI,
SagemakerEndpoint,
)
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 langchain_community.chat_models import ChatOpenAI

# this is necessary because `langchain.pydantic_v1.main` does not include
# `ModelMetaclass`, as it is not listed in `__all__` by the `pydantic.main`
Expand Down Expand Up @@ -103,11 +94,12 @@ class IntegerField(BaseModel):

Field = Union[TextField, MultilineTextField, IntegerField]


class ProviderMetaclass(ModelMetaclass):
"""
A metaclass that ensures all class attributes defined inline within the
class definition are accessible and included in `Class.__dict__`.
This is necessary because Pydantic drops any ClassVars that are defined as
an instance field by a parent class, even if they are defined inline within
the class definition. We encountered this case when `langchain` added a
Expand All @@ -119,7 +111,7 @@ def __new__(mcs, name, bases, namespace, **kwargs):
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
for key in namespace:
# skip private class attributes
if key.startswith('_'):
if key.startswith("_"):
continue
# skip class attributes already listed in `cls.__dict__`
if key in cls.__dict__:
Expand All @@ -129,6 +121,7 @@ def __new__(mcs, name, bases, namespace, **kwargs):

return cls


class BaseProvider(BaseModel, metaclass=ProviderMetaclass):
#
# pydantic config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import ClassVar, Optional
from ..providers import ProviderMetaclass

from langchain.pydantic_v1 import BaseModel

from ..providers import ProviderMetaclass


def test_provider_metaclass():
"""
Expand All @@ -14,12 +16,11 @@ def test_provider_metaclass():

class Parent(BaseModel):
test: Optional[str]

class Base(BaseModel):
test: ClassVar[str]

class Child(Base, Parent, metaclass=ProviderMetaclass):
test: ClassVar[str] = "expected"

assert Child.test == "expected"

0 comments on commit 04c985a

Please sign in to comment.