From f5e97b7cdd2f79e796cdaa1898918d153dbd1802 Mon Sep 17 00:00:00 2001 From: innacampo Date: Sat, 25 Apr 2026 19:31:49 -0400 Subject: [PATCH] Update model references and dependencies to use Ollama embeddings --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- QUICKSTART.md | 5 +++-- README.md | 10 +++++----- docs/technical_reference.md | 18 +++++++++--------- pyproject.toml | 2 +- requirements.txt | 2 +- scripts/setup_project.sh | 7 +++++-- src/selene/settings.py | 22 +++++++++++----------- src/selene/storage/chat_db.py | 2 +- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 7c540ac..589e8ad 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -27,7 +27,7 @@ If applicable, add screenshots or log snippets. - **OS**: [e.g., Ubuntu 22.04, macOS 14] - **Python version**: [e.g., 3.13.1] - **Ollama version**: [e.g., 0.3.x] -- **MedGemma model**: [e.g., MedAIBase/MedGemma1.5:4b] +- **MedGemma model**: [e.g., medgemma:27b] - **Browser** (if UI issue): [e.g., Chrome 120] ## Additional Context diff --git a/QUICKSTART.md b/QUICKSTART.md index d711f52..e63f511 100644 --- a/QUICKSTART.md +++ b/QUICKSTART.md @@ -25,8 +25,9 @@ pip install -e ".[dev]" # 4. Set up pre-commit hooks pre-commit install -# 5. Pull the MedGemma model (one-time) -ollama pull MedAIBase/MedGemma1.5:4b +# 5. Pull the models (one-time) +ollama pull medgemma:27b +ollama pull nomic-embed-text # 6. Run tests to verify setup python -m pytest tests/ -v diff --git a/README.md b/README.md index 9576a64..ee8e8fb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - Daily Attune: capture rest/internal weather/clarity + notes; validated saves with backups. - Chat: contextualized queries, Chroma RAG, past-session recall, streaming MedGemma responses. - Clinical summary: deterministic stats/patterns/risk + single MedGemma call; PDF export via xhtml2pdf. -- Local knowledge base: Chroma collections (medical_docs, chat_history) with SentenceTransformer embeddings. +- Local knowledge base: Chroma collections (medical_docs, chat_history) with Ollama embeddings. - Safety: deterministic risk flags, conservative prompts, low temperature, offline defaults. ## Architecture (brief) @@ -31,7 +31,7 @@ ## Prerequisites - Python 3.11+ -- Ollama running locally with model `MedAIBase/MedGemma1.5:4b` pulled +- Ollama running locally with model `medgemma:27b` pulled - Basic build deps for scientific stack (numpy/scipy/pandas) and xhtml2pdf; install via requirements.txt ## Quick Start @@ -47,7 +47,7 @@ source med_env/bin/activate pip install -e ".[dev]" # 3. Pull model in Ollama (once) -ollama pull MedAIBase/MedGemma1.5:4b +ollama pull medgemma:27b # 4. Launch app streamlit run app.py @@ -90,11 +90,11 @@ See [DIRECTORY_STRUCTURE.md](DIRECTORY_STRUCTURE.md) for the full tree. ## Configuration Highlights - Paths, model names, cache TTLs in [settings.py](src/selene/settings.py): RAG_TOP_K=2, contextualize cache 300s, RAG cache 600s, user context cache 180s. -- Offline/telemetry disabled by default via envs set in settings (TRANSFORMERS_OFFLINE, HF_*_OFFLINE, CHROMA_TELEMETRY=False). +- Offline/telemetry disabled by default via envs set in settings (CHROMA_TELEMETRY=False). - Logging defaults to DEBUG; file logging enabled by default (toggle LOG_TO_FILE). ## Knowledge Base Management -- Chroma collections live under `data/user_data/user_med_db`; embeddings via SentenceTransformer all-MiniLM-L6-v2. +- Chroma collections live under `data/user_data/user_med_db`; embeddings via Ollama nomic-embed-text. - Import/export and collection maintenance via [scripts/update_kb_chroma.py](scripts/update_kb_chroma.py) (keeps collection IDs stable). ## Safety & Guardrails diff --git a/docs/technical_reference.md b/docs/technical_reference.md index 6fffea4..3a3eaa4 100755 --- a/docs/technical_reference.md +++ b/docs/technical_reference.md @@ -111,7 +111,7 @@ ### Vector-based retrieval (RAG) - Knowledge base stored in Chroma `medical_docs`. -- Embeddings via `settings.get_embedding_function()` (SentenceTransformer). +- Embeddings via `settings.get_embedding_function()` (OllamaEmbeddingFunction). - `med_logic.query_knowledge_base()` performs retrieval, formats chunks with `SOURCE` and `SECTION`, and returns (context, sources, full_results). - `update_kb_chroma.py` provides tools to import Chroma exports, clear collection contents safely, and keep collection IDs stable. @@ -139,14 +139,14 @@ ### What never leaves the device (by design) - User profile, pulse entries, chat history, and local knowledge base contents **are not transmitted externally** by the provided code paths. -- Chroma telemetry is disabled (`CHROMA_TELEMETRY = False`). Transformers/HF offline flags are set in `settings.py`. +- Chroma telemetry is disabled (`CHROMA_TELEMETRY = False`). --- ## 4. Reasoning Engine ### Model choice: MedGemma -- Configured model: `settings.LLM_MODEL = "MedAIBase/MedGemma1.5:4b"`. +- Configured model: `settings.LLM_MODEL = "medgemma:27b"`. - Rationale: - Domain-adapted medical reasoning (specialized training for menopause/clinical texts). - Size and local-run compatibility balance for edge devices when deployed via Ollama. @@ -265,7 +265,7 @@ ### Local-only processing guarantees - All user data (profile, pulse, chat) and the knowledge base (Chroma collections) are stored locally under `settings.USER_DATA_DIR` by default. -- The LLM interaction is intended to occur to a local Ollama endpoint. `settings.py` sets `TRANSFORMERS_OFFLINE=1`, `HF_DATASETS_OFFLINE=1`, `HF_HUB_OFFLINE=1`, and `CHROMA_TELEMETRY=False`. +- The LLM interaction is intended to occur to a local Ollama endpoint. `settings.py` sets `CHROMA_TELEMETRY=False`. ### Storage model - File-based: `user_data/pulse_history.json`, `user_data/user_profile.json`. @@ -280,7 +280,7 @@ ### Threat model - Protects against accidental network leakage and default external telemetry: - CHROMA_TELEMETRY disabled. - - HF/transformers offline env flags set. + - Uses local Ollama for embeddings. - Does not protect against: - Host compromise (malicious software or root access). - Misconfiguration where remote LLM endpoints are permitted (must be audited). @@ -343,7 +343,7 @@ ### Resource constraints - LLMs (MedGemma) require significant RAM/VRAM. Ollama hosting may require GPU or optimized CPU inference. -- Sentencetransformer embedding uses CPU if no GPU; embedding large KBs has CPU and memory cost. +- Ollama embedding requires Ollama to be running. ### Performance considerations - Caching: @@ -417,7 +417,7 @@ - The host device is trusted and not compromised. - Ollama and local model files are available locally or via a local host endpoint; the system is not configured to use remote LLM endpoints in the default repo configuration. - Disk-level encryption and OS security are the recommended mechanism for protecting data at rest unless the operator integrates additional encryption. -- CHROMA_TELEMETRY and offline mode flags (`TRANSFORMERS_OFFLINE`, `HF_DATASETS_OFFLINE`, `HF_HUB_OFFLINE`) set in `settings.py` are respected by installed libraries. +- CHROMA_TELEMETRY disabled in `settings.py`. --- @@ -425,8 +425,8 @@ ### Notable config values (in `settings.py`) - `DB_PATH = 'user_data/user_med_db'` -- `LLM_MODEL = 'MedAIBase/MedGemma1.5:4b'` -- `EMBEDDING_MODEL = 'all-MiniLM-L6-v2'` +- `LLM_MODEL = 'medgemma:27b'` +- `EMBEDDING_MODEL = 'nomic-embed-text'` - `RAG_TOP_K = 2` - Cache TTLs: contextualized_queries 300s, rag_cache 600s, user_context_cache 180s. diff --git a/pyproject.toml b/pyproject.toml index 49e17ae..a73c09d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,7 @@ dependencies = [ "chromadb>=0.4", "numpy>=1.24", "scipy>=1.10", - "sentence-transformers>=2.0", + "ollama>=0.3.0", "xhtml2pdf>=0.2.17", "markdown>=3.5", "pymupdf>=1.23", diff --git a/requirements.txt b/requirements.txt index 4891ebd..1aa4479 100755 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ requests>=2.28 chromadb>=0.4 numpy>=1.24 scipy>=1.10 -sentence-transformers>=2.0 +ollama>=0.3.0 xhtml2pdf>=0.2.17 markdown>=3.5 pymupdf>=1.23 \ No newline at end of file diff --git a/scripts/setup_project.sh b/scripts/setup_project.sh index 0ba8492..5512848 100755 --- a/scripts/setup_project.sh +++ b/scripts/setup_project.sh @@ -22,9 +22,12 @@ except ImportError: sys.exit("Python venv module missing. Install python3-venv package.") PY -# 2. Pull MedGemma +# 2. Pull Models echo "Pulling MedGemma model (this may take a few minutes)..." -ollama pull MedAIBase/MedGemma1.5:4b +ollama pull medgemma:27b + +echo "Pulling embedding model..." +ollama pull nomic-embed-text # Verify Ollama service responds (helpful if serve is not running) if command -v curl >/dev/null 2>&1; then diff --git a/src/selene/settings.py b/src/selene/settings.py index 2213681..f11812e 100755 --- a/src/selene/settings.py +++ b/src/selene/settings.py @@ -8,11 +8,10 @@ import os -# Force libraries to only look for local files - must be set before -# importing transformers, sentence_transformers, or huggingface_hub. -os.environ.setdefault("TRANSFORMERS_OFFLINE", "1") -os.environ.setdefault("HF_DATASETS_OFFLINE", "1") -os.environ.setdefault("HF_HUB_OFFLINE", "1") +# Disable offline HF flags since we no longer use Hugging Face for embeddings +# os.environ.setdefault("TRANSFORMERS_OFFLINE", "1") +# os.environ.setdefault("HF_DATASETS_OFFLINE", "1") +# os.environ.setdefault("HF_HUB_OFFLINE", "1") from pathlib import Path # ============================================================================ @@ -39,7 +38,7 @@ DB_PATH = str(USER_DATA_DIR / "user_med_db") MEDICAL_DOCS_COLLECTION = "medical_docs" CHAT_HISTORY_COLLECTION = "chat_history" -EMBEDDING_MODEL = "all-MiniLM-L6-v2" +EMBEDDING_MODEL = "nomic-embed-text" CHROMA_TELEMETRY = False # ============================================================================ @@ -48,7 +47,7 @@ OLLAMA_BASE_URL = "http://localhost:11434" OLLAMA_TIMEOUT = 60 -LLM_MODEL = "MedAIBase/MedGemma1.5:4b" +LLM_MODEL = "medgemma:27b" # ============================================================================ # RAG & Chat History Retrieval @@ -101,16 +100,17 @@ def get_embedding_function(): """Return a cached ChromaDB-compatible embedding function. - Uses ChromaDB's built-in ``SentenceTransformerEmbeddingFunction`` so the - persisted collection metadata stays consistent (type ``sentence_transformer``). + Uses ChromaDB's built-in ``OllamaEmbeddingFunction`` so the + persisted collection metadata stays consistent. The model is loaded once and reused across all callers (Streamlit app and CLI tools like update_kb_chroma.py). """ global _embedding_function_instance if _embedding_function_instance is None: - from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction + from chromadb.utils.embedding_functions import OllamaEmbeddingFunction - _embedding_function_instance = SentenceTransformerEmbeddingFunction( + _embedding_function_instance = OllamaEmbeddingFunction( + url=f"{OLLAMA_BASE_URL}/api/embeddings", model_name=EMBEDDING_MODEL ) return _embedding_function_instance diff --git a/src/selene/storage/chat_db.py b/src/selene/storage/chat_db.py index 8556a6b..2ffb2ff 100755 --- a/src/selene/storage/chat_db.py +++ b/src/selene/storage/chat_db.py @@ -45,7 +45,7 @@ class ChatDBConfig: def _get_chat_client(): """ Returns a (collection, None) tuple on success, cached for the app lifetime. - Uses the same SentenceTransformer embedding model as med_logic so both + Uses the same Ollama embedding model as med_logic so both collections live in the same vector space - consistent and efficient. """ try: