mirror of
https://github.com/hwchase17/langchain
synced 2024-11-11 19:11:02 +00:00
core[patch]: support load from path for default namespaces (#26675)
This commit is contained in:
parent
e8236e58f2
commit
409f35363b
@ -19,6 +19,17 @@ DEFAULT_NAMESPACES = [
|
|||||||
"langchain_anthropic",
|
"langchain_anthropic",
|
||||||
"langchain_groq",
|
"langchain_groq",
|
||||||
"langchain_google_genai",
|
"langchain_google_genai",
|
||||||
|
"langchain_aws",
|
||||||
|
"langchain_openai",
|
||||||
|
"langchain_google_vertexai",
|
||||||
|
"langchain_mistralai",
|
||||||
|
"langchain_fireworks",
|
||||||
|
]
|
||||||
|
# Namespaces for which only deserializing via the SERIALIZABLE_MAPPING is allowed.
|
||||||
|
# Load by path is not allowed.
|
||||||
|
DISALLOW_LOAD_FROM_PATH = [
|
||||||
|
"langchain_community",
|
||||||
|
"langchain",
|
||||||
]
|
]
|
||||||
|
|
||||||
ALL_SERIALIZABLE_MAPPINGS = {
|
ALL_SERIALIZABLE_MAPPINGS = {
|
||||||
@ -103,40 +114,31 @@ class Reviver:
|
|||||||
and value.get("id", None) is not None
|
and value.get("id", None) is not None
|
||||||
):
|
):
|
||||||
[*namespace, name] = value["id"]
|
[*namespace, name] = value["id"]
|
||||||
|
mapping_key = tuple(value["id"])
|
||||||
|
|
||||||
if namespace[0] not in self.valid_namespaces:
|
if namespace[0] not in self.valid_namespaces:
|
||||||
raise ValueError(f"Invalid namespace: {value}")
|
raise ValueError(f"Invalid namespace: {value}")
|
||||||
|
# The root namespace ["langchain"] is not a valid identifier.
|
||||||
# The root namespace "langchain" is not a valid identifier.
|
elif namespace == ["langchain"]:
|
||||||
if len(namespace) == 1 and namespace[0] == "langchain":
|
|
||||||
raise ValueError(f"Invalid namespace: {value}")
|
raise ValueError(f"Invalid namespace: {value}")
|
||||||
|
# Has explicit import path.
|
||||||
# If namespace is in known namespaces, try to use mapping
|
elif mapping_key in self.import_mappings:
|
||||||
key = tuple(namespace + [name])
|
import_path = self.import_mappings[mapping_key]
|
||||||
if namespace[0] in DEFAULT_NAMESPACES:
|
|
||||||
# Get the importable path
|
|
||||||
if key not in self.import_mappings:
|
|
||||||
raise ValueError(
|
|
||||||
"Trying to deserialize something that cannot "
|
|
||||||
"be deserialized in current version of langchain-core: "
|
|
||||||
f"{key}"
|
|
||||||
)
|
|
||||||
import_path = self.import_mappings[key]
|
|
||||||
# Split into module and name
|
# Split into module and name
|
||||||
import_dir, import_obj = import_path[:-1], import_path[-1]
|
import_dir, name = import_path[:-1], import_path[-1]
|
||||||
# Import module
|
# Import module
|
||||||
mod = importlib.import_module(".".join(import_dir))
|
mod = importlib.import_module(".".join(import_dir))
|
||||||
# Import class
|
elif namespace[0] in DISALLOW_LOAD_FROM_PATH:
|
||||||
cls = getattr(mod, import_obj)
|
raise ValueError(
|
||||||
# Otherwise, load by path
|
"Trying to deserialize something that cannot "
|
||||||
|
"be deserialized in current version of langchain-core: "
|
||||||
|
f"{mapping_key}."
|
||||||
|
)
|
||||||
|
# Otherwise, treat namespace as path.
|
||||||
else:
|
else:
|
||||||
if key in self.additional_import_mappings:
|
mod = importlib.import_module(".".join(namespace))
|
||||||
import_path = self.import_mappings[key]
|
|
||||||
mod = importlib.import_module(".".join(import_path[:-1]))
|
cls = getattr(mod, name)
|
||||||
name = import_path[-1]
|
|
||||||
else:
|
|
||||||
mod = importlib.import_module(".".join(namespace))
|
|
||||||
cls = getattr(mod, name)
|
|
||||||
|
|
||||||
# The class must be a subclass of Serializable.
|
# The class must be a subclass of Serializable.
|
||||||
if not issubclass(cls, Serializable):
|
if not issubclass(cls, Serializable):
|
||||||
|
Loading…
Reference in New Issue
Block a user