from pydantic import BaseModel, Field from typing import List, Optional, Union, Any import sys from pydantic_settings import BaseSettings class CliConfig(BaseSettings, cli_parse_args=True, cli_use_class_docs_for_groups=True): CONFIG_FILE: str = Field("./config.local.yaml", description="Config file path") CLI_CONFIG = CliConfig(CONFIG_FILE="./config.local.yaml") class SamplerConfig(BaseModel): """Default sampler configuration for each model.""" max_tokens: int = Field(512, description="Maximum number of tokens to generate.") temperature: float = Field(1.0, description="Sampling temperature.") top_p: float = Field(0.3, description="Top-p sampling threshold.") presence_penalty: float = Field(0.5, description="Presence penalty.") count_penalty: float = Field(0.5, description="Count penalty.") penalty_decay: float = Field(0.996, description="Penalty decay factor.") stop: List[str] = Field(["\n\n"], description="List of stop sequences.") stop_tokens: List[int] = Field([0], description="List of stop tokens.") ALLOW_WEB_SEARCH: Optional[bool] = Field(None, description="Per-sampler override for allowing web search. If None, falls back to model/global.") ALLOW_FILE_TOOL: Optional[bool] = Field(None, description="Per-sampler override for allowing file tools (e.g., file_read). If None, falls back to model/global.") ALLOW_TOOLS: Optional[bool] = Field(None, description="Per-sampler override for allowing server-side tools. If None, falls back to model/global.") ALLOW_REASONING: Optional[bool] = Field(None, description="Per-sampler override for allowing built-in reasoning. If None, falls back to model/global.") # UI flags (non-functional in server, included so UI clients can show controls) SHOW_WEB_SEARCH_BUTTON: Optional[bool] = Field(None, description="Whether to show the web-search toggle in the client UI for this sampler") SHOW_FILE_UPLOAD_BUTTON: Optional[bool] = Field(None, description="Whether to show the file-upload control in the client UI for this sampler") SHOW_REASONING_TOGGLE: Optional[bool] = Field(None, description="Whether to show the reasoning (think) toggle in the client UI for this sampler") UI_STYLE: Optional[str] = Field(None, description="UI style hint that clients may use to render controls (example: 'whatsapp' or 'compact')") # Per-sampler tool allow overrides ALLOW_FETCH_URL: Optional[bool] = Field(None, description="Per-sampler override for allowing fetch_url") ALLOW_SUMMARIZE: Optional[bool] = Field(None, description="Per-sampler override for allowing summarize") ALLOW_KEYWORDS: Optional[bool] = Field(None, description="Per-sampler override for allowing keywords extraction") ALLOW_SENTIMENT: Optional[bool] = Field(None, description="Per-sampler override for allowing sentiment analysis") ALLOW_TRANSLATE: Optional[bool] = Field(None, description="Per-sampler override for allowing translation") ALLOW_SPELL_CHECK: Optional[bool] = Field(None, description="Per-sampler override for allowing spell check") ALLOW_FORMAT_CODE: Optional[bool] = Field(None, description="Per-sampler override for allowing code formatting") ALLOW_EXPLAIN_CODE: Optional[bool] = Field(None, description="Per-sampler override for allowing code explanation") SHOW_FETCH_URL_BUTTON: Optional[bool] = Field(None, description="Whether to show the fetch_url button for this sampler") SHOW_SUMMARIZE_BUTTON: Optional[bool] = Field(None, description="Whether to show the summarize button for this sampler") SHOW_KEYWORDS_BUTTON: Optional[bool] = Field(None, description="Whether to show the keywords button for this sampler") SHOW_SENTIMENT_BUTTON: Optional[bool] = Field(None, description="Whether to show the sentiment button for this sampler") SHOW_TRANSLATE_BUTTON: Optional[bool] = Field(None, description="Whether to show the translate button for this sampler") SHOW_SPELL_CHECK_BUTTON: Optional[bool] = Field(None, description="Whether to show the spell_check button for this sampler") SHOW_FORMAT_CODE_BUTTON: Optional[bool] = Field(None, description="Whether to show the format_code button for this sampler") SHOW_EXPLAIN_CODE_BUTTON: Optional[bool] = Field(None, description="Whether to show the explain_code button for this sampler") # New tools (duplicates removed) ALLOW_FORMAT_CODE: Optional[bool] = Field(None, description="Per-sampler override for allow code format") ALLOW_EXPLAIN_CODE: Optional[bool] = Field(None, description="Per-sampler override for allow code explanation") SHOW_TRANSLATE_BUTTON: Optional[bool] = Field(None, description="Whether to show translate button in UI for this sampler") SHOW_SPELL_CHECK_BUTTON: Optional[bool] = Field(None, description="Whether to show spellcheck button in UI for this sampler") SHOW_FORMAT_CODE_BUTTON: Optional[bool] = Field(None, description="Whether to show format code button in UI for this sampler") SHOW_EXPLAIN_CODE_BUTTON: Optional[bool] = Field(None, description="Whether to show explain code button in UI for this sampler") class ModelConfig(BaseModel): """Configuration for each individual model.""" SERVICE_NAME: str = Field(..., description="Service name of the model.") MODEL_FILE_PATH: Optional[str] = Field(None, description="Model file path.") DOWNLOAD_MODEL_FILE_NAME: Optional[str] = Field( None, description="Model name, should end with .pth" ) DOWNLOAD_MODEL_REPO_ID: Optional[str] = Field( None, description="Model repository ID on Hugging Face Hub." ) DOWNLOAD_MODEL_DIR: Optional[str] = Field( "./models", description="Directory to download the model to." ) REASONING: bool = Field( False, description="Whether reasoning is enabled for this model." ) DEFAULT_CHAT: bool = Field(False, description="Whether this model is the default chat model.") DEFAULT_REASONING: bool = Field(False, description="Whether this model is the default reasoning model.") DEFAULT_SAMPLER: SamplerConfig = Field( SamplerConfig( max_tokens=512, temperature=1.0, top_p=0.3, presence_penalty=0.5, count_penalty=0.5, penalty_decay=0.996, stop=["\n\n"], stop_tokens=[0], ALLOW_WEB_SEARCH=None, ALLOW_TOOLS=None, ALLOW_REASONING=None, ALLOW_FILE_TOOL=None, SHOW_WEB_SEARCH_BUTTON=None, SHOW_FILE_UPLOAD_BUTTON=None, SHOW_REASONING_TOGGLE=None, UI_STYLE=None, ALLOW_FETCH_URL=None, ALLOW_SUMMARIZE=None, ALLOW_KEYWORDS=None, ALLOW_SENTIMENT=None, ALLOW_TRANSLATE=None, ALLOW_SPELL_CHECK=None, ALLOW_FORMAT_CODE=None, ALLOW_EXPLAIN_CODE=None, SHOW_FETCH_URL_BUTTON=None, SHOW_SUMMARIZE_BUTTON=None, SHOW_KEYWORDS_BUTTON=None, SHOW_SENTIMENT_BUTTON=None, SHOW_TRANSLATE_BUTTON=None, SHOW_SPELL_CHECK_BUTTON=None, SHOW_FORMAT_CODE_BUTTON=None, SHOW_EXPLAIN_CODE_BUTTON=None, ), description="Default sampler configuration for this model." ) VOCAB: str = Field("rwkv_vocab_v20230424", description="Vocab Name") # Allow or disallow server-side features on a per-model basis ALLOW_WEB_SEARCH: bool = Field(True, description="Whether this model supports web search injection") ALLOW_TOOLS: bool = Field(True, description="Whether this model supports server-side tools execution") ALLOW_REASONING: bool = Field(True, description="Whether this model supports built-in reasoning (in-process)") ALLOW_FILE_TOOL: bool = Field(True, description="Whether this model supports file-based tools (file_upload/file_read)") ALLOW_FETCH_URL: bool = Field(True, description="Whether this model supports fetch_url tool") ALLOW_SUMMARIZE: bool = Field(True, description="Whether this model supports summarize tool") ALLOW_KEYWORDS: bool = Field(True, description="Whether this model supports keywords extraction tool") ALLOW_SENTIMENT: bool = Field(True, description="Whether this model supports sentiment analysis tool") ALLOW_TRANSLATE: bool = Field(True, description="Whether this model supports translate tool") ALLOW_SPELL_CHECK: bool = Field(True, description="Whether this model supports spell check tool") ALLOW_FORMAT_CODE: bool = Field(True, description="Whether this model supports code formatting tool") ALLOW_EXPLAIN_CODE: bool = Field(True, description="Whether this model supports explain code tool") # remove duplicate ALLOW_TRANSLATE/ALLOW_SPELLCHECK ALLOW_FORMAT_CODE: bool = Field(True, description="Whether this model supports code formatting tool") ALLOW_EXPLAIN_CODE: bool = Field(True, description="Whether this model supports code explanation tool") # UI flags for the model that the client may use to show/hide controls SHOW_WEB_SEARCH_BUTTON: bool = Field(True, description="Whether to show the web search toggle for this model in client UIs") SHOW_FILE_UPLOAD_BUTTON: bool = Field(True, description="Whether to show a file upload button for this model in client UIs") SHOW_REASONING_TOGGLE: bool = Field(True, description="Whether to show the reasoning toggle for this model in client UIs") SHOW_FETCH_URL_BUTTON: bool = Field(True, description="Whether to show the fetch_url button for this model in client UIs") SHOW_SUMMARIZE_BUTTON: bool = Field(True, description="Whether to show the summarize button for this model in client UIs") SHOW_KEYWORDS_BUTTON: bool = Field(True, description="Whether to show the keywords button for this model in client UIs") SHOW_SENTIMENT_BUTTON: bool = Field(True, description="Whether to show the sentiment button for this model in client UIs") SHOW_TRANSLATE_BUTTON: bool = Field(True, description="Whether to show the translate button for this model in client UIs") SHOW_SPELL_CHECK_BUTTON: bool = Field(True, description="Whether to show the spell_check button for this model in client UIs") SHOW_FORMAT_CODE_BUTTON: bool = Field(True, description="Whether to show the format_code button for this model in client UIs") SHOW_EXPLAIN_CODE_BUTTON: bool = Field(True, description="Whether to show the explain_code button for this model in client UIs") # remove duplicate SHOW_TRANSLATE_BUTTON/SHOW_SPELLCHECK_BUTTON SHOW_FORMAT_CODE_BUTTON: bool = Field(True, description="Whether to show format code button for this model in client UIs") SHOW_EXPLAIN_CODE_BUTTON: bool = Field(True, description="Whether to show explain code button for this model in client UIs") class RootConfig(BaseModel): """Root configuration for the RWKV service.""" HOST: Optional[str] = Field("127.0.0.1", description="Host IP address to bind to.") PORT: Optional[int] = Field(8000, description="Port number to listen on.") STRATEGY: str = Field("cpu", description="Strategy for model execution (e.g., 'cuda fp16').") RWKV_CUDA_ON: bool = Field(False, description="Whether to enable RWKV CUDA kernel.") CHUNK_LEN: int = Field(256, description="Chunk length for processing.") MODELS: List[ModelConfig] = Field(..., description="List of model configurations.") # Additional defaults for auto behavior DEFAULT_STREAM: bool = Field(True, description="Whether streaming is enabled by default") AUTO_ENABLE_TOOLS: bool = Field(True, description="Whether to try auto-enabling tools based on intent") AUTO_ENABLE_REASONING: bool = Field(True, description="Whether to auto-enable reasoning when needed") AUTO_ENABLE_WEB_SEARCH: bool = Field(True, description="Whether to auto-enable web search based on intent") ENABLE_TOOLS_BY_DEFAULT: bool = Field(False, description="Whether tools are enabled by default (without explicit request)") ENABLE_WEB_SEARCH_BY_DEFAULT: bool = Field(True, description="Whether web search is enabled by default") ENABLE_REASONING_BY_DEFAULT: bool = Field(True, description="Whether model reasoning is enabled by default when requested/supported") # State store persistence STATE_STORE_PATH: str = Field("./state_store.json", description="Path to persist streaming/resume state store") STATE_STORE_FLUSH_INTERVAL: int = Field(5, description="Seconds between background flushes to the state store file") STATE_STORE_SAVE_ON_UPDATE: bool = Field(True, description="Whether to save the state store to disk immediately when updated") # File uploads / tools UPLOAD_DIR: str = Field("./uploads", description="Directory to store uploaded files") ALLOW_FILE_TOOL_BY_DEFAULT: bool = Field(True, description="Whether file-based tools are enabled by default") # Public artifacts: allow enabling a public file REST API if desired. Default: False to keep API surface minimal ALLOW_PUBLIC_UPLOADS: bool = Field(False, description="Allow public file REST endpoints (disabled by default to reduce public surface)") # UI flags for the root server. These flags are advisory only and do not enable functionality. SHOW_WEB_SEARCH_BUTTON_BY_DEFAULT: bool = Field(True, description="Whether to show web search toggle by default in clients") SHOW_FILE_UPLOAD_BUTTON_BY_DEFAULT: bool = Field(True, description="Whether to show file-upload control by default in clients") SHOW_REASONING_TOGGLE_BY_DEFAULT: bool = Field(True, description="Whether to show reasoning toggle by default in clients") # Safety and numeric limits MAX_GENERATION_TOKENS_LIMIT: int = Field(64000, description="Absolute maximum tokens a generation can request when reasoning is enabled") MAX_TOKENS_PER_REQUEST: int = Field(65536, description="Absolute maximum number of tokens permitted per request (prompt + completion).") MIN_TEMPERATURE: float = Field(0.0, description="Minimum permitted temperature for sampling") MAX_TEMPERATURE: float = Field(2.0, description="Maximum permitted temperature for sampling") MIN_TOP_P: float = Field(0.0, description="Minimum permitted top_p") MAX_TOP_P: float = Field(1.0, description="Maximum permitted top_p") MAX_UPLOAD_SIZE_BYTES: int = Field(10 * 1024 * 1024, description="Maximum size of an upload in bytes (default 10MB)") import yaml try: with open(CLI_CONFIG.CONFIG_FILE, "r", encoding="utf-8") as f: CONFIG = RootConfig.model_validate(yaml.safe_load(f.read())) except Exception as e: print(f"Pydantic Model Validation Failed: {e}") # Exit with non-zero to indicate error when config is invalid sys.exit(1)