From cf448a63144b832c090e99c4a02557599bffb4a0 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Oct 2023 08:25:21 +0100 Subject: [PATCH 1/2] Ensure that configurable fields with enums support deduplication --- .../langchain/schema/runnable/configurable.py | 33 ++++++++++++++----- .../langchain/schema/runnable/utils.py | 23 +++++++++++++ .../schema/runnable/test_runnable.py | 3 +- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/libs/langchain/langchain/schema/runnable/configurable.py b/libs/langchain/langchain/schema/runnable/configurable.py index 7c246e6e01c..bf45e4c004a 100644 --- a/libs/langchain/langchain/schema/runnable/configurable.py +++ b/libs/langchain/langchain/schema/runnable/configurable.py @@ -14,6 +14,7 @@ from typing import ( Union, cast, ) +from weakref import WeakValueDictionary from langchain.pydantic_v1 import BaseModel from langchain.schema.runnable.base import Runnable, RunnableSerializable @@ -262,6 +263,14 @@ class StrEnum(str, enum.Enum): pass +_enums_for_spec: WeakValueDictionary[ + Union[ + ConfigurableFieldSingleOption, ConfigurableFieldMultiOption, ConfigurableField + ], + Type[StrEnum], +] = WeakValueDictionary() + + class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]): which: ConfigurableField @@ -271,10 +280,14 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]): @property def config_specs(self) -> Sequence[ConfigurableFieldSpec]: - which_enum = StrEnum( # type: ignore[call-overload] - self.which.name or self.which.id, - ((v, v) for v in list(self.alternatives.keys()) + [self.default_key]), - ) + if which_enum := _enums_for_spec.get(self.which): + pass + else: + which_enum = StrEnum( # type: ignore[call-overload] + self.which.name or self.which.id, + ((v, v) for v in list(self.alternatives.keys()) + [self.default_key]), + ) + _enums_for_spec[self.which] = cast(Type[StrEnum], which_enum) return [ ConfigurableFieldSpec( id=self.which.id, @@ -312,10 +325,14 @@ def make_options_spec( spec: Union[ConfigurableFieldSingleOption, ConfigurableFieldMultiOption], description: Optional[str], ) -> ConfigurableFieldSpec: - enum = StrEnum( # type: ignore[call-overload] - spec.name or spec.id, - ((v, v) for v in list(spec.options.keys())), - ) + if enum := _enums_for_spec.get(spec): + pass + else: + enum = StrEnum( # type: ignore[call-overload] + spec.name or spec.id, + ((v, v) for v in list(spec.options.keys())), + ) + _enums_for_spec[spec] = cast(Type[StrEnum], enum) if isinstance(spec, ConfigurableFieldSingleOption): return ConfigurableFieldSpec( id=spec.id, diff --git a/libs/langchain/langchain/schema/runnable/utils.py b/libs/langchain/langchain/schema/runnable/utils.py index 284cf8cc237..a5655b243df 100644 --- a/libs/langchain/langchain/schema/runnable/utils.py +++ b/libs/langchain/langchain/schema/runnable/utils.py @@ -250,6 +250,9 @@ class ConfigurableField(NamedTuple): description: Optional[str] = None annotation: Optional[Any] = None + def __hash__(self) -> int: + return hash((self.id, self.annotation)) + class ConfigurableFieldSingleOption(NamedTuple): """A field that can be configured by the user with a default value.""" @@ -261,6 +264,9 @@ class ConfigurableFieldSingleOption(NamedTuple): name: Optional[str] = None description: Optional[str] = None + def __hash__(self) -> int: + return hash((self.id, tuple(self.options.items()), self.default)) + class ConfigurableFieldMultiOption(NamedTuple): """A field that can be configured by the user with multiple default values.""" @@ -272,6 +278,9 @@ class ConfigurableFieldMultiOption(NamedTuple): name: Optional[str] = None description: Optional[str] = None + def __hash__(self) -> int: + return hash((self.id, tuple(self.options.items()), tuple(self.default))) + AnyConfigurableField = Union[ ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption @@ -298,6 +307,20 @@ def get_unique_config_specs( for id, dupes in grouped: first = next(dupes) others = list(dupes) + if len(others) > 0: + print( + first, + others, + [ + ( + o.id == first.id, + o.annotation == first.annotation, + o.default == first.default, + o.name == first.name, + ) + for o in others + ], + ) if len(others) == 0: unique.append(first) elif all(o == first for o in others): diff --git a/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py b/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py index 87a558db649..3f2189342da 100644 --- a/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py +++ b/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py @@ -941,7 +941,8 @@ def test_configurable_fields_example() -> None: ) ) - chain_configurable = prompt | fake_llm + # deduplication of configurable fields + chain_configurable = prompt | fake_llm | (lambda x: {"name": x}) | prompt | fake_llm assert chain_configurable.invoke({"name": "John"}) == "a" From 754aca794f89b1cf118e35ac28d4a7ebcc1d1214 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Oct 2023 08:46:07 +0100 Subject: [PATCH 2/2] remove print --- libs/langchain/langchain/schema/runnable/utils.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/libs/langchain/langchain/schema/runnable/utils.py b/libs/langchain/langchain/schema/runnable/utils.py index a5655b243df..cb1be68da4f 100644 --- a/libs/langchain/langchain/schema/runnable/utils.py +++ b/libs/langchain/langchain/schema/runnable/utils.py @@ -307,20 +307,6 @@ def get_unique_config_specs( for id, dupes in grouped: first = next(dupes) others = list(dupes) - if len(others) > 0: - print( - first, - others, - [ - ( - o.id == first.id, - o.annotation == first.annotation, - o.default == first.default, - o.name == first.name, - ) - for o in others - ], - ) if len(others) == 0: unique.append(first) elif all(o == first for o in others):