|
| 1 | +__all__ = ["Settings"] |
| 2 | + |
| 3 | + |
| 4 | +from pydantic import Field, IPvAnyAddress |
| 5 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 6 | + |
| 7 | +ENV_PREFIX = "CUSTOMER_SUPPORT_" |
| 8 | +COMMON_CONFIG = { |
| 9 | + "case_sensitive": False, # from the environment |
| 10 | + "env_file_encoding": "utf-8", |
| 11 | + "env_file": ".env", |
| 12 | + "extra": "ignore", |
| 13 | + "env_nested_delimiter": "_", |
| 14 | + "validate_assignment": True, |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +class APISettings(BaseSettings): |
| 19 | + """API settings.""" |
| 20 | + |
| 21 | + model_config = SettingsConfigDict( |
| 22 | + env_prefix=ENV_PREFIX + "API_", |
| 23 | + **COMMON_CONFIG, # type: ignore |
| 24 | + ) |
| 25 | + |
| 26 | + host: IPvAnyAddress = Field( |
| 27 | + "0.0.0.0", |
| 28 | + description="Webservice bind host. Defaults to 0.0.0.0 to accept connections from all interfaces.", |
| 29 | + ) |
| 30 | + port: int = Field( |
| 31 | + 8000, |
| 32 | + description="Webservice port.", |
| 33 | + ge=0, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +class LLMSettings(BaseSettings): |
| 38 | + """LLM settings.""" |
| 39 | + |
| 40 | + model_config = SettingsConfigDict( |
| 41 | + env_prefix=ENV_PREFIX + "LLM_", |
| 42 | + **COMMON_CONFIG, # type: ignore |
| 43 | + ) |
| 44 | + |
| 45 | + hf_token_key: str = Field( |
| 46 | + "HF_TOKEN", |
| 47 | + description="Name of the environment variable that contains the HuggingFace token.", |
| 48 | + ) |
| 49 | + default_llm_id: str = Field( |
| 50 | + "TinyLlama/TinyLlama_v1.1", |
| 51 | + description="Default LLM.", |
| 52 | + ) |
| 53 | + default_llm_mlx_id: str = Field( |
| 54 | + "mlx-community/quantized-gemma-2b-it", |
| 55 | + description="Default LLM MLX. For more all available LLMs, see: https://huggingface.co/mlx-community", |
| 56 | + ) |
| 57 | + system_prompt: str = Field( |
| 58 | + """ |
| 59 | + You are the customer support for a baby-sitting app. |
| 60 | + """, |
| 61 | + description="System prompt for LLM.", |
| 62 | + ) |
| 63 | + bind_host: IPvAnyAddress = Field( |
| 64 | + "0.0.0.0", |
| 65 | + description="Host the LLM server binds to. Defaults to 0.0.0.0 to listen on all interfaces.", |
| 66 | + ) |
| 67 | + host: IPvAnyAddress = Field( |
| 68 | + "127.0.0.1", |
| 69 | + description=( |
| 70 | + "Host the API container connects to when calling the LLM server. " |
| 71 | + "Defaults to 127.0.0.1 (loopback), which works when both services run on the same machine." |
| 72 | + ), |
| 73 | + ) |
| 74 | + port: int = Field( |
| 75 | + 8080, |
| 76 | + description="Port number for the LLM server.", |
| 77 | + ge=0, |
| 78 | + ) |
| 79 | + |
| 80 | + @property |
| 81 | + def llm_server(self) -> str: |
| 82 | + """LLM server URL.""" |
| 83 | + return f"http://{self.host}:{self.port}/v1" |
| 84 | + |
| 85 | + |
| 86 | +class Settings(BaseSettings): |
| 87 | + """Library cofiguration.""" |
| 88 | + |
| 89 | + model_config = SettingsConfigDict( |
| 90 | + env_prefix=ENV_PREFIX, |
| 91 | + **COMMON_CONFIG, # type: ignore |
| 92 | + ) |
| 93 | + |
| 94 | + api: APISettings = Field( |
| 95 | + default_factory=APISettings, |
| 96 | + description="API settings.", |
| 97 | + ) |
| 98 | + llm: LLMSettings = Field( |
| 99 | + default_factory=LLMSettings, |
| 100 | + description="LLM settings.", |
| 101 | + ) |
0 commit comments