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
|
@property
|
||||||
def lc_secrets(self) -> Dict[str, str]:
|
def lc_secrets(self) -> Dict[str, str]:
|
||||||
"""
|
"""A map of constructor argument names to secret ids.
|
||||||
Return a map of constructor argument names to secret ids.
|
|
||||||
eg. {"openai_api_key": "OPENAI_API_KEY"}
|
For example,
|
||||||
|
{"openai_api_key": "OPENAI_API_KEY"}
|
||||||
"""
|
"""
|
||||||
return dict()
|
return dict()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def lc_attributes(self) -> Dict:
|
def lc_attributes(self) -> Dict:
|
||||||
"""
|
"""List of attribute names that should be included in the serialized kwargs.
|
||||||
Return a list of attribute names that should be included in the
|
|
||||||
serialized kwargs. These attributes must be accepted by the
|
These attributes must be accepted by the constructor.
|
||||||
constructor.
|
|
||||||
"""
|
"""
|
||||||
return {}
|
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:
|
class Config:
|
||||||
extra = "ignore"
|
extra = "ignore"
|
||||||
|
|
||||||
@ -122,7 +131,7 @@ class Serializable(BaseModel, ABC):
|
|||||||
return {
|
return {
|
||||||
"lc": 1,
|
"lc": 1,
|
||||||
"type": "constructor",
|
"type": "constructor",
|
||||||
"id": [*self.get_lc_namespace(), self.__class__.__name__],
|
"id": self.lc_id(),
|
||||||
"kwargs": lc_kwargs
|
"kwargs": lc_kwargs
|
||||||
if not secrets
|
if not secrets
|
||||||
else _replace_secrets(lc_kwargs, secrets),
|
else _replace_secrets(lc_kwargs, secrets),
|
||||||
|
@ -57,6 +57,7 @@ def test_person(snapshot: Any) -> None:
|
|||||||
assert dumps(p, pretty=True) == snapshot
|
assert dumps(p, pretty=True) == snapshot
|
||||||
sp = SpecialPerson(another_secret="Wooo", secret="Hmm")
|
sp = SpecialPerson(another_secret="Wooo", secret="Hmm")
|
||||||
assert dumps(sp, pretty=True) == snapshot
|
assert dumps(sp, pretty=True) == snapshot
|
||||||
|
assert Person.lc_id() == ["test_dump", "Person"]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.requires("openai")
|
@pytest.mark.requires("openai")
|
||||||
|
Loading…
Reference in New Issue
Block a user