Integrations / Chroma
Chroma.
Use separate named prompts for indexing documents and embedding queries.
Install
pip install chromadb sentence-transformersIndex and query
import chromadb
from chromadb import Documents, EmbeddingFunction, Embeddings
from sentence_transformers import SentenceTransformer
class HobbyLMEmbeddingFunction(EmbeddingFunction):
def __init__(self):
self._model = SentenceTransformer(
"harims95/hobbylm-embed-140m",
revision="ea708cee107818fa0c1871eebd26d82a1e3fb1aa",
trust_remote_code=True,
)
def __call__(self, input: Documents) -> Embeddings:
return self._model.encode(list(input), prompt_name="passage").tolist()
def embed_query(self, input: Documents) -> Embeddings:
return self._model.encode(list(input), prompt_name="query").tolist()
client = chromadb.Client()
collection = client.create_collection(
name="hobbylm_documents",
embedding_function=HobbyLMEmbeddingFunction(),
metadata={"hnsw:space": "cosine"},
)
collection.add(ids=["local_embeddings"], documents=["HobbyLM-Embed runs locally."])
results = collection.query(query_texts=["How do I load HobbyLM-Embed?"], n_results=1)The function uses prompt_name="passage" for documents and prompt_name="query" for search. Pass raw text without adding prefixes.
Longer inputs
512 tokens by default and up to 2,048 tokens opt-in. Set self._model.max_seq_length = 2048 after loading the model inside the embedding function to opt in.
Rebuild the document embeddings if you change the model or encoding configuration.