From f691053c32c0148c6b321a57a72d493948b1e23d Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 13 Nov 2023 12:10:34 -0500 Subject: [PATCH 1/3] x --- langserve/server.py | 12 ++++++++++-- tests/unit_tests/test_server_client.py | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/langserve/server.py b/langserve/server.py index aec218fb..6c88b5bf 100644 --- a/langserve/server.py +++ b/langserve/server.py @@ -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 " + "not accept `configurable`. ", + ) projected_config = {k: config[k] for k in keys if k in config} return ( per_req_config_modifier(projected_config, request) @@ -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, @@ -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. diff --git a/tests/unit_tests/test_server_client.py b/tests/unit_tests/test_server_client.py index 8f4281f4..3b943698 100644 --- a/tests/unit_tests/test_server_client.py +++ b/tests/unit_tests/test_server_client.py @@ -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 @@ -1094,7 +1094,7 @@ 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" ) @@ -1102,11 +1102,22 @@ async def test_configurable_runnables(event_loop: AbstractEventLoop) -> None: 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 From 9283e6a668d8a5c932be904264a0d5ecc6818ff3 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 13 Nov 2023 13:52:30 -0500 Subject: [PATCH 2/3] x --- langserve/server.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/langserve/server.py b/langserve/server.py index 6c88b5bf..5f58aefc 100644 --- a/langserve/server.py +++ b/langserve/server.py @@ -118,8 +118,7 @@ def _unpack_request_config( if "configurable" not in keys: raise HTTPException( 422, - "Server code has modified the default accepted config keys to " - "not accept `configurable`. ", + "The config field `configurable` has been disallowed by the server.", ) projected_config = {k: config[k] for k in keys if k in config} return ( From b49b326b84583c3f116557332805a29f6cdf5ba8 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 13 Nov 2023 14:02:09 -0500 Subject: [PATCH 3/3] x --- langserve/server.py | 24 +++++++++++++----------- tests/unit_tests/test_validation.py | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/langserve/server.py b/langserve/server.py index 5f58aefc..9bd37fce 100644 --- a/langserve/server.py +++ b/langserve/server.py @@ -97,7 +97,7 @@ def _config_from_hash(config_hash: str) -> Dict[str, Any]: def _unpack_request_config( *configs: Union[BaseModel, Mapping, str], - keys: Sequence[str], + config_keys: Sequence[str], model: Type[BaseModel], request: Request, per_req_config_modifier: Optional[PerRequestConfigModifier], @@ -115,12 +115,14 @@ def _unpack_request_config( 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: + if "configurable" not in config_keys: raise HTTPException( 422, - "The config field `configurable` has been disallowed by the server.", + "The config field `configurable` has been disallowed by the server. " + "This can be modified server side by adding `configurable` to the list " + "of `config_keys` argument in `add_routes`", ) - projected_config = {k: config[k] for k in keys if k in config} + projected_config = {k: config[k] for k in config_keys if k in config} return ( per_req_config_modifier(projected_config, request) if per_req_config_modifier @@ -576,7 +578,7 @@ async def _get_config_and_input( config = _unpack_request_config( config_hash, body.config, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -661,7 +663,7 @@ async def batch( _unpack_request_config( config_hash, config, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -672,7 +674,7 @@ async def batch( configs = _unpack_request_config( config_hash, config, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -942,7 +944,7 @@ async def input_schema(request: Request, config_hash: str = "") -> Any: with _with_validation_error_translation(): config = _unpack_request_config( config_hash, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -965,7 +967,7 @@ async def output_schema(request: Request, config_hash: str = "") -> Any: with _with_validation_error_translation(): config = _unpack_request_config( config_hash, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -985,7 +987,7 @@ async def config_schema(request: Request, config_hash: str = "") -> Any: with _with_validation_error_translation(): config = _unpack_request_config( config_hash, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, @@ -1004,7 +1006,7 @@ async def playground( with _with_validation_error_translation(): config = _unpack_request_config( config_hash, - keys=config_keys, + config_keys=config_keys, model=ConfigPayload, request=request, per_req_config_modifier=per_req_config_modifier, diff --git a/tests/unit_tests/test_validation.py b/tests/unit_tests/test_validation.py index d63f3810..c6442e6a 100644 --- a/tests/unit_tests/test_validation.py +++ b/tests/unit_tests/test_validation.py @@ -157,7 +157,7 @@ def test_invoke_request_with_runnables() -> None: Model( input={"name": "bob"}, ).config, - keys=[], + config_keys=[], model=config, request=MagicMock(Request), per_req_config_modifier=lambda x, y: x, @@ -184,7 +184,7 @@ def test_invoke_request_with_runnables() -> None: assert _unpack_request_config( request.config, - keys=["configurable"], + config_keys=["configurable"], model=config, request=MagicMock(Request), per_req_config_modifier=lambda x, y: x,