core: docstrings load (#23787)

Added missed docstrings. Formatted docstrings to the consistent form.
This commit is contained in:
Leonid Ganeline 2024-07-05 09:23:19 -07:00 committed by GitHub
parent 6f08e11d7c
commit 77f5fc3d55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 93 additions and 20 deletions

View File

@ -6,7 +6,14 @@ from langchain_core.load.serializable import Serializable, to_json_not_implement
def default(obj: Any) -> Any: def default(obj: Any) -> Any:
"""Return a default value for a Serializable object or """Return a default value for a Serializable object or
a SerializedNotImplemented object.""" a SerializedNotImplemented object.
Args:
obj: The object to serialize to json if it is a Serializable object.
Returns:
A json serializable object or a SerializedNotImplemented object.
"""
if isinstance(obj, Serializable): if isinstance(obj, Serializable):
return obj.to_json() return obj.to_json()
else: else:
@ -17,13 +24,17 @@ def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str:
"""Return a json string representation of an object. """Return a json string representation of an object.
Args: Args:
obj: The object to dump obj: The object to dump.
pretty: Whether to pretty print the json. If true, the json will be pretty: Whether to pretty print the json. If true, the json will be
indented with 2 spaces (if no indent is provided as part of kwargs) indented with 2 spaces (if no indent is provided as part of kwargs).
Default is False.
**kwargs: Additional arguments to pass to json.dumps **kwargs: Additional arguments to pass to json.dumps
Returns: Returns:
A json string representation of the object A json string representation of the object.
Raises:
ValueError: If `default` is passed as a kwarg.
""" """
if "default" in kwargs: if "default" in kwargs:
raise ValueError("`default` should not be passed to dumps") raise ValueError("`default` should not be passed to dumps")

View File

@ -36,6 +36,17 @@ class Reviver:
valid_namespaces: Optional[List[str]] = None, valid_namespaces: Optional[List[str]] = None,
secrets_from_env: bool = True, secrets_from_env: bool = True,
) -> None: ) -> None:
"""Initialize the reviver.
Args:
secrets_map: A map of secrets to load. If a secret is not found in
the map, it will be loaded from the environment if `secrets_from_env`
is True. Defaults to None.
valid_namespaces: A list of additional namespaces (modules)
to allow to be deserialized. Defaults to None.
secrets_from_env: Whether to load secrets from the environment.
Defaults to True.
"""
self.secrets_from_env = secrets_from_env self.secrets_from_env = secrets_from_env
self.secrets_map = secrets_map or dict() self.secrets_map = secrets_map or dict()
# By default only support langchain, but user can pass in additional namespaces # By default only support langchain, but user can pass in additional namespaces
@ -130,9 +141,13 @@ def loads(
Args: Args:
text: The string to load. text: The string to load.
secrets_map: A map of secrets to load. secrets_map: A map of secrets to load. If a secret is not found in
the map, it will be loaded from the environment if `secrets_from_env`
is True. Defaults to None.
valid_namespaces: A list of additional namespaces (modules) valid_namespaces: A list of additional namespaces (modules)
to allow to be deserialized. to allow to be deserialized. Defaults to None.
secrets_from_env: Whether to load secrets from the environment.
Defaults to True.
Returns: Returns:
Revived LangChain objects. Revived LangChain objects.
@ -155,9 +170,13 @@ def load(
Args: Args:
obj: The object to load. obj: The object to load.
secrets_map: A map of secrets to load. secrets_map: A map of secrets to load. If a secret is not found in
the map, it will be loaded from the environment if `secrets_from_env`
is True. Defaults to None.
valid_namespaces: A list of additional namespaces (modules) valid_namespaces: A list of additional namespaces (modules)
to allow to be deserialized. to allow to be deserialized. Defaults to None.
secrets_from_env: Whether to load secrets from the environment.
Defaults to True.
Returns: Returns:
Revived LangChain objects. Revived LangChain objects.

View File

@ -16,7 +16,14 @@ from langchain_core.pydantic_v1 import BaseModel
class BaseSerialized(TypedDict): class BaseSerialized(TypedDict):
"""Base class for serialized objects.""" """Base class for serialized objects.
Parameters:
lc: The version of the serialization format.
id: The unique identifier of the object.
name: The name of the object. Optional.
graph: The graph of the object. Optional.
"""
lc: int lc: int
id: List[str] id: List[str]
@ -25,20 +32,34 @@ class BaseSerialized(TypedDict):
class SerializedConstructor(BaseSerialized): class SerializedConstructor(BaseSerialized):
"""Serialized constructor.""" """Serialized constructor.
Parameters:
type: The type of the object. Must be "constructor".
kwargs: The constructor arguments.
"""
type: Literal["constructor"] type: Literal["constructor"]
kwargs: Dict[str, Any] kwargs: Dict[str, Any]
class SerializedSecret(BaseSerialized): class SerializedSecret(BaseSerialized):
"""Serialized secret.""" """Serialized secret.
Parameters:
type: The type of the object. Must be "secret".
"""
type: Literal["secret"] type: Literal["secret"]
class SerializedNotImplemented(BaseSerialized): class SerializedNotImplemented(BaseSerialized):
"""Serialized not implemented.""" """Serialized not implemented.
Parameters:
type: The type of the object. Must be "not_implemented".
repr: The representation of the object. Optional.
"""
type: Literal["not_implemented"] type: Literal["not_implemented"]
repr: Optional[str] repr: Optional[str]
@ -50,10 +71,13 @@ def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
Args: Args:
value: The value. value: The value.
key: The key. key: The key.
model: The model. model: The pydantic model.
Returns: Returns:
Whether the value is different from the default. Whether the value is different from the default.
Raises:
Exception: If the key is not in the model.
""" """
try: try:
return model.__fields__[key].get_default() != value return model.__fields__[key].get_default() != value
@ -69,23 +93,31 @@ class Serializable(BaseModel, ABC):
It relies on the following methods and properties: It relies on the following methods and properties:
- `is_lc_serializable`: Is this class serializable? - `is_lc_serializable`: Is this class serializable?
By design even if a class inherits from Serializable, it is not serializable by By design, even if a class inherits from Serializable, it is not serializable by
default. This is to prevent accidental serialization of objects that should not default. This is to prevent accidental serialization of objects that should not
be serialized. be serialized.
- `get_lc_namespace`: Get the namespace of the langchain object. - `get_lc_namespace`: Get the namespace of the langchain object.
During de-serialization this namespace is used to identify During deserialization, this namespace is used to identify
the correct class to instantiate. the correct class to instantiate.
Please see the `Reviver` class in `langchain_core.load.load` for more details. Please see the `Reviver` class in `langchain_core.load.load` for more details.
During de-serialization an additional mapping is handle During deserialization an additional mapping is handle
classes that have moved or been renamed across package versions. classes that have moved or been renamed across package versions.
- `lc_secrets`: A map of constructor argument names to secret ids. - `lc_secrets`: A map of constructor argument names to secret ids.
- `lc_attributes`: List of additional attribute names that should be included - `lc_attributes`: List of additional attribute names that should be included
as part of the serialized representation.. as part of the serialized representation.
""" """
@classmethod @classmethod
def is_lc_serializable(cls) -> bool: def is_lc_serializable(cls) -> bool:
"""Is this class serializable?""" """Is this class serializable?
By design, even if a class inherits from Serializable, it is not serializable by
default. This is to prevent accidental serialization of objects that should not
be serialized.
Returns:
Whether the class is serializable. Default is False.
"""
return False return False
@classmethod @classmethod
@ -111,6 +143,7 @@ class Serializable(BaseModel, ABC):
"""List of attribute names that should be included in the serialized kwargs. """List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor. These attributes must be accepted by the constructor.
Default is an empty dictionary.
""" """
return {} return {}
@ -120,6 +153,8 @@ class Serializable(BaseModel, ABC):
The unique identifier is a list of strings that describes the path The unique identifier is a list of strings that describes the path
to the object. to the object.
For example, for the class `langchain.llms.openai.OpenAI`, the id is
["langchain", "llms", "openai", "OpenAI"].
""" """
return [*cls.get_lc_namespace(), cls.__name__] return [*cls.get_lc_namespace(), cls.__name__]
@ -134,6 +169,11 @@ class Serializable(BaseModel, ABC):
] ]
def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]: def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
"""Serialize the object to JSON.
Returns:
A json serializable object or a SerializedNotImplemented object.
"""
if not self.is_lc_serializable(): if not self.is_lc_serializable():
return self.to_json_not_implemented() return self.to_json_not_implemented()
@ -209,7 +249,10 @@ def _is_field_useful(inst: Serializable, key: str, value: Any) -> bool:
value: The value. value: The value.
Returns: Returns:
Whether the field is useful. Whether the field is useful. If the field is required, it is useful.
If the field is not required, it is useful if the value is not None.
If the field is not required and the value is None, it is useful if the
default value is different from the value.
""" """
field = inst.__fields__.get(key) field = inst.__fields__.get(key)
if not field: if not field:
@ -242,7 +285,7 @@ def to_json_not_implemented(obj: object) -> SerializedNotImplemented:
"""Serialize a "not implemented" object. """Serialize a "not implemented" object.
Args: Args:
obj: object to serialize obj: object to serialize.
Returns: Returns:
SerializedNotImplemented SerializedNotImplemented