使用 Clarifai 进行嵌入¶
LlamaIndex 支持 Clarifai 的嵌入模型。
您必须拥有 Clarifai 账户和个人访问令牌(PAT)。
点击此处获取或创建 PAT。
请将 CLARIFAI_PAT 设置为环境变量,或者您可以将 PAT 作为参数传递给 ClarifaiEmbedding 类
In [ ]:
Copied!
%pip install llama-index-embeddings-clarifai
%pip install llama-index-embeddings-clarifai
In [ ]:
Copied!
!export CLARIFAI_PAT=YOUR_KEY
!export CLARIFAI_PAT=YOUR_KEY
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
模型可以通过完整 URL 或通过 model_name、用户 ID 和应用 ID 的组合进行引用。
In [ ]:
Copied!
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,
)
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,
)
In [ ]:
Copied!
embeddings = embed_model.get_text_embedding("Hello World!")
print(len(embeddings))
print(embeddings[:5])
embeddings = embed_model.get_text_embedding("Hello World!")
print(len(embeddings))
print(embeddings[:5])
嵌入文本列表
In [ ]:
Copied!
text = "roses are red violets are blue."
text2 = "Make hay while the sun shines."
text = "roses are red violets are blue."
text2 = "Make hay while the sun shines."
In [ ]:
Copied!
embeddings = embed_model._get_text_embeddings([text2, text])
print(len(embeddings))
print(embeddings[0][:5])
print(embeddings[1][:5])
embeddings = embed_model._get_text_embeddings([text2, text])
print(len(embeddings))
print(embeddings[0][:5])
print(embeddings[1][:5])