classTest: def__init__(self, a, b): self.a = a self.b = b def__repr__(self): returnf"<Test a={self.a} b={self.b}>" defuser_decoder(d): if'a'in d and'b'in d: return Test(d['a'], d['b']) return d
def__call__(self, value: dict[str, Any]) -> Any: if ( value.get("lc") == 1 and value.get("type") == "secret" and value.get("id") isnotNone ): [key] = value["id"] if key inself.secrets_map: returnself.secrets_map[key] ifself.secrets_from_env and key in os.environ and os.environ[key]: return os.environ[key] returnNone
if ( value.get("lc") == 1 and value.get("type") == "not_implemented" and value.get("id") isnotNone ): ifself.ignore_unserializable_fields: returnNone msg = ( "Trying to load an object that doesn't implement " f"serialization: {value}" ) raise NotImplementedError(msg)
if ( value.get("lc") == 1 and value.get("type") == "constructor" and value.get("id") isnotNone ): [*namespace, name] = value["id"] mapping_key = tuple(value["id"])
if ( namespace[0] notinself.valid_namespaces # The root namespace ["langchain"] is not a valid identifier. or namespace == ["langchain"] ): msg = f"Invalid namespace: {value}" raise ValueError(msg) # Has explicit import path. if mapping_key inself.import_mappings: import_path = self.import_mappings[mapping_key] # Split into module and name import_dir, name = import_path[:-1], import_path[-1] # Import module mod = importlib.import_module(".".join(import_dir)) elif namespace[0] in DISALLOW_LOAD_FROM_PATH: msg = ( "Trying to deserialize something that cannot " "be deserialized in current version of langchain-core: " f"{mapping_key}." ) raise ValueError(msg) # Otherwise, treat namespace as path. else: mod = importlib.import_module(".".join(namespace))
cls = getattr(mod, name)
# The class must be a subclass of Serializable. ifnotissubclass(cls, Serializable): msg = f"Invalid namespace: {value}" raise ValueError(msg)
# We don't need to recurse on kwargs # as json.loads will do that for us. kwargs = value.get("kwargs", {}) return cls(**kwargs)
restored_obj = loads(json_str) response = restored_obj.invoke("Hello, are you there?")
不过如果确实这么写了是能收到请求/响应
<!doctype html> <htmllang=en> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>