Repository navigation
feat(embeddings): opt-in Ollama embedding backend for GPU acceleration #982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,31 @@ | |
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_embedding_function(): | ||
| """Return an embedding function based on env, or ``None`` for Chroma's default. | ||
|
|
||
| Set ``EMBEDDING_PROVIDER=ollama`` to route embeddings through a local | ||
| Ollama server (GPU-accelerated). Tunables: | ||
|
|
||
| - ``OLLAMA_URL`` (default ``http://localhost:11434``) - base URL | ||
| - ``OLLAMA_EMBED_MODEL`` (default ``nomic-embed-text``) | ||
| - ``OLLAMA_EMBED_TIMEOUT`` seconds (default ``60``) | ||
|
|
||
| Returning ``None`` keeps ChromaDB's ``DefaultEmbeddingFunction`` (ONNX | ||
| MiniLM, 384 dims, CPU) for backward compatibility with existing palaces. | ||
| """ | ||
| provider = os.environ.get("EMBEDDING_PROVIDER", "").lower() | ||
| if provider != "ollama": | ||
| return None | ||
| from chromadb.utils.embedding_functions import OllamaEmbeddingFunction | ||
|
|
||
| return OllamaEmbeddingFunction( | ||
| url=os.environ.get("OLLAMA_URL") or "http://localhost:11434", | ||
| model_name=os.environ.get("OLLAMA_EMBED_MODEL") or "nomic-embed-text", | ||
| timeout=int(os.environ.get("OLLAMA_EMBED_TIMEOUT") or "60"), | ||
| ) | ||
|
Comment on lines
+33
to
+55
|
||
|
|
||
|
|
||
| def _fix_blob_seq_ids(palace_path: str): | ||
| """Fix ChromaDB 0.6.x -> 1.5.x migration bug: BLOB seq_ids -> INTEGER. | ||
|
|
||
|
|
@@ -124,12 +149,17 @@ def get_collection(self, palace_path: str, collection_name: str, create: bool = | |
| pass | ||
|
|
||
| client = self._client(palace_path) | ||
| ef = get_embedding_function() | ||
| if create: | ||
| collection = client.get_or_create_collection( | ||
| collection_name, metadata={"hnsw:space": "cosine"} | ||
| collection_name, | ||
| metadata={"hnsw:space": "cosine"}, | ||
| embedding_function=ef, | ||
| ) | ||
| else: | ||
| collection = client.get_collection(collection_name) | ||
| collection = client.get_collection( | ||
| collection_name, embedding_function=ef | ||
| ) | ||
|
||
| return ChromaCollection(collection) | ||
|
|
||
| def get_or_create_collection( | ||
|
|
@@ -147,6 +177,8 @@ def create_collection( | |
| ) -> "ChromaCollection": | ||
| """Create (not get-or-create) *collection_name* with cosine HNSW space.""" | ||
| collection = self._client(palace_path).create_collection( | ||
| collection_name, metadata={"hnsw:space": hnsw_space} | ||
| collection_name, | ||
| metadata={"hnsw:space": hnsw_space}, | ||
| embedding_function=get_embedding_function(), | ||
| ) | ||
|
Comment on lines
209
to
213
|
||
| return ChromaCollection(collection) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ | |
| sanitize_content, | ||
| ) | ||
| from .version import __version__ # noqa: E402 | ||
| from .backends.chroma import ChromaBackend, ChromaCollection # noqa: E402 | ||
| from .backends.chroma import ChromaBackend, ChromaCollection, get_embedding_function # noqa: E402 | ||
| from .query_sanitizer import sanitize_query # noqa: E402 | ||
| from .searcher import search_memories # noqa: E402 | ||
| from .palace_graph import ( # noqa: E402 | ||
|
|
@@ -216,16 +216,24 @@ def _get_collection(create=False): | |
| global _collection_cache, _metadata_cache, _metadata_cache_time | ||
| try: | ||
| client = _get_client() | ||
| ef = get_embedding_function() | ||
| if create: | ||
|
Comment on lines
218
to
223
|
||
| _collection_cache = ChromaCollection( | ||
| client.get_or_create_collection( | ||
| _config.collection_name, metadata={"hnsw:space": "cosine"} | ||
| _config.collection_name, | ||
| metadata={"hnsw:space": "cosine"}, | ||
| embedding_function=ef, # type: ignore[arg-type] | ||
| ) | ||
|
||
| ) | ||
| _metadata_cache = None | ||
| _metadata_cache_time = 0 | ||
| elif _collection_cache is None: | ||
| _collection_cache = ChromaCollection(client.get_collection(_config.collection_name)) | ||
| _collection_cache = ChromaCollection( | ||
| client.get_collection( | ||
| _config.collection_name, | ||
| embedding_function=ef, # type: ignore[arg-type] | ||
| ) | ||
| ) | ||
| _metadata_cache = None | ||
| _metadata_cache_time = 0 | ||
| return _collection_cache | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
provider = os.environ.get("EMBEDDING_PROVIDER", "").lower()doesn't handle leading/trailing whitespace (e.g.," ollama"), which can make the opt-in flag unexpectedly fail. Consider normalizing with.strip().lower()before comparing.