mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-09 06:53:59 +00:00
core: add additional import mappings to loads (#26406)
Support using additional import mapping. This allows users to override old mappings/add new imports to the loads function. - [x ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from typing import Dict
|
||||
|
||||
from langchain_core.load import Serializable, dumpd
|
||||
from langchain_core.load import Serializable, dumpd, load
|
||||
from langchain_core.load.serializable import _is_field_useful
|
||||
from langchain_core.pydantic_v1 import Field
|
||||
|
||||
@@ -107,3 +107,61 @@ def test__is_field_useful() -> None:
|
||||
foo = Foo(x=default_x, y=default_y, z=ArrayObj())
|
||||
assert not _is_field_useful(foo, "x", foo.x)
|
||||
assert not _is_field_useful(foo, "y", foo.y)
|
||||
|
||||
|
||||
class Foo(Serializable):
|
||||
bar: int
|
||||
baz: str
|
||||
|
||||
@classmethod
|
||||
def is_lc_serializable(cls) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def test_simple_deserialization() -> None:
|
||||
foo = Foo(bar=1, baz="hello")
|
||||
assert foo.lc_id() == ["tests", "unit_tests", "load", "test_serializable", "Foo"]
|
||||
serialized_foo = dumpd(foo)
|
||||
assert serialized_foo == {
|
||||
"id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
|
||||
"kwargs": {"bar": 1, "baz": "hello"},
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
}
|
||||
new_foo = load(serialized_foo, valid_namespaces=["tests"])
|
||||
assert new_foo == foo
|
||||
|
||||
|
||||
class Foo2(Serializable):
|
||||
bar: int
|
||||
baz: str
|
||||
|
||||
@classmethod
|
||||
def is_lc_serializable(cls) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def test_simple_deserialization_with_additional_imports() -> None:
|
||||
foo = Foo(bar=1, baz="hello")
|
||||
assert foo.lc_id() == ["tests", "unit_tests", "load", "test_serializable", "Foo"]
|
||||
serialized_foo = dumpd(foo)
|
||||
assert serialized_foo == {
|
||||
"id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
|
||||
"kwargs": {"bar": 1, "baz": "hello"},
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
}
|
||||
new_foo = load(
|
||||
serialized_foo,
|
||||
valid_namespaces=["tests"],
|
||||
additional_import_mappings={
|
||||
("tests", "unit_tests", "load", "test_serializable", "Foo"): (
|
||||
"tests",
|
||||
"unit_tests",
|
||||
"load",
|
||||
"test_serializable",
|
||||
"Foo2",
|
||||
)
|
||||
},
|
||||
)
|
||||
assert isinstance(new_foo, Foo2)
|
||||
|
Reference in New Issue
Block a user