Skip to content

mistral_common.experimental.app.main

cli()

Mistral-common CLI.

Source code in src/mistral_common/experimental/app/main.py
@click.group()
def cli() -> None:
    r"""Mistral-common CLI."""
    pass

create_app(tokenizer, validation_mode=ValidationMode.test, engine_url='http://127.0.0.1:8080', engine_backend=EngineBackend.llama_cpp, timeout=60)

Create a Mistral-common FastAPI app with the given tokenizer and validation mode.

The app exposes tokenize, decode, and health routes backed by the given tokenizer, and can proxy completions to an external engine.

Parameters:

Name Type Description Default
tokenizer str | Path | MistralTokenizer

Path to a tokenizer file, or an already-loaded MistralTokenizer instance.

required
validation_mode ValidationMode

The validation mode applied to incoming requests. Only used when tokenizer is a path.

test
engine_url str

The URL of the engine API used for chat completions.

'http://127.0.0.1:8080'
engine_backend EngineBackend

The backend type of the engine API.

llama_cpp
timeout TimeoutType

The timeout of the engine API in seconds: a positive finite number, or a string that converts to one.

60

Returns:

Type Description
FastAPI

The configured FastAPI application.

Raises:

Type Description
ValueError

If tokenizer is neither a path nor a MistralTokenizer, or if timeout is not a positive finite number.

Source code in src/mistral_common/experimental/app/main.py
def create_app(
    tokenizer: str | Path | MistralTokenizer,
    validation_mode: ValidationMode = ValidationMode.test,
    engine_url: str = "http://127.0.0.1:8080",
    engine_backend: EngineBackend = EngineBackend.llama_cpp,
    timeout: TimeoutType = 60,
) -> FastAPI:
    r"""Create a Mistral-common FastAPI app with the given tokenizer and validation mode.

    The app exposes tokenize, decode, and health routes backed by the given
    tokenizer, and can proxy completions to an external engine.

    Args:
        tokenizer: Path to a tokenizer file, or an already-loaded
            MistralTokenizer instance.
        validation_mode: The validation mode applied to incoming requests.
            Only used when tokenizer is a path.
        engine_url: The URL of the engine API used for chat completions.
        engine_backend: The backend type of the engine API.
        timeout: The timeout of the engine API in seconds: a positive finite number, or a string that
            converts to one.

    Returns:
        The configured FastAPI application.

    Raises:
        ValueError: If tokenizer is neither a path nor a MistralTokenizer, or if timeout is not a positive
            finite number.
    """
    if not isinstance(tokenizer, (MistralTokenizer, str, Path)):
        raise ValueError("Tokenizer must be a path or a MistralTokenizer instance.")

    app = FastAPI()
    app.include_router(tokenize_router)
    app.include_router(decode_router)
    app.include_router(main_router)

    @lru_cache
    def get_settings_override() -> Settings:
        settings = Settings(
            engine_url=engine_url,
            engine_backend=engine_backend,
            timeout=resolve_engine_timeout(timeout),
        )
        if isinstance(tokenizer, MistralTokenizer):
            settings.tokenizer = tokenizer
        else:
            settings._load_tokenizer(tokenizer, validation_mode)
        return settings

    get_settings_override()
    app.dependency_overrides[get_settings] = get_settings_override

    return app

serve(tokenizer_path, validation_mode=ValidationMode.test, host='127.0.0.1', port=0, engine_url='http://127.0.0.1:8080', engine_backend=EngineBackend.llama_cpp.value, timeout=60)

Serve the Mistral-common API with the given tokenizer path and validation mode.

Source code in src/mistral_common/experimental/app/main.py
@cli.command(name="serve", context_settings={"auto_envvar_prefix": "UVICORN"})
@click.argument("tokenizer_path", type=str)
@click.argument(
    "validation_mode",
    type=click.Choice([mode.value for mode in ValidationMode], case_sensitive=False),
    default=ValidationMode.test.value,
)
@click.option(
    "--host",
    type=str,
    default="127.0.0.1",
    help="Mistral-common API host",
    show_default=True,
)
@click.option(
    "--port",
    type=int,
    default=0,
    help="Mistral-common API port",
    show_default=True,
)
@click.option(
    "--engine-url",
    type=str,
    default="http://127.0.0.1:8080",
    help="Engine URL",
    show_default=True,
)
@click.option(
    "--engine-backend",
    type=click.Choice([mode.value for mode in EngineBackend], case_sensitive=False),
    default=EngineBackend.llama_cpp.value,
    help="Engine API backend",
    show_default=True,
)
@click.option(
    "--timeout",
    type=float,
    default=60,
    callback=_validate_timeout_option,
    help="Engine timeout in seconds",
    show_default=True,
)
def serve(
    tokenizer_path: str | Path,
    validation_mode: ValidationMode | str = ValidationMode.test,
    host: str = "127.0.0.1",
    port: int = 0,
    engine_url: str = "http://127.0.0.1:8080",
    engine_backend: str = EngineBackend.llama_cpp.value,
    timeout: float = 60,
) -> None:
    r"""Serve the Mistral-common API with the given tokenizer path and validation mode."""
    app = create_app(
        tokenizer=tokenizer_path,
        validation_mode=ValidationMode(validation_mode),
        engine_url=engine_url,
        engine_backend=EngineBackend(engine_backend),
        timeout=timeout,
    )
    uvicorn.run(app, host=host, port=port)