64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
import pathlib
|
|
from typing import TypedDict, Any, Union
|
|
|
|
|
|
class OpenAIConfig(TypedDict):
|
|
"""
|
|
The OpenAI section of the configuration file.
|
|
"""
|
|
api_key: str
|
|
model: str
|
|
temperature: float
|
|
max_tokens: int
|
|
top_p: float
|
|
frequency_penalty: float
|
|
presence_penalty: float
|
|
|
|
|
|
def openai_config_valid(conf: dict[str, Union[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.
|
|
"""
|
|
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
|