Add model settings and Markdown chat export
This commit is contained in:
1 parent
12f2a1655f
commit
e6db824792
5 files changed
+317
-36
No files matched your search
+42
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import os
|
||||
from dataclasses import asdict
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
DEFAULT_PATH = Path('sloppy.json')
|
||||
|
||||
|
||||
def load_settings(path=DEFAULT_PATH):
|
||||
path = Path(path)
|
||||
if not path.exists():
|
||||
return {'models': {}, 'selected': {'ai1': '', 'ai2': ''}}
|
||||
data = json.loads(path.read_text(encoding='utf-8'))
|
||||
if not isinstance(data, dict) or not isinstance(data.get('models', {}), dict):
|
||||
raise ValueError('sloppy.json must contain a models object')
|
||||
data.setdefault('models', {})
|
||||
data.setdefault('selected', {'ai1': '', 'ai2': ''})
|
||||
return data
|
||||
|
||||
|
||||
def save_settings(data, path=DEFAULT_PATH):
|
||||
path = Path(path)
|
||||
temporary = path.with_name(path.name + '.tmp')
|
||||
temporary.write_text(json.dumps(data, indent=2, ensure_ascii=False) + '\n', encoding='utf-8')
|
||||
os.replace(temporary, path)
|
||||
if os.name != 'nt':
|
||||
path.chmod(0o600)
|
||||
|
||||
|
||||
def model_from_settings(data, identifier):
|
||||
values = data['models'][identifier]
|
||||
return {key: values.get(key, default) for key, default in (
|
||||
('name', identifier), ('base_url', ''), ('api_key', ''),
|
||||
('model', ''), ('system_prompt', 'You are a helpful assistant.'))}
|
||||
|
||||
|
||||
def store_model(data, identifier, config):
|
||||
identifier = identifier.strip()
|
||||
if not identifier or not all((config.name.strip(), config.base_url.strip(), config.model.strip())):
|
||||
raise ValueError('ID, name, base URL, and model are required')
|
||||
data['models'][identifier] = asdict(config)
|
||||
Reference in new issue
Block a user