Make lckwargs private (#6344)

<!--
Thank you for contributing to LangChain! Your PR will appear in our
release under the title you set. Please make sure it highlights your
valuable contribution.

Replace this with a description of the change, the issue it fixes (if
applicable), and relevant context. List any dependencies required for
this change.

After you're done, someone will review your PR. They may suggest
improvements. If no one reviews your PR within a few days, feel free to
@-mention the same people again, as notifications can get lost.

Finally, we'd love to show appreciation for your contribution - if you'd
like us to shout you out on Twitter, please also include your handle!
-->

<!-- Remove if not applicable -->

Fixes # (issue)

#### Before submitting

<!-- If you're adding a new integration, please include:

1. a test for the integration - favor unit tests that does not rely on
network access.
2. an example notebook showing its use


See contribution guidelines for more information on how to write tests,
lint
etc:


https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->

#### Who can review?

Tag maintainers/contributors who might be interested:

<!-- For a quicker response, figure out the right person to tag with @

  @hwchase17 - project lead

  Tracing / Callbacks
  - @agola11

  Async
  - @agola11

  DataLoaders
  - @eyurtsev

  Models
  - @hwchase17
  - @agola11

  Agents / Tools / Toolkits
  - @hwchase17

  VectorStores / Retrievers / Memory
  - @dev2049

 -->
This commit is contained in:
Nuno Campos 2023-06-17 19:08:25 +01:00 committed by GitHub
parent 8cfb52ddbb
commit e194dc5306
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 16 additions and 41 deletions

View File

@ -1,7 +1,5 @@
from typing import Any, Dict, List, Optional
from pydantic import Extra
from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
@ -34,11 +32,6 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon):
model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key")
"""
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@property
def _llm_type(self) -> str:
"""Return type of chat model."""

View File

@ -5,7 +5,7 @@ from abc import ABC, abstractmethod
from functools import partial
from typing import Any, Dict, List, Mapping, Optional, Sequence
from pydantic import Extra, Field, root_validator
from pydantic import Field, root_validator
import langchain
from langchain.base_language import BaseLanguageModel
@ -56,7 +56,6 @@ class BaseChatModel(BaseLanguageModel, ABC):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True
def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict:

View File

@ -15,7 +15,7 @@ from typing import (
Union,
)
from pydantic import Extra, Field, root_validator
from pydantic import Field, root_validator
from tenacity import (
before_sleep_log,
retry,
@ -182,7 +182,6 @@ class ChatOpenAI(BaseChatModel):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.ignore
allow_population_by_field_name = True
@root_validator(pre=True)

View File

@ -3,7 +3,7 @@ import re
import warnings
from typing import Any, Callable, Dict, Generator, List, Mapping, Optional, Tuple, Union
from pydantic import BaseModel, Extra, root_validator
from pydantic import BaseModel, root_validator
from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun,
@ -140,11 +140,6 @@ class Anthropic(LLM, _AnthropicCommon):
)
return values
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@property
def _llm_type(self) -> str:
"""Return type of llm."""

View File

@ -7,7 +7,7 @@ from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
import yaml
from pydantic import Extra, Field, root_validator, validator
from pydantic import Field, root_validator, validator
import langchain
from langchain.base_language import BaseLanguageModel
@ -85,7 +85,6 @@ class BaseLLM(BaseLanguageModel, ABC):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True
@root_validator()

View File

@ -20,7 +20,7 @@ from typing import (
Union,
)
from pydantic import Extra, Field, root_validator
from pydantic import Field, root_validator
from tenacity import (
before_sleep_log,
retry,
@ -187,7 +187,6 @@ class BaseOpenAI(BaseLLM):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.ignore
allow_population_by_field_name = True
@root_validator(pre=True)
@ -686,11 +685,6 @@ class OpenAIChat(BaseLLM):
disallowed_special: Union[Literal["all"], Collection[str]] = "all"
"""Set of special tokens that are not allowed。"""
class Config:
"""Configuration for this pydantic object."""
extra = Extra.ignore
@root_validator(pre=True)
def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""Build extra kwargs from additional params that were passed in."""

View File

@ -1,7 +1,7 @@
from abc import ABC
from typing import Any, Dict, List, Literal, TypedDict, Union, cast
from pydantic import BaseModel, Field
from pydantic import BaseModel, PrivateAttr
class BaseSerialized(TypedDict):
@ -55,11 +55,14 @@ class Serializable(BaseModel, ABC):
"""
return {}
lc_kwargs: Dict[str, Any] = Field(default_factory=dict, exclude=True, repr=False)
class Config:
extra = "ignore"
_lc_kwargs = PrivateAttr(default_factory=dict)
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.lc_kwargs = kwargs
self._lc_kwargs = kwargs
def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
if not self.lc_serializable:
@ -69,8 +72,8 @@ class Serializable(BaseModel, ABC):
# Get latest values for kwargs if there is an attribute with same name
lc_kwargs = {
k: getattr(self, k, v)
for k, v in self.lc_kwargs.items()
if not self.__exclude_fields__.get(k, False) # type: ignore
for k, v in self._lc_kwargs.items()
if not (self.__exclude_fields__ or {}).get(k, False) # type: ignore
}
# Merge the lc_secrets and lc_attributes from every class in the MRO

View File

@ -7,7 +7,7 @@ from pathlib import Path
from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Union
import yaml
from pydantic import Extra, Field, root_validator
from pydantic import Field, root_validator
from langchain.formatting import formatter
from langchain.load.serializable import Serializable
@ -119,7 +119,6 @@ class BasePromptTemplate(Serializable, ABC):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True
@abstractmethod

View File

@ -5,7 +5,7 @@ from pathlib import Path
from string import Formatter
from typing import Any, Dict, List, Union
from pydantic import Extra, root_validator
from pydantic import root_validator
from langchain.prompts.base import (
DEFAULT_FORMATTER_MAPPING,
@ -48,11 +48,6 @@ class PromptTemplate(StringPromptTemplate):
"""Return the prompt type key."""
return "prompt"
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
def format(self, **kwargs: Any) -> str:
"""Format the prompt with the inputs.

View File

@ -16,7 +16,7 @@ from typing import (
)
from uuid import UUID
from pydantic import BaseModel, Extra, Field, root_validator
from pydantic import BaseModel, Field, root_validator
from langchain.load.serializable import Serializable
@ -229,7 +229,6 @@ class BaseMemory(Serializable, ABC):
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True
@property