使用 Clarifai 的嵌入¶
LlamaIndex 支持 Clarifai 嵌入模型。
您必须拥有 Clarifai 账户和个人访问令牌 (PAT) 密钥。在此处查看或创建 PAT。
将 CLARIFAI_PAT 设置为环境变量,或者您可以将 PAT 作为参数传递给 ClarifaiEmbedding 类
In [ ]
已复制!
%pip install llama-index-embeddings-clarifai
%pip install llama-index-embeddings-clarifai
In [ ]
已复制!
!export CLARIFAI_PAT=YOUR_KEY
!export CLARIFAI_PAT=YOUR_KEY
如果您在 colab 上打开此 Notebook,您可能需要安装 LlamaIndex 🦙。
In [ ]
已复制!
!pip install llama-index
!pip install llama-index
模型可以通过完整 URL 或通过 model_name、用户 ID 和应用 ID 组合来引用。
In [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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 [ ]
已复制!
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])