New web interface, move llm engine to separate process
This commit is contained in:
262
sia/config.py
262
sia/config.py
@@ -1,9 +1,10 @@
|
||||
from dataclasses import dataclass
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Dict
|
||||
import argparse
|
||||
import os
|
||||
import tomli as tomllib
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
@@ -59,170 +60,27 @@ class Config:
|
||||
parser.add_argument(
|
||||
'--static-files',
|
||||
type=Path,
|
||||
default=self._parse_optional_path('SIA_STATIC_FILES', '/root/static/'),
|
||||
default=os.getenv('SIA_STATIC_FILES', '/root/static/'),
|
||||
help='Path to static web files (default: /root/static/, env: SIA_STATIC_FILES)'
|
||||
)
|
||||
|
||||
# Local LLM configuration
|
||||
# LLM configuration
|
||||
parser.add_argument(
|
||||
'--local-enable',
|
||||
action='store_true',
|
||||
default=self._parse_bool_env('SIA_LOCAL_ENABLED', False),
|
||||
help='Enable local LLM engine (env: SIA_LOCAL_ENABLED)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--local-model',
|
||||
type=str,
|
||||
default=os.getenv('SIA_LOCAL_MODEL', '/root/models/current'),
|
||||
help='Path to local model directory (default: /root/models/current, env: SIA_LOCAL_MODEL)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--local-temperature',
|
||||
type=float,
|
||||
default=float(os.getenv('SIA_LOCAL_TEMPERATURE', '0.1')),
|
||||
help='Local LLM temperature (default: 0.1, env: SIA_LOCAL_TEMPERATURE)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--local-token-limit',
|
||||
type=int,
|
||||
default=int(os.getenv('SIA_LOCAL_TOKEN_LIMIT', '2048')),
|
||||
help='Local LLM token limit (env: SIA_LOCAL_TOKEN_LIMIT)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--local-api-key',
|
||||
type=str,
|
||||
default=os.getenv('SIA_LOCAL_API_KEY'),
|
||||
help='API key for local models (env: SIA_LOCAL_API_KEY)'
|
||||
)
|
||||
|
||||
# OpenAI configuration
|
||||
parser.add_argument(
|
||||
'--openai-enable',
|
||||
action='store_true',
|
||||
default=self._parse_bool_env('SIA_OPENAI_ENABLED', False),
|
||||
help='Enable OpenAI LLM engine (env: SIA_OPENAI_ENABLED)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--openai-model',
|
||||
type=str,
|
||||
default=os.getenv('SIA_OPENAI_MODEL', 'gpt-4o'),
|
||||
help='OpenAI model name (default: gpt-4o, env: SIA_OPENAI_MODEL)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--openai-temperature',
|
||||
type=float,
|
||||
default=float(os.getenv('SIA_OPENAI_TEMPERATURE', '0.1')),
|
||||
help='OpenAI temperature (default: 0.1, env: SIA_OPENAI_TEMPERATURE)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--openai-token-limit',
|
||||
type=int,
|
||||
default=int(os.getenv('SIA_OPENAI_TOKEN_LIMIT', '4096')),
|
||||
help='OpenAI token limit (env: SIA_OPENAI_TOKEN_LIMIT)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--openai-api-key',
|
||||
type=str,
|
||||
default=os.getenv('SIA_OPENAI_API_KEY'),
|
||||
help='OpenAI API key (env: SIA_OPENAI_API_KEY)'
|
||||
)
|
||||
|
||||
# Hugging Face configuration
|
||||
parser.add_argument(
|
||||
'--hf-enable',
|
||||
action='store_true',
|
||||
default=self._parse_bool_env('SIA_HF_ENABLED', False),
|
||||
help='Enable Hugging Face LLM engine (env: SIA_HF_ENABLED)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hf-model',
|
||||
type=str,
|
||||
default=os.getenv('SIA_HF_MODEL'),
|
||||
help='Hugging Face model name (env: SIA_HF_MODEL)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hf-temperature',
|
||||
type=float,
|
||||
default=float(os.getenv('SIA_HF_TEMPERATURE', '0.1')),
|
||||
help='Hugging Face temperature (default: 0.1, env: SIA_HF_TEMPERATURE)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hf-api-key',
|
||||
type=str,
|
||||
default=os.getenv('SIA_HF_API_KEY'),
|
||||
help='Hugging Face API key (env: SIA_HF_API_KEY)'
|
||||
)
|
||||
|
||||
# Mistral configuration
|
||||
parser.add_argument(
|
||||
'--mistral-enable',
|
||||
action='store_true',
|
||||
default=self._parse_bool_env('SIA_MISTRAL_ENABLED', False),
|
||||
help='Enable Mistral LLM engine (env: SIA_MISTRAL_ENABLED)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--mistral-model',
|
||||
type=str,
|
||||
default=os.getenv('SIA_MISTRAL_MODEL'),
|
||||
help='Mistral model name (env: SIA_MISTRAL_MODEL)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--mistral-temperature',
|
||||
type=float,
|
||||
default=float(os.getenv('SIA_MISTRAL_TEMPERATURE', '0.1')),
|
||||
help='Mistral temperature (default: 0.1, env: SIA_MISTRAL_TEMPERATURE)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--mistral-token-limit',
|
||||
type=int,
|
||||
default=int(os.getenv('SIA_MISTRAL_TOKEN_LIMIT', '4096')),
|
||||
help='Mistral token limit (env: SIA_MISTRAL_TOKEN_LIMIT)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--mistral-api-key',
|
||||
type=str,
|
||||
default=os.getenv('SIA_MISTRAL_API_KEY'),
|
||||
help='Mistral API key (env: SIA_MISTRAL_API_KEY)'
|
||||
)
|
||||
# QwQ configuration
|
||||
parser.add_argument(
|
||||
'--qwq-enable',
|
||||
action='store_true',
|
||||
default=self._parse_bool_env('SIA_QWQ_ENABLED', False),
|
||||
help='Enable QwQ LLM engine (env: SIA_QWQ_ENABLED)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--qwq-model',
|
||||
type=str,
|
||||
default=os.getenv('SIA_QWQ_MODEL', '/root/models/current'),
|
||||
help='Path to QwQ model or HF model ID (default: /root/models/current, env: SIA_QWQ_MODEL)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--qwq-temperature',
|
||||
type=float,
|
||||
default=float(os.getenv('SIA_QWQ_TEMPERATURE', '0.6')),
|
||||
help='QwQ temperature (default: 0.1, env: SIA_QWQ_TEMPERATURE)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--qwq-token-limit',
|
||||
type=int,
|
||||
default=int(os.getenv('SIA_QWQ_TOKEN_LIMIT', '4096')),
|
||||
help='QwQ token limit (0 for model default, env: SIA_QWQ_TOKEN_LIMIT)'
|
||||
'--llm-config',
|
||||
type=Path,
|
||||
default=os.getenv('SIA_LLM_CONFIG', '/root/sia/llm_config.toml'),
|
||||
help='Path to TOML configuration file for LLMs (default: /root/sia/llm_config.toml, env: SIA_LLM_CONFIG)'
|
||||
)
|
||||
|
||||
self.args = parser.parse_args()
|
||||
with open(self.llm_config, "rb") as f:
|
||||
self._llm_configs = tomllib.load(f)
|
||||
|
||||
def _parse_bool_env(self, env_var: str, default: bool) -> bool:
|
||||
val = os.getenv(env_var)
|
||||
if val is None:
|
||||
return default
|
||||
return val.lower() in ('true', '1', 'yes', 'on')
|
||||
|
||||
def _parse_optional_path(self, env_var: str, default: Optional[Path]) -> Optional[Path]:
|
||||
val = os.getenv(env_var)
|
||||
if val is None:
|
||||
return default
|
||||
return Path(val)
|
||||
|
||||
# Core properties
|
||||
@property
|
||||
@@ -258,100 +116,12 @@ class Config:
|
||||
def static_files(self) -> Path:
|
||||
return self.args.static_files
|
||||
|
||||
# Local LLM properties
|
||||
# LLM properties
|
||||
@property
|
||||
def local_enabled(self) -> bool:
|
||||
return self.args.local_enable
|
||||
|
||||
@property
|
||||
def local_model(self) -> str:
|
||||
return self.args.local_model
|
||||
|
||||
@property
|
||||
def local_temperature(self) -> float:
|
||||
return self.args.local_temperature
|
||||
|
||||
@property
|
||||
def local_token_limit(self) -> int:
|
||||
return self.args.local_token_limit
|
||||
def llm_config(self) -> Path:
|
||||
return self.args.llm_config
|
||||
|
||||
@property
|
||||
def local_api_key(self) -> Optional[str]:
|
||||
return self.args.local_api_key
|
||||
|
||||
# OpenAI properties
|
||||
@property
|
||||
def openai_enabled(self) -> bool:
|
||||
return self.args.openai_enable
|
||||
|
||||
@property
|
||||
def openai_model(self) -> str:
|
||||
return self.args.openai_model
|
||||
|
||||
@property
|
||||
def openai_temperature(self) -> float:
|
||||
return self.args.openai_temperature
|
||||
|
||||
@property
|
||||
def openai_token_limit(self) -> int:
|
||||
return self.args.openai_token_limit
|
||||
|
||||
@property
|
||||
def openai_api_key(self) -> Optional[str]:
|
||||
return self.args.openai_api_key
|
||||
|
||||
# Hugging Face properties
|
||||
@property
|
||||
def hf_enabled(self) -> bool:
|
||||
return self.args.hf_enable
|
||||
|
||||
@property
|
||||
def hf_model(self) -> str:
|
||||
return self.args.hf_model
|
||||
|
||||
@property
|
||||
def hf_temperature(self) -> float:
|
||||
return self.args.hf_temperature
|
||||
|
||||
@property
|
||||
def hf_api_key(self) -> Optional[str]:
|
||||
return self.args.hf_api_key
|
||||
|
||||
# Mistral properties
|
||||
@property
|
||||
def mistral_enabled(self) -> bool:
|
||||
return self.args.mistral_enable
|
||||
|
||||
@property
|
||||
def mistral_model(self) -> str:
|
||||
return self.args.mistral_model
|
||||
|
||||
@property
|
||||
def mistral_temperature(self) -> float:
|
||||
return self.args.mistral_temperature
|
||||
|
||||
@property
|
||||
def mistral_token_limit(self) -> int:
|
||||
return self.args.mistral_token_limit
|
||||
|
||||
@property
|
||||
def mistral_api_key(self) -> Optional[str]:
|
||||
return self.args.mistral_api_key
|
||||
|
||||
# QwQ properties
|
||||
@property
|
||||
def qwq_enabled(self) -> bool:
|
||||
return self.args.qwq_enable
|
||||
|
||||
@property
|
||||
def qwq_model(self) -> str:
|
||||
return self.args.qwq_model
|
||||
|
||||
@property
|
||||
def qwq_temperature(self) -> float:
|
||||
return self.args.qwq_temperature
|
||||
|
||||
@property
|
||||
def qwq_token_limit(self) -> Optional[int]:
|
||||
# Return None if 0 to use model default
|
||||
return self.args.qwq_token_limit if self.args.qwq_token_limit > 0 else None
|
||||
def llms(self) -> Dict[str, str]:
|
||||
"""Get only the enabled LLM configurations."""
|
||||
return self._llm_configs
|
||||
Reference in New Issue
Block a user