Coverage for src/fastoai/settings.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-06 09:34 +0800

1from functools import lru_cache 

2from pathlib import Path 

3from typing import Annotated 

4 

5from pydantic import Field 

6from pydantic_settings import BaseSettings 

7 

8 

9class OpenAISettings(BaseSettings, env_prefix="openai_", frozen=True): # type: ignore 

10 """OpenAI settings.""" 

11 

12 api_key: str 

13 base_url: str = "https://api.openai.com/v1" 

14 

15 

16class Settings(BaseSettings, env_prefix="fastoai_", frozen=True): # type: ignore 

17 """Settings.""" 

18 

19 openai: Annotated[OpenAISettings, Field(default_factory=OpenAISettings)] # type: ignore 

20 base_url: str = "http://127.0.0.1:8000" 

21 database_url: str = "sqlite+aiosqlite:///" 

22 upload_dir: Path = Path.home() / ".fastoai" / "uploads" 

23 generate_models: bool = False 

24 

25 def model_post_init(self, __context): 

26 self.upload_dir.mkdir(parents=True, exist_ok=True) 

27 

28 

29@lru_cache 

30def get_settings() -> Settings: 

31 """Get settings.""" 

32 return Settings() # type: ignore