主题
LlamaIndex 支持 Clarifai 嵌入模型。
你必须拥有 Clarifai 账户和 Personal Access Token(PAT)密钥。请在此处获取或创建 PAT。
将 CLARIFAI_PAT 设置为环境变量,或者将 PAT 作为参数传给 ClarifaiEmbedding 类
%pip install llama-index-embeddings-clarifai1
!export CLARIFAI_PAT=YOUR_KEY1
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
!pip install llama-index1
模型既可以通过完整 URL 引用,也可以通过 model_name、用户 ID 和应用 ID 的组合引用。
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,
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
embeddings = embed_model.get_text_embedding("Hello World!")
print(len(embeddings))
print(embeddings[:5])1
2
3
2
3
嵌入文本列表
text = "roses are red violets are blue."
text2 = "Make hay while the sun shines."1
2
2
embeddings = embed_model._get_text_embeddings([text2, text])
print(len(embeddings))
print(embeddings[0][:5])
print(embeddings[1][:5])1
2
3
4
2
3
4