Automatically add configurable key to config_schema if config_specs i… (#12798)

…s present

<!-- 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:
Nuno Campos 2023-11-02 21:46:15 +00:00 committed by GitHub
parent 21eeba075c
commit f66a9d2adf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 12 deletions

View File

@ -202,7 +202,9 @@ class Runnable(Generic[Input, Output], ABC):
"""List configurable fields for this runnable."""
return []
def config_schema(self, *, include: Sequence[str]) -> Type[BaseModel]:
def config_schema(
self, *, include: Optional[Sequence[str]] = None
) -> Type[BaseModel]:
"""The type of config this runnable accepts specified as a pydantic model.
To mark a field as configurable, see the `configurable_fields`
@ -233,7 +235,7 @@ class Runnable(Generic[Input, Output], ABC):
for spec in config_specs
},
)
if config_specs and "configurable" in include
if config_specs
else None
)
@ -2354,7 +2356,9 @@ class RunnableEach(RunnableSerializable[List[Input], List[Output]]):
def config_specs(self) -> Sequence[ConfigurableFieldSpec]:
return self.bound.config_specs
def config_schema(self, *, include: Sequence[str]) -> Type[BaseModel]:
def config_schema(
self, *, include: Optional[Sequence[str]] = None
) -> Type[BaseModel]:
return self.bound.config_schema(include=include)
@classmethod
@ -2516,7 +2520,9 @@ class RunnableBinding(RunnableSerializable[Input, Output]):
def config_specs(self) -> Sequence[ConfigurableFieldSpec]:
return self.bound.config_specs
def config_schema(self, *, include: Sequence[str]) -> Type[BaseModel]:
def config_schema(
self, *, include: Optional[Sequence[str]] = None
) -> Type[BaseModel]:
return self.bound.config_schema(include=include)
@classmethod

View File

@ -71,7 +71,9 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):
for spec in step.config_specs
)
def config_schema(self, *, include: Sequence[str]) -> Type[BaseModel]:
def config_schema(
self, *, include: Optional[Sequence[str]] = None
) -> Type[BaseModel]:
return self.runnable.config_schema(include=include)
@classmethod

View File

@ -869,7 +869,7 @@ def test_configurable_fields() -> None:
assert fake_llm_configurable.invoke("...") == "a"
assert fake_llm_configurable.config_schema(include=["configurable"]).schema() == {
assert fake_llm_configurable.config_schema().schema() == {
"title": "RunnableConfigurableFieldsConfig",
"type": "object",
"properties": {"configurable": {"$ref": "#/definitions/Configurable"}},
@ -912,7 +912,7 @@ def test_configurable_fields() -> None:
text="Hello, John!"
)
assert prompt_configurable.config_schema(include=["configurable"]).schema() == {
assert prompt_configurable.config_schema().schema() == {
"title": "RunnableConfigurableFieldsConfig",
"type": "object",
"properties": {"configurable": {"$ref": "#/definitions/Configurable"}},
@ -955,7 +955,7 @@ def test_configurable_fields() -> None:
assert chain_configurable.invoke({"name": "John"}) == "a"
assert chain_configurable.config_schema(include=["configurable"]).schema() == {
assert chain_configurable.config_schema().schema() == {
"title": "RunnableSequenceConfig",
"type": "object",
"properties": {"configurable": {"$ref": "#/definitions/Configurable"}},
@ -1021,9 +1021,7 @@ def test_configurable_fields() -> None:
"llm3": "a",
}
assert chain_with_map_configurable.config_schema(
include=["configurable"]
).schema() == {
assert chain_with_map_configurable.config_schema().schema() == {
"title": "RunnableSequenceConfig",
"type": "object",
"properties": {"configurable": {"$ref": "#/definitions/Configurable"}},
@ -1122,7 +1120,7 @@ def test_configurable_fields_example() -> None:
assert chain_configurable.invoke({"name": "John"}) == "a"
assert chain_configurable.config_schema(include=["configurable"]).schema() == {
assert chain_configurable.config_schema().schema() == {
"title": "RunnableSequenceConfig",
"type": "object",
"properties": {"configurable": {"$ref": "#/definitions/Configurable"}},