Skip to main content
Open In ColabOpen on GitHub

model2vec

Overviewโ€‹

Model2Vec is a technique to turn any sentence transformer into a really small static model model2vec can be used to generate embeddings.

Setupโ€‹

pip install -U langchain-community

Instantiationโ€‹

Ensure that model2vec is installed

pip install -U model2vec

Indexing and Retrievalโ€‹

from langchain_community.embeddings import Model2vecEmbeddings
API Reference:Model2vecEmbeddings
embeddings = Model2vecEmbeddings("minishlab/potion-base-8M")
query_text = "This is a test query."
query_result = embeddings.embed_query(query_text)
document_text = "This is a test document."
document_result = embeddings.embed_documents([document_text])

Direct Usageโ€‹

Here's how you would directly make use of model2vec

from model2vec import StaticModel

# Load a model from the HuggingFace hub (in this case the potion-base-8M model)
model = StaticModel.from_pretrained("minishlab/potion-base-8M")

# Make embeddings
embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])

# Make sequences of token embeddings
token_embeddings = model.encode_as_sequence(["It's dangerous to go alone!", "It's a secret to everybody."])

API Referenceโ€‹

For more information check out the model2vec github repo


Was this page helpful?