-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathsettings.py
More file actions
377 lines (298 loc) · 11.5 KB
/
settings.py
File metadata and controls
377 lines (298 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pydantic import (
Field,
HttpUrl,
AfterValidator,
BaseModel,
ConfigDict,
field_serializer,
model_validator,
)
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Optional, Union, Annotated, Self, List, Dict, Any, Callable
from dremioai.config.tools import ToolType
from enum import auto, StrEnum
from pathlib import Path
from yaml import safe_load, add_representer, dump
from functools import reduce
from operator import ior
from shutil import which
from contextvars import ContextVar, copy_context
from os import environ
from importlib.util import find_spec
from datetime import datetime
def _resolve_tools_settings(server_mode: Union[ToolType, int, str]) -> ToolType:
if isinstance(server_mode, str):
try:
server_mode = reduce(
ior, [ToolType[m.upper()] for m in server_mode.split(",")]
)
except KeyError:
return _resolve_tools_settings(int(server_mode))
if isinstance(server_mode, int):
return ToolType(server_mode)
return server_mode
class Tools(BaseModel):
server_mode: Annotated[
Optional[Union[ToolType, int, str]], AfterValidator(_resolve_tools_settings)
] = Field(default=ToolType.FOR_SELF)
model_config = ConfigDict(validate_assignment=True, use_enum_values=True)
@field_serializer("server_mode")
def serialize_server_mode(self, server_mode: ToolType):
return ",".join(m.name for m in ToolType if m & server_mode)
class DremioCloudUri(StrEnum):
PROD = auto()
PRODEMEA = auto()
def _resolve_dremio_uri(
uri: Union[str, DremioCloudUri, HttpUrl],
) -> Union[HttpUrl, str]:
if isinstance(uri, str):
try:
uri = DremioCloudUri[uri.upper()]
except KeyError:
uri = HttpUrl(uri)
if isinstance(uri, DremioCloudUri):
match uri:
case DremioCloudUri.PROD:
return f"https://api.dremio.cloud"
case DremioCloudUri.PRODEMEA:
return f"https://api.eu.dremio.cloud"
return uri
elif isinstance(uri, HttpUrl):
uri = str(uri)
return uri.rstrip("/")
def _resolve_token_file(pat: str) -> str:
return (
Path(pat[1:]).expanduser().read_text().strip() if pat.startswith("@") else pat
)
class Model(StrEnum):
ollama = auto()
openai = auto()
class OAuth2(BaseModel):
client_id: str
refresh_token: Optional[str] = None
dremio_user_identifier: Optional[str] = None
expiry: Optional[datetime] = None
model_config = ConfigDict(validate_assignment=True)
@property
def has_expired(self) -> bool:
return self.expiry is not None and self.expiry < datetime.now()
class Dremio(BaseModel):
uri: Annotated[
Union[str, HttpUrl, DremioCloudUri], AfterValidator(_resolve_dremio_uri)
]
raw_pat: Optional[str] = Field(default=None, alias="pat")
username: Optional[str] = None
password: Optional[str] = None
project_id: Optional[str] = None
enable_experimental: Optional[bool] = False # enable experimental tools
oauth2: Optional[OAuth2] = None
allow_dml: Optional[bool] = False
model_config = ConfigDict(validate_assignment=True)
@field_serializer("raw_pat")
def serialize_pat(self, pat: str):
return self.raw_pat if pat != self.raw_pat else pat
@model_validator(mode='after')
def validate_auth_method(self) -> 'Dremio':
"""Validate authentication method configuration"""
has_pat = self.raw_pat is not None
has_user_pass = self.username is not None and self.password is not None
# Allow configurations with no authentication for backward compatibility
# The actual authentication requirement will be enforced at runtime
if has_pat and has_user_pass:
raise ValueError("Cannot specify both 'pat' and 'username/password' authentication methods")
if (self.username is None) != (self.password is None):
raise ValueError("Both 'username' and 'password' must be provided together")
return self
@property
def oauth_configured(self) -> bool:
return self.oauth2 is not None
@property
def oauth_supported(self) -> bool:
return self.project_id is not None
@property
def has_username_password(self) -> bool:
"""Check if username/password authentication is configured"""
return self.username is not None and self.password is not None
# @field_validator("_pat", mode="wrap")
# @classmethod
# def validate_pat(cls, v: str, handler: ValidatorFunctionWrapHandler) -> str:
# v = _resolve_token_file(v)
# return handler(v)
@property
def pat(self) -> str:
if v := getattr(self, "_pat_resolved", None):
return v
if self.raw_pat is not None and self.raw_pat.startswith("@"):
self._pat_resolved = _resolve_token_file(self.raw_pat)
return self._pat_resolved
return self.raw_pat
@pat.setter
def pat(self, v: str):
self.raw_pat = v
self._pat_resolved = None
class OpenAi(BaseModel):
api_key: Annotated[str, AfterValidator(_resolve_token_file)] = None
model: Optional[str] = Field(default="gpt-4o")
org: Optional[str] = Field(default=None)
model_config = ConfigDict(validate_assignment=True)
class Ollama(BaseModel):
model: Optional[str] = Field(default="llama3.1")
model_config = ConfigDict(validate_assignment=True)
class LangChain(BaseModel):
llm: Optional[Model] = None
openai: Optional[OpenAi] = Field(default_factory=OpenAi)
ollama: Optional[Ollama] = Field(default=None)
model_config = ConfigDict(validate_assignment=True)
class Prometheus(BaseModel):
uri: Union[HttpUrl, str]
token: str
model_config = ConfigDict(validate_assignment=True)
def _resolve_executable(executable: str) -> str:
executable = Path(executable).expanduser()
if not executable.is_absolute():
if (c := which(executable)) is not None:
executable = Path(c)
executable = executable.resolve()
if not executable.is_file():
raise FileNotFoundError(f"Command {executable} not found.")
return str(executable)
class MCPServer(BaseModel):
command: Annotated[str, AfterValidator(_resolve_executable)]
args: Optional[List[str]] = Field(default_factory=list)
env: Optional[Dict[str, str]] = Field(default_factory=dict)
model_config = ConfigDict(validate_assignment=True)
class Anthropic(BaseModel):
api_key: Annotated[str, AfterValidator(_resolve_token_file)] = None
chat_model: Optional[str] = Field(default=None)
model_config = ConfigDict(validate_assignment=True)
class BeeAI(BaseModel):
mcp_server: Optional[MCPServer] = Field(default=None)
sliding_memory_size: Optional[int] = Field(default=10)
anthropic: Optional[Anthropic] = Field(default=None)
openai: Optional[OpenAi] = Field(default=None)
ollama: Optional[Ollama] = Field(default=None)
model_config = ConfigDict(validate_assignment=True)
class Settings(BaseSettings):
dremio: Optional[Dremio] = Field(default=None)
tools: Optional[Tools] = Field(default_factory=Tools)
prometheus: Optional[Prometheus] = Field(default=None)
langchain: Optional[LangChain] = Field(default=None)
beeai: Optional[BeeAI] = Field(default=None)
model_config = SettingsConfigDict(
env_file=".env",
env_nested_delimiter="_",
env_extra="allow",
use_enum_values=True,
)
def with_overrides(self, overrides: Dict[str, Any]) -> Self:
def set_values(aparts: List[str], value: Any, obj: Any):
if len(aparts) == 1 and hasattr(obj, aparts[0]):
setattr(obj, aparts[0], value)
elif hasattr(obj, aparts[0]):
set_values(aparts[1:], value, getattr(obj, aparts[0]))
for aparts, value in [
(attr.split("."), value)
for attr, value in overrides.items()
if value is not None
]:
set_values(aparts, value, self)
return self
_settings: ContextVar[Settings] = ContextVar("settings", default=None)
# the default config is ~/.config/dremioai/config.yaml, use it if it exists
def default_config() -> Path:
_top = "dremioai"
if (_top := find_spec(__name__)) and _top.name:
_top = _top.name.split(".")[0]
return (
Path(environ.get("XDG_CONFIG_HOME", Path.home() / ".config"))
/ _top
/ "config.yaml"
)
# configures the settings using the given config file and overwrites the global
# settings instance if force is True
def configure(cfg: Union[str, Path] = None, force=False) -> ContextVar[Settings]:
global _settings
if force and isinstance(_settings.get(), Settings):
old = _settings.get()
try:
_settings.set(None)
configure(cfg, force=False)
except:
# don't replace the old if there is an issue setting the new value
_settings.set(old)
raise
if isinstance(cfg, str):
cfg = Path(cfg)
if cfg is None:
cfg = default_config()
if not cfg.exists():
cfg.parent.mkdir(parents=True, exist_ok=True)
cfg.touch()
with cfg.open() as f:
s = safe_load(f)
_settings.set(Settings.model_validate(s if s else {}))
return _settings
# Get the current settings instance if one has been configured. If not try
# to configure it using the default config file. If that fails, create a new
# empty settings instance.
def instance() -> Settings | None:
global _settings
if not isinstance(_settings.get(), Settings):
try:
configure() # use default config, if exists
except FileNotFoundError:
# no default config, create a new default one
_settings.set(Settings())
return _settings.get()
async def run_with(
func: Callable,
overrides: Optional[Dict[str, Any]] = {},
args: Optional[List[Any]] = [],
kw: Optional[Dict[str, Any]] = {},
) -> Any:
global _settings
async def _call():
tok = _settings.set(instance().model_copy(deep=True).with_overrides(overrides))
try:
return await func(*args, **kw)
finally:
_settings.reset(tok)
ctx = copy_context()
return await _call()
def write_settings(
cfg: Path = None, inst: Settings = None, dry_run: bool = False
) -> str | None:
if cfg is None:
cfg = default_config()
if not isinstance(inst, Settings):
inst = instance()
d = inst.model_dump(
exclude_none=True, mode="json", exclude_unset=True, by_alias=True
)
add_representer(
str,
lambda dumper, data: dumper.represent_scalar(
"tag:yaml.org,2002:str", data, style=('"' if "@" in data else None)
),
)
if dry_run:
return dump(d)
if not cfg.exists() or not cfg.parent.exists():
cfg.parent.mkdir(parents=True, exist_ok=True)
with cfg.open("w") as f:
dump(d, f)