Download core/embeddings.py from sk-aiu/cortex: direct link, hf CLI and curl.
- Browser
- Download file 729 Bytes
-
https://huggingface.co/spaces/sk-aiu/cortex/resolve/main/core/embeddings.py
- Command line
-
hf download hf://spaces/sk-aiu/cortex/core/embeddings.py
-
curl -L -o embeddings.py https://huggingface.co/spaces/sk-aiu/cortex/resolve/main/core/embeddings.py
729 Bytes
| """Local CPU embeddings (no API cost) exposed through LangChain's Embeddings interface.""" | |
| import os | |
| from functools import lru_cache | |
| from langchain_core.embeddings import Embeddings | |
| MODEL = "BAAI/bge-base-en-v1.5" | |
| DIM = 768 | |
| def _model(): | |
| from fastembed import TextEmbedding | |
| path = os.getenv("CORTEX_EMBED_PATH") # pre-downloaded model dir (offline builds) | |
| return TextEmbedding(MODEL, specific_model_path=path) if path else TextEmbedding(MODEL) | |
| class LocalEmbeddings(Embeddings): | |
| def embed_documents(self, texts): | |
| return [v.tolist() for v in _model().embed(texts, batch_size=32)] | |
| def embed_query(self, text): | |
| return next(iter(_model().query_embed(text))).tolist() | |