Skip to content

Commit 75dac35

Browse files
parfeniukinkDmytro Parfeniuk
andauthored
👔 Project settings is now encapsulated into guidellm.config module (#17)
* The module is currently has only `__init__` file * All `os.getenv` replaced with `pydantic_settings` facade * `GUIDELLM__` env prefix is set --------- Co-authored-by: Dmytro Parfeniuk <[email protected]>
1 parent 88e6c12 commit 75dac35

File tree

11 files changed

+154
-152
lines changed

11 files changed

+154
-152
lines changed

‎.env.example

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎DEVELOPING.md

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@ This document aims to provide developers with all the necessary information to c
1010
### Prerequisites
1111

1212
Before you begin, ensure you have the following installed:
13+
1314
- Python 3.8 or higher
1415
- `pip` (Python package installer)
1516
- `git` (version control system)
1617

1718
### Installation
1819

1920
1. Clone the repository:
20-
```bash
21-
git clone https://github.com/neuralmagic/guidellm.git
22-
cd guidellm
23-
```
21+
22+
```bash
23+
git clone https://github.com/neuralmagic/guidellm.git
24+
cd guidellm
25+
```
2426

2527
2. Install the required dependencies:
26-
```bash
27-
pip install -e .[dev]
28-
```
28+
```bash
29+
pip install -e .[dev]
30+
```
2931

3032
## Project Structure
3133

3234
The project follows a standard Python project structure:
35+
3336
```plaintext
3437
guidellm/
3538
├── src/
@@ -51,14 +54,15 @@ guidellm/
5154
To set up your development environment, follow these steps:
5255

5356
1. Install pre-commit hooks:
54-
```bash
55-
pre-commit install
56-
```
57+
58+
```bash
59+
pre-commit install
60+
```
5761

5862
2. Ensure all dependencies are installed:
59-
```bash
60-
pip install -e .[dev]
61-
```
63+
```bash
64+
pip install -e .[dev]
65+
```
6266

6367
## Code Quality and Style Guidelines
6468

@@ -70,39 +74,39 @@ Ruff is used for linting and formatting checks.
7074

7175
- Configuration is in `pyproject.toml`.
7276
- To run Ruff:
73-
```bash
74-
ruff check src tests
75-
```
77+
```bash
78+
ruff check src tests
79+
```
7680

7781
### Isort
7882

7983
Isort is used for sorting imports.
8084

8185
- Configuration is in `pyproject.toml`.
8286
- To sort imports:
83-
```bash
84-
isort src tests
85-
```
87+
```bash
88+
isort src tests
89+
```
8690

8791
### Flake8
8892

8993
Flake8 is used for linting.
9094

9195
- Configuration is in `tox.ini`.
9296
- To run Flake8:
93-
```bash
94-
flake8 src tests --max-line-length 88
95-
```
97+
```bash
98+
flake8 src tests --max-line-length 88
99+
```
96100

97101
### MyPy
98102

99103
MyPy is used for type checking.
100104

101105
- Configuration is in `pyproject.toml`.
102106
- To run MyPy:
103-
```bash
104-
mypy src/guidellm
105-
```
107+
```bash
108+
mypy src/guidellm
109+
```
106110

107111
## Testing
108112

@@ -111,6 +115,7 @@ We use `pytest` for running tests.
111115
### Running All Tests
112116

113117
To run all tests:
118+
114119
```bash
115120
tox
116121
```
@@ -150,6 +155,7 @@ tox -e test-e2e
150155
```
151156

152157
## Formatting, Linting, and Type Checking
158+
153159
### Running Quality Checks (Linting)
154160

155161
To run quality checks (ruff, isort, flake8, mypy), use the following command:
@@ -204,6 +210,24 @@ Please refer to the CONTRIBUTING.md file for guidelines on how to contribute to
204210

205211
Please refer to the MAINTAINERS file for maintenance guidelines and contact information.
206212

213+
## Project configuration
214+
215+
The project configuartion is powered by _[`🔗 pydantic-settings`](https://docs.pydantic.dev/latest/concepts/pydantic_settings/)_
216+
217+
The project configuration entry point is represented by lazy-loaded `settigns` singleton object ( `src/config/__init__` )
218+
219+
The project is fully configurable with environment variables. With that configuration set you can load parameters to `LoggingSettings()` by using environment variables. Just run `export GUIDELLM__LOGGING__DISABLED=true` or `export GUIDELLM__LOGGING__NESTED=another_value` respectfully. The nesting delimiter is `__`.
220+
221+
### Environment variables
222+
223+
| Name | Default value | Description |
224+
| ------------------------------ | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
225+
| `GUIDELLM__LOGGING__DISABLED` | `False` | Determines whether logger for the `guidellm` package is disabled or not |
226+
| `GUIDELLM__LOGGING__LOG_LEVEL` | `INFO` | The level of `guidellm` package logging |
227+
| `GUIDELLM__LOGGING__LOG_FILE` | `guidellm.log` | The name of a log file |
228+
| `GUIDELLM__OPENAI__BASE_URL` | `http://localhost:8080` | The address to the **OpenAI-compatible** server.<br><br>OpenAI live base url is `https://api.openai.com/v1` |
229+
| `GUIDELLM__OPENAI__API_KEY` | `invalid` | Corresponds to the **OpenAI-compatible** server API key.<br><br>If you look for the live key - check [this link](https://platform.openai.com/api-keys). |
230+
207231
## Contact and Support
208232

209233
If you need help or have any questions, please open an issue on GitHub or contact us at [email protected].

‎pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies = [
3131
"numpy",
3232
"openai",
3333
"pydantic>=2.0.0",
34+
"pydantic-settings>=2.0.0",
3435
"pyyaml>=6.0.0",
3536
"requests",
3637
"transformers",

‎src/config/__init__.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from typing import Optional
2+
3+
from pydantic import BaseModel
4+
from pydantic_settings import BaseSettings, SettingsConfigDict
5+
6+
__all__ = ["settings"]
7+
8+
9+
class LoggingSettings(BaseModel):
10+
disabled: bool = False
11+
clear_loggers: bool = True
12+
console_log_level: str = "INFO"
13+
log_file: Optional[str] = None
14+
log_file_level: Optional[str] = None
15+
16+
17+
class OpenAISettings(BaseModel):
18+
19+
# OpenAI API key.
20+
api_key: str = "invalid"
21+
22+
# OpenAI-compatible server URL
23+
# NOTE: The default value is default address of llama.cpp web server
24+
base_url: str = "http://localhost:8080"
25+
26+
27+
class Settings(BaseSettings):
28+
"""
29+
All the settings are powered by pydantic_settings and could be
30+
populated from the .env file.
31+
32+
The format to populate the settings is next
33+
34+
```sh
35+
export GUIDELLM__LOGGING__DISABLED=true
36+
export GUIDELLM__OPENAI__API_KEY=******
37+
```
38+
39+
"""
40+
41+
model_config = SettingsConfigDict(
42+
env_prefix="GUIDELLM",
43+
env_nested_delimiter="__",
44+
env_file=".env",
45+
extra="ignore",
46+
)
47+
48+
logging: LoggingSettings = LoggingSettings()
49+
openai: OpenAISettings = OpenAISettings()
50+
51+
52+
settings = Settings()

‎src/guidellm/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
evaluating and benchmarking large language models (LLMs).
44
"""
55

6-
from .logger import LoggerConfig, configure_logger, logger
6+
from .logger import configure_logger, logger
77

8-
__all__ = [
9-
"logger",
10-
"configure_logger",
11-
"LoggerConfig",
12-
]
8+
__all__ = ["logger", "configure_logger"]

‎src/guidellm/backend/openai.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import functools
2-
import os
32
from typing import Any, Dict, Generator, List, Optional
43

54
from loguru import logger
65
from openai import OpenAI, Stream
76
from openai.types import Completion
87
from transformers import AutoTokenizer
98

9+
from config import settings
1010
from guidellm.backend import Backend, BackendEngine, GenerativeResponse
1111
from guidellm.core import TextGenerationRequest
1212

@@ -47,18 +47,16 @@ def __init__(
4747

4848
self.request_args = request_args
4949

50-
if not (_api_key := (openai_api_key or os.getenv("OPENAI_API_KEY", None))):
50+
if not (_api_key := (openai_api_key or settings.openai.api_key)):
5151
raise ValueError(
52-
"`OPENAI_API_KEY` environment variable "
52+
"`GUIDELLM__OPENAI__API_KEY` environment variable "
5353
"or --openai-api-key CLI parameter "
5454
"must be specify for the OpenAI backend"
5555
)
5656

57-
if not (
58-
_base_url := (internal_callback_url or os.getenv("OPENAI_BASE_URL", None))
59-
):
57+
if not (_base_url := (internal_callback_url or settings.openai.base_url)):
6058
raise ValueError(
61-
"`OPENAI_BASE_URL` environment variable "
59+
"`GUIDELLM__OPENAI__BASE_URL` environment variable "
6260
"or --openai-base-url CLI parameter "
6361
"must be specify for the OpenAI backend"
6462
)

0 commit comments

Comments
 (0)