feat: dict() protocol (#85)

* feat: dict() protocol

Signed-off-by: aarnphm-ec2-dev <29749331+aarnphm@users.noreply.github.com>

* ci: auto fixes from pre-commit.ci
For more information, see https://pre-commit.ci

---------

Signed-off-by: aarnphm-ec2-dev <29749331+aarnphm@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Aaron Pham
2023-06-28 00:00:29 -04:00
committed by GitHub
parent f37136de1a
commit c0a9aa4314

View File

@@ -1575,8 +1575,6 @@ class LLMConfig(_ConfigAttr):
"""
if item is None:
raise TypeError(f"{self} doesn't understand how to index None.")
if not isinstance(item, str):
raise TypeError(f"LLM only supports string indexing, not {item.__class__.__name__}")
item = inflection.underscore(item)
if item in _reserved_namespace:
raise ForbiddenAttributeError(
@@ -1603,6 +1601,27 @@ class LLMConfig(_ConfigAttr):
)
return _object_getattribute.__get__(self)(item)
def __len__(self):
return len(self.__openllm_accepted_keys__) + len(self.__openllm_extras__)
def keys(self) -> list[str]:
return list(self.__openllm_accepted_keys__) + list(self.__openllm_extras__)
def values(self) -> list[t.Any]:
return [getattr(self, k.name) for k in attr.fields(self.__class__)] + [getattr(self.generation_config, k.name) for k in attr.fields(self.__openllm_generation_class__)] + list(self.__openllm_extras__.values())
def items(self) -> list[tuple[str, t.Any]]:
return [(k.name, getattr(self, k.name)) for k in attr.fields(self.__class__)] + [(k.name, getattr(self.generation_config, k.name)) for k in attr.fields(self.__openllm_generation_class__)] + list(self.__openllm_extras__.items())
def __iter__(self):
return iter(self.keys())
def __contains__(self, item: t.Any):
if item in self.__openllm_extras__:
return True
return item in self.__openllm_accepted_keys__
@classmethod
def model_derivate(cls, name: str | None = None, **attrs: t.Any) -> LLMConfig:
"""A helper class to generate a new LLMConfig class with additional attributes.