configuration: added validation
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from typing import TypedDict
|
||||
import pathlib
|
||||
from typing import TypedDict, Any
|
||||
|
||||
|
||||
class OpenAIConfig(TypedDict):
|
||||
@@ -14,6 +15,25 @@ class OpenAIConfig(TypedDict):
|
||||
presence_penalty: float
|
||||
|
||||
|
||||
def openai_config_valid(conf: dict[str, str | float | int]) -> bool:
|
||||
"""
|
||||
Checks if the given Open AI configuration dict is complete
|
||||
and contains valid types and values.
|
||||
"""
|
||||
try:
|
||||
str(conf['api_key'])
|
||||
str(conf['model'])
|
||||
int(conf['max_tokens'])
|
||||
float(conf['temperature'])
|
||||
float(conf['top_p'])
|
||||
float(conf['frequency_penalty'])
|
||||
float(conf['presence_penalty'])
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"OpenAI configuration is invalid: {e}")
|
||||
return False
|
||||
|
||||
|
||||
class Config(TypedDict):
|
||||
"""
|
||||
The configuration file structure.
|
||||
@@ -21,3 +41,23 @@ class Config(TypedDict):
|
||||
system: str
|
||||
db: str
|
||||
openai: OpenAIConfig
|
||||
|
||||
|
||||
def config_valid(conf: dict[str, Any]) -> bool:
|
||||
"""
|
||||
Checks if the given configuration dict is complete
|
||||
and contains valid types and values.
|
||||
"""
|
||||
try:
|
||||
str(conf['system'])
|
||||
pathlib.Path(str(conf['db']))
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Configuration is invalid: {e}")
|
||||
return False
|
||||
if 'openai' in conf:
|
||||
return openai_config_valid(conf['openai'])
|
||||
else:
|
||||
# required as long as we only support OpenAI
|
||||
print("Section 'openai' is missing in the configuration!")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user