File size: 4,235 Bytes
585cfd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f805e9
 
 
 
585cfd5
6f805e9
585cfd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
config.py β€” Central configuration for the Personal Document Intel & Archiver
All paths, model settings, and user-configurable patient info live here.
"""

import json
import os
from pathlib import Path

BASE_DIR = Path(__file__).parent

# ── Directory layout ────────────────────────────────────────────────────────
DATA_DIR       = BASE_DIR / "data"
DOCUMENTS_DIR  = DATA_DIR / "documents"
CACHE_DIR      = BASE_DIR / "cache"
MODELS_DIR     = BASE_DIR / "models"
ASSETS_DIR     = BASE_DIR / "assets"

# ── Model settings ──────────────────────────────────────────────────────────
MODEL_PATH      = MODELS_DIR / "qwen2.5-3b-instruct-q4_k_m.gguf"
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
CHUNK_SIZE      = 500
CHUNK_OVERLAP   = 50
TOP_K_RETRIEVAL = 3
TEMPERATURE     = 0.1
MAX_TOKENS      = 512
CONTEXT_SIZE    = 4096

# ── LM Studio endpoint ────────────────────────────────────────────────────────
LM_STUDIO_URL = "http://192.168.1.160:1234/v1/completions"

# ── FAISS / chunk cache ─────────────────────────────────────────────────────
FAISS_INDEX_PATH = CACHE_DIR / "index.faiss"
CHUNKS_JSON_PATH = CACHE_DIR / "chunks.json"

# ── Data store paths ────────────────────────────────────────────────────────
MEDICATIONS_JSON   = DATA_DIR / "medications.json"
APPOINTMENTS_JSON  = DATA_DIR / "appointments.json"
FOOD_CHART_JSON    = DATA_DIR / "food_chart.json"
PATIENT_CONFIG_JSON = DATA_DIR / "patient_config.json"

# ── Patient info defaults (overridden by PATIENT_CONFIG_JSON at runtime) ────
_PATIENT_DEFAULTS = {
    "patient_name":    "",
    "patient_dob":     "",
    "insurance_info":  "",
    "model_path":      str(MODEL_PATH),
    "welcome_dismissed": False,
    "active_profile_id": "",
}


def _ensure_dirs():
    """Create all required directories if they don't exist."""
    for d in [DATA_DIR, DOCUMENTS_DIR, CACHE_DIR, MODELS_DIR, ASSETS_DIR]:
        d.mkdir(parents=True, exist_ok=True)


def _ensure_model():
    """
    Stub: previously auto-downloaded a local GGUF for llama-cpp-python.
    Now we use the Hugging Face Inference API (hosted Qwen 2.5 7B), which
    needs no local model file. Kept as a no-op for backwards compatibility
    with any callers that still expect this function to exist.
    """
    return


def load_patient_config() -> dict:
    """Load patient config from disk, falling back to defaults."""
    _ensure_dirs()
    if PATIENT_CONFIG_JSON.exists():
        try:
            with open(PATIENT_CONFIG_JSON, "r", encoding="utf-8") as f:
                data = json.load(f)
            # Merge so any new keys from defaults are present
            merged = {**_PATIENT_DEFAULTS, **data}
            return merged
        except Exception:
            pass
    return dict(_PATIENT_DEFAULTS)


def save_patient_config(cfg: dict):
    """Persist patient config to disk."""
    _ensure_dirs()
    with open(PATIENT_CONFIG_JSON, "w", encoding="utf-8") as f:
        json.dump(cfg, f, indent=2)


def get_model_path() -> Path:
    """Return the active model path (from saved config or default)."""
    cfg = load_patient_config()
    return Path(cfg.get("model_path", str(MODEL_PATH)))


def load_active_profile_id() -> str:
    """Return persisted active care profile id, or empty string."""
    return (load_patient_config().get("active_profile_id") or "").strip()


def save_active_profile_id(profile_id: str | None):
    """Persist the active care profile id (empty string clears the session)."""
    cfg = load_patient_config()
    cfg["active_profile_id"] = profile_id or ""
    save_patient_config(cfg)


# Ensure directories exist when module is first imported
_ensure_dirs()
_ensure_model()