mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 12:48:12 +00:00
Expose lc_id as a classmethod (#11176)
* Expose LC id as a class method * User should not need to know that the last part of the id is the class name
This commit is contained in:
parent
44489e7029
commit
de3e25683e
@ -50,21 +50,30 @@ class Serializable(BaseModel, ABC):
|
||||
|
||||
@property
|
||||
def lc_secrets(self) -> Dict[str, str]:
|
||||
"""
|
||||
Return a map of constructor argument names to secret ids.
|
||||
eg. {"openai_api_key": "OPENAI_API_KEY"}
|
||||
"""A map of constructor argument names to secret ids.
|
||||
|
||||
For example,
|
||||
{"openai_api_key": "OPENAI_API_KEY"}
|
||||
"""
|
||||
return dict()
|
||||
|
||||
@property
|
||||
def lc_attributes(self) -> Dict:
|
||||
"""
|
||||
Return a list of attribute names that should be included in the
|
||||
serialized kwargs. These attributes must be accepted by the
|
||||
constructor.
|
||||
"""List of attribute names that should be included in the serialized kwargs.
|
||||
|
||||
These attributes must be accepted by the constructor.
|
||||
"""
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def lc_id(cls) -> List[str]:
|
||||
"""A unique identifier for this class for serialization purposes.
|
||||
|
||||
The unique identifier is a list of strings that describes the path
|
||||
to the object.
|
||||
"""
|
||||
return [*cls.get_lc_namespace(), cls.__name__]
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
|
||||
@ -122,7 +131,7 @@ class Serializable(BaseModel, ABC):
|
||||
return {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [*self.get_lc_namespace(), self.__class__.__name__],
|
||||
"id": self.lc_id(),
|
||||
"kwargs": lc_kwargs
|
||||
if not secrets
|
||||
else _replace_secrets(lc_kwargs, secrets),
|
||||
|
@ -57,6 +57,7 @@ def test_person(snapshot: Any) -> None:
|
||||
assert dumps(p, pretty=True) == snapshot
|
||||
sp = SpecialPerson(another_secret="Wooo", secret="Hmm")
|
||||
assert dumps(sp, pretty=True) == snapshot
|
||||
assert Person.lc_id() == ["test_dump", "Person"]
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
|
Loading…
Reference in New Issue
Block a user