Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable configurable by default langserve side #219

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def _unpack_request_config(
else:
raise TypeError(f"Expected a string, dict or BaseModel got {type(config)}")
config = merge_configs(*config_dicts)
if "configurable" in config and config["configurable"]:
if "configurable" not in keys:
raise HTTPException(
422,
"Server code has modified the default accepted config keys to "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - should we say something like:

setting configurable fields is disallowed by the server

"not accept `configurable`. ",
)
projected_config = {k: config[k] for k in keys if k in config}
return (
per_req_config_modifier(projected_config, request)
Expand Down Expand Up @@ -405,7 +412,7 @@ def add_routes(
path: str = "",
input_type: Union[Type, Literal["auto"], BaseModel] = "auto",
output_type: Union[Type, Literal["auto"], BaseModel] = "auto",
config_keys: Sequence[str] = (),
config_keys: Sequence[str] = ("configurable",),
include_callback_events: bool = False,
enable_feedback_endpoint: bool = False,
per_req_config_modifier: Optional[PerRequestConfigModifier] = None,
Expand Down Expand Up @@ -438,7 +445,8 @@ def add_routes(
Favor using runnable.with_types(input_type=..., output_type=...) instead.
This parameter may get deprecated!
config_keys: list of config keys that will be accepted, by default
no config keys are accepted.
will accept `configurable` key in the config. Will only be used
if the runnable is configurable.
include_callback_events: Whether to include callback events in the response.
If true, the client will be able to show trace information
including events that occurred on the server side.
Expand Down
17 changes: 14 additions & 3 deletions tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ async def test_configurable_runnables(event_loop: AbstractEventLoop) -> None:
assert chain.invoke({"name": "cat"}) == "say cat"

app = FastAPI()
add_routes(app, chain, config_keys=["tags", "configurable"])
add_routes(app, chain)

async with get_async_remote_runnable(app) as remote_runnable:
# Test with hard-coded LLM
Expand All @@ -1094,19 +1094,30 @@ async def test_configurable_runnables(event_loop: AbstractEventLoop) -> None:
assert (
await remote_runnable.ainvoke(
{"name": "foo"},
{"configurable": {"template": "hear {name}"}, "tags": ["h"]},
{"configurable": {"template": "hear {name}"}},
)
== "hear foo"
)
# Test with alternative passthrough LLM
assert (
await remote_runnable.ainvoke(
{"name": "foo"},
{"configurable": {"llm": "hardcoded_llm"}, "tags": ["h"]},
{"configurable": {"llm": "hardcoded_llm"}},
)
== "hello Mr. Kitten!"
)

add_routes(app, chain, path="/no_config", config_keys=["tags"])

async with get_async_remote_runnable(app, path="/no_config") as remote_runnable:
with pytest.raises(httpx.HTTPError) as cb:
await remote_runnable.ainvoke(
{"name": "foo"},
{"configurable": {"template": "hear {name}"}},
)

assert cb.value.response.status_code == 422


# Test for utilities

Expand Down