Skip to content

Commit 23706ea

Browse files
committed
update
1 parent 5ae74f9 commit 23706ea

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .config import * # noqa
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def tests_dir() -> Path:
8+
"""Location to this `tests` folder."""
9+
return Path(__file__).parent
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
from unittest.mock import patch
3+
4+
import pytest
5+
6+
from customer_support import Settings
7+
from customer_support.config import ENV_PREFIX
8+
9+
10+
def test_settings() -> None:
11+
"""Test `Settings` class can use environment variables."""
12+
with patch.dict(
13+
os.environ,
14+
{
15+
f"{ENV_PREFIX}API_PORT": "8000",
16+
f"{ENV_PREFIX}LLM_PORT": "8001",
17+
f"{ENV_PREFIX}API_HOST": "0.0.0.0",
18+
f"{ENV_PREFIX}LLM_HOST": "0.0.0.0",
19+
},
20+
):
21+
settings = Settings()
22+
assert settings.api.port == 8000
23+
assert settings.llm.port == 8001
24+
assert settings.api.host == "0.0.0.0"
25+
assert settings.llm.host == "0.0.0.0"
26+
27+
28+
if __name__ == "__main__":
29+
pytest.main([__file__, "-x", "-s"])

0 commit comments

Comments
 (0)