Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /PascalNobereit_langserve /tests /unit_tests /test_validation.py
| from typing import List, Optional | |
| from unittest.mock import MagicMock | |
| import pytest | |
| from fastapi import Request | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain_core.runnables import ConfigurableField | |
| from langserve.api_handler import _unpack_request_config | |
| try: | |
| from pydantic.v1 import BaseModel, ValidationError | |
| except ImportError: | |
| from pydantic import BaseModel, ValidationError | |
| from langserve.validation import ( | |
| create_batch_request_model, | |
| create_invoke_request_model, | |
| ) | |
| def test_create_invoke_and_batch_models(test_case: dict) -> None: | |
| """Test that the invoke request model is created correctly.""" | |
| class Input(BaseModel): | |
| """Test input.""" | |
| a: int | |
| b: Optional[str] = None | |
| valid = test_case.pop("valid") | |
| class Config(BaseModel): | |
| tags: Optional[List[str]] = None | |
| model = create_invoke_request_model("namespace", Input, Config) | |
| if valid: | |
| model(**test_case) | |
| else: | |
| with pytest.raises(ValidationError): | |
| model(**test_case) | |
| # Validate batch request | |
| # same structure as input request, but | |
| # 'input' is a list of inputs and is called 'inputs' | |
| batch_model = create_batch_request_model("namespace", Input, Config) | |
| test_case["inputs"] = [test_case.pop("input")] | |
| if valid: | |
| batch_model(**test_case) | |
| else: | |
| with pytest.raises(ValidationError): | |
| batch_model(**test_case) | |
| def test_validation(test_case) -> None: | |
| """Test that the invoke request model is created correctly.""" | |
| class Config(BaseModel): | |
| tags: Optional[List[str]] = None | |
| model = create_invoke_request_model("namespace", test_case.pop("type"), Config) | |
| if test_case["valid"]: | |
| model(**test_case) | |
| else: | |
| with pytest.raises(ValidationError): | |
| model(**test_case) | |
| async def test_invoke_request_with_runnables() -> None: | |
| """Test that the invoke request model is created correctly.""" | |
| runnable = PromptTemplate.from_template("say hello to {name}").configurable_fields( | |
| template=ConfigurableField( | |
| id="template", | |
| name="Template", | |
| description="The template to use for the prompt", | |
| ) | |
| ) | |
| config = runnable.config_schema(include=["tags", "run_name", "configurable"]) | |
| Model = create_invoke_request_model("", runnable.input_schema, config) | |
| assert ( | |
| await _unpack_request_config( | |
| Model( | |
| input={"name": "bob"}, | |
| ).config, | |
| config_keys=[], | |
| model=config, | |
| request=MagicMock(Request), | |
| per_req_config_modifier=lambda x, y: x, | |
| server_config=None, | |
| ) | |
| == {} | |
| ) | |
| # Test that the config is unpacked correctly | |
| request = Model( | |
| input={"name": "bob"}, | |
| config={ | |
| "tags": ["hello"], | |
| "run_name": "run", | |
| "configurable": {"template": "goodbye {name}"}, | |
| }, | |
| ) | |
| assert request.input == {"name": "bob"} | |
| assert request.config.tags == ["hello"] | |
| assert request.config.run_name == "run" | |
| assert isinstance(request.config.configurable, BaseModel) | |
| assert request.config.configurable.dict() == { | |
| "template": "goodbye {name}", | |
| } | |
| assert await _unpack_request_config( | |
| request.config, | |
| config_keys=["configurable"], | |
| model=config, | |
| request=MagicMock(Request), | |
| per_req_config_modifier=lambda x, y: x, | |
| server_config=None, | |
| ) == { | |
| "configurable": {"template": "goodbye {name}"}, | |
| } | |
Xet Storage Details
- Size:
- 5.04 kB
- Xet hash:
- e123c5692570c1120460ee2a2b9996c66014c0d71e0105adfbb94df177228820
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.