mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 15:43:54 +00:00
Ensure that configurable fields with enums support deduplication (#11909)
<!-- Thank you for contributing to LangChain! Replace this entire comment with: - **Description:** a description of the change, - **Issue:** the issue # it fixes (if applicable), - **Dependencies:** any dependencies required for this change, - **Tag maintainer:** for a quicker response, tag the relevant maintainer (see below), - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out! Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` to check this locally. See contribution guidelines for more information on how to write/run tests, lint, etc: https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md If you're adding a new integration, please include: 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/extras` directory. If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17. -->
This commit is contained in:
commit
42cd2ef329
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user