Spaces:
Running
Running
Commit
·
20c7bad
1
Parent(s):
b50fffd
refactor: centralize embedding configuration in Settings
Browse files- Add openai_embedding_model and local_embedding_model to Settings
- Update LlamaIndexRAGService to use settings.openai_embedding_model
- Update EmbeddingService to use settings.local_embedding_model
- Both services now read from centralized config instead of hardcoding
- Constraint (OpenAI-only for cloud embeddings) is now explicit in config
- src/services/embeddings.py +9 -3
- src/services/llamaindex_rag.py +4 -3
- src/utils/config.py +11 -0
src/services/embeddings.py
CHANGED
|
@@ -11,18 +11,24 @@ import chromadb
|
|
| 11 |
import structlog
|
| 12 |
from sentence_transformers import SentenceTransformer
|
| 13 |
|
|
|
|
| 14 |
from src.utils.models import Evidence
|
| 15 |
|
| 16 |
|
| 17 |
class EmbeddingService:
|
| 18 |
-
"""Handles text embedding and vector storage.
|
| 19 |
|
| 20 |
All embedding operations run in a thread pool to avoid blocking
|
| 21 |
the async event loop.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
|
| 24 |
-
def __init__(self, model_name: str =
|
| 25 |
-
self.
|
|
|
|
| 26 |
self._client = chromadb.Client() # In-memory for hackathon
|
| 27 |
self._collection = self._client.create_collection(
|
| 28 |
name="evidence", metadata={"hnsw:space": "cosine"}
|
|
|
|
| 11 |
import structlog
|
| 12 |
from sentence_transformers import SentenceTransformer
|
| 13 |
|
| 14 |
+
from src.utils.config import settings
|
| 15 |
from src.utils.models import Evidence
|
| 16 |
|
| 17 |
|
| 18 |
class EmbeddingService:
|
| 19 |
+
"""Handles text embedding and vector storage using local sentence-transformers.
|
| 20 |
|
| 21 |
All embedding operations run in a thread pool to avoid blocking
|
| 22 |
the async event loop.
|
| 23 |
+
|
| 24 |
+
Note:
|
| 25 |
+
Uses local sentence-transformers models (no API key required).
|
| 26 |
+
Model is configured via settings.local_embedding_model.
|
| 27 |
"""
|
| 28 |
|
| 29 |
+
def __init__(self, model_name: str | None = None):
|
| 30 |
+
self._model_name = model_name or settings.local_embedding_model
|
| 31 |
+
self._model = SentenceTransformer(self._model_name)
|
| 32 |
self._client = chromadb.Client() # In-memory for hackathon
|
| 33 |
self._collection = self._client.create_collection(
|
| 34 |
name="evidence", metadata={"hnsw:space": "cosine"}
|
src/services/llamaindex_rag.py
CHANGED
|
@@ -27,7 +27,7 @@ class LlamaIndexRAGService:
|
|
| 27 |
self,
|
| 28 |
collection_name: str = "deepcritical_evidence",
|
| 29 |
persist_dir: str | None = None,
|
| 30 |
-
embedding_model: str =
|
| 31 |
similarity_top_k: int = 5,
|
| 32 |
) -> None:
|
| 33 |
"""
|
|
@@ -36,7 +36,7 @@ class LlamaIndexRAGService:
|
|
| 36 |
Args:
|
| 37 |
collection_name: Name of the ChromaDB collection
|
| 38 |
persist_dir: Directory to persist ChromaDB data
|
| 39 |
-
embedding_model: OpenAI embedding model to
|
| 40 |
similarity_top_k: Number of top results to retrieve
|
| 41 |
"""
|
| 42 |
# Lazy import - only when instantiated
|
|
@@ -64,6 +64,7 @@ class LlamaIndexRAGService:
|
|
| 64 |
self.collection_name = collection_name
|
| 65 |
self.persist_dir = persist_dir or settings.chroma_db_path
|
| 66 |
self.similarity_top_k = similarity_top_k
|
|
|
|
| 67 |
|
| 68 |
# Validate API key before use
|
| 69 |
if not settings.openai_api_key:
|
|
@@ -75,7 +76,7 @@ class LlamaIndexRAGService:
|
|
| 75 |
api_key=settings.openai_api_key,
|
| 76 |
)
|
| 77 |
self._Settings.embed_model = OpenAIEmbedding(
|
| 78 |
-
model=embedding_model,
|
| 79 |
api_key=settings.openai_api_key,
|
| 80 |
)
|
| 81 |
|
|
|
|
| 27 |
self,
|
| 28 |
collection_name: str = "deepcritical_evidence",
|
| 29 |
persist_dir: str | None = None,
|
| 30 |
+
embedding_model: str | None = None,
|
| 31 |
similarity_top_k: int = 5,
|
| 32 |
) -> None:
|
| 33 |
"""
|
|
|
|
| 36 |
Args:
|
| 37 |
collection_name: Name of the ChromaDB collection
|
| 38 |
persist_dir: Directory to persist ChromaDB data
|
| 39 |
+
embedding_model: OpenAI embedding model (defaults to settings.openai_embedding_model)
|
| 40 |
similarity_top_k: Number of top results to retrieve
|
| 41 |
"""
|
| 42 |
# Lazy import - only when instantiated
|
|
|
|
| 64 |
self.collection_name = collection_name
|
| 65 |
self.persist_dir = persist_dir or settings.chroma_db_path
|
| 66 |
self.similarity_top_k = similarity_top_k
|
| 67 |
+
self.embedding_model = embedding_model or settings.openai_embedding_model
|
| 68 |
|
| 69 |
# Validate API key before use
|
| 70 |
if not settings.openai_api_key:
|
|
|
|
| 76 |
api_key=settings.openai_api_key,
|
| 77 |
)
|
| 78 |
self._Settings.embed_model = OpenAIEmbedding(
|
| 79 |
+
model=self.embedding_model,
|
| 80 |
api_key=settings.openai_api_key,
|
| 81 |
)
|
| 82 |
|
src/utils/config.py
CHANGED
|
@@ -29,6 +29,17 @@ class Settings(BaseSettings):
|
|
| 29 |
openai_model: str = Field(default="gpt-4o", description="OpenAI model name")
|
| 30 |
anthropic_model: str = Field(default="claude-sonnet-4-20250514", description="Anthropic model")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# PubMed Configuration
|
| 33 |
ncbi_api_key: str | None = Field(
|
| 34 |
default=None, description="NCBI API key for higher rate limits"
|
|
|
|
| 29 |
openai_model: str = Field(default="gpt-4o", description="OpenAI model name")
|
| 30 |
anthropic_model: str = Field(default="claude-sonnet-4-20250514", description="Anthropic model")
|
| 31 |
|
| 32 |
+
# Embedding Configuration
|
| 33 |
+
# Note: OpenAI embeddings require OPENAI_API_KEY (Anthropic has no embeddings API)
|
| 34 |
+
openai_embedding_model: str = Field(
|
| 35 |
+
default="text-embedding-3-small",
|
| 36 |
+
description="OpenAI embedding model (used by LlamaIndex RAG)",
|
| 37 |
+
)
|
| 38 |
+
local_embedding_model: str = Field(
|
| 39 |
+
default="all-MiniLM-L6-v2",
|
| 40 |
+
description="Local sentence-transformers model (used by EmbeddingService)",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
# PubMed Configuration
|
| 44 |
ncbi_api_key: str | None = Field(
|
| 45 |
default=None, description="NCBI API key for higher rate limits"
|