Back to Llama Index

Embeddings with Clarifai

docs/examples/embeddings/clarifai.ipynb

0.14.211.8 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/clarifai.ipynb" target="_parent"></a>

Embeddings with Clarifai

LlamaIndex has support for Clarifai embeddings models.

You must have a Clarifai account and a Personal Access Token (PAT) key. Check here to get or create a PAT.

Set CLARIFAI_PAT as an environment variable or You can pass PAT as argument to ClarifaiEmbedding class

python
%pip install llama-index-embeddings-clarifai
python
!export CLARIFAI_PAT=YOUR_KEY

If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

python
!pip install llama-index

Models can be referenced either by the full URL or by the model_name, user ID, and app ID combination.

python
from llama_index.embeddings.clarifai import ClarifaiEmbedding

# Create a clarifai embedding class just with model_url, assuming that CLARIFAI_PAT is set as an environment variable
embed_model = ClarifaiEmbedding(
    model_url="https://clarifai.com/clarifai/main/models/BAAI-bge-base-en"
)

# Alternatively you can initialize the class with model_name, user_id, app_id and pat as well.
embed_model = ClarifaiEmbedding(
    model_name="BAAI-bge-base-en",
    user_id="clarifai",
    app_id="main",
    pat=CLARIFAI_PAT,
)
python
embeddings = embed_model.get_text_embedding("Hello World!")
print(len(embeddings))
print(embeddings[:5])

Embed list of texts

python
text = "roses are red violets are blue."
text2 = "Make hay while the sun shines."
python
embeddings = embed_model._get_text_embeddings([text2, text])
print(len(embeddings))
print(embeddings[0][:5])
print(embeddings[1][:5])