Support runnable factories in .configurable_alts() (#12065)

<!-- 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.
 -->
pull/11804/head^2
Nuno Campos 9 months ago committed by GitHub
parent b01a443ee5
commit 5fee61a207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1013,7 +1013,7 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
self,
which: ConfigurableField,
default_key: str = "default",
**kwargs: Runnable[Input, Output],
**kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]],
) -> RunnableSerializable[Input, Output]:
from langchain.schema.runnable.configurable import (
RunnableConfigurableAlternatives,

@ -6,6 +6,7 @@ from abc import abstractmethod
from typing import (
Any,
AsyncIterator,
Callable,
Dict,
Iterator,
List,
@ -287,7 +288,10 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
which: ConfigurableField
alternatives: Dict[str, RunnableSerializable[Input, Output]]
alternatives: Dict[
str,
Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]],
]
default_key: str = "default"
@ -314,7 +318,12 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
default=self.default_key,
),
*self.default.config_specs,
] + [s for alt in self.alternatives.values() for s in alt.config_specs]
] + [
s
for alt in self.alternatives.values()
if isinstance(alt, RunnableSerializable)
for s in alt.config_specs
]
def configurable_fields(
self, **kwargs: AnyConfigurableField
@ -333,7 +342,11 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
if which == self.default_key:
return self.default
elif which in self.alternatives:
return self.alternatives[which]
alt = self.alternatives[which]
if isinstance(alt, Runnable):
return alt
else:
return alt()
else:
raise ValueError(f"Unknown alternative: {which}")

@ -1,4 +1,5 @@
import sys
from functools import partial
from operator import itemgetter
from typing import (
Any,
@ -925,6 +926,17 @@ def test_configurable_fields() -> None:
).invoke({"name": "John"}) == {"llm1": "c", "llm2": "c", "llm3": "d"}
def test_configurable_alts_factory() -> None:
fake_llm = FakeListLLM(responses=["a"]).configurable_alternatives(
ConfigurableField(id="llm", name="LLM"),
chat=partial(FakeListLLM, responses=["b"]),
)
assert fake_llm.invoke("...") == "a"
assert fake_llm.with_config(configurable={"llm": "chat"}).invoke("...") == "b"
def test_configurable_fields_example() -> None:
fake_chat = FakeListChatModel(responses=["b"]).configurable_fields(
responses=ConfigurableFieldMultiOption(

Loading…
Cancel
Save