通过 LlamaIndex 与部署在 Vertex AI 终端的嵌入模型交互¶
Vertex AI 终端是一种托管资源,可用于部署机器学习模型(如嵌入模型)来对新数据进行预测。
本笔记本演示了如何使用 VertexEndpointEmbedding
类与嵌入终端进行交互,该功能基于 LlamaIndex 实现。
环境配置¶
如果您在 Colab 上打开此 Notebook,很可能需要安装 LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-embeddings-vertex-endpoint
%pip install llama-index-embeddings-vertex-endpoint
In [ ]:
Copied!
! pip install llama-index
! pip install llama-index
您需要指定端点信息(端点ID、项目ID和区域)才能与部署在Vertex AI中的模型进行交互。
In [ ]:
Copied!
ENDPOINT_ID = "<-YOUR-ENDPOINT-ID->"
PROJECT_ID = "<-YOUR-PROJECT-ID->"
LOCATION = "<-YOUR-GCP-REGION->"
ENDPOINT_ID = "<-YOUR-ENDPOINT-ID->"
PROJECT_ID = "<-YOUR-PROJECT-ID->"
LOCATION = "<-YOUR-GCP-REGION->"
需要提供凭证以连接到终端节点。您可以选择以下任一方式:
- 通过指定
service_account_file
参数使用服务账户 JSON 文件。 - 通过
service_account_info
参数直接提供服务账户信息。
使用服务账号文件的示例:
In [ ]:
Copied!
from llama_index.embeddings.vertex_endpoint import VertexEndpointEmbedding
SERVICE_ACCOUNT_FILE = "<-YOUR-SERVICE-ACCOUNT-FILE-PATH->.json"
embed_model = VertexEndpointEmbedding(
endpoint_id=ENDPOINT_ID,
project_id=PROJECT_ID,
location=LOCATION,
service_account_file=SERVICE_ACCOUNT_FILE,
)
from llama_index.embeddings.vertex_endpoint import VertexEndpointEmbedding
SERVICE_ACCOUNT_FILE = "<-YOUR-SERVICE-ACCOUNT-FILE-PATH->.json"
embed_model = VertexEndpointEmbedding(
endpoint_id=ENDPOINT_ID,
project_id=PROJECT_ID,
location=LOCATION,
service_account_file=SERVICE_ACCOUNT_FILE,
)
使用直接服务账户信息的示例::
In [ ]:
Copied!
from llama_index.embeddings.vertex_endpoint import VertexEndpointEmbedding
SERVICE_ACCOUNT_INFO = {
"private_key": "<-PRIVATE-KEY->",
"client_email": "<-SERVICE-ACCOUNT-EMAIL->",
"token_uri": "https://oauth2.googleapis.com/token",
}
embed_model = VertexEndpointEmbedding(
endpoint_id=ENDPOINT_ID,
project_id=PROJECT_ID,
location=LOCATION,
service_account_info=SERVICE_ACCOUNT_INFO,
)
from llama_index.embeddings.vertex_endpoint import VertexEndpointEmbedding
SERVICE_ACCOUNT_INFO = {
"private_key": "<-PRIVATE-KEY->",
"client_email": "<-SERVICE-ACCOUNT-EMAIL->",
"token_uri": "https://oauth2.googleapis.com/token",
}
embed_model = VertexEndpointEmbedding(
endpoint_id=ENDPOINT_ID,
project_id=PROJECT_ID,
location=LOCATION,
service_account_info=SERVICE_ACCOUNT_INFO,
)
基本用法¶
调用 get_text_embedding
¶
In [ ]:
Copied!
embeddings = embed_model.get_text_embedding(
"Vertex AI is a managed machine learning (ML) platform provided by Google Cloud. It allows data scientists and developers to build, deploy, and scale machine learning models efficiently, leveraging Google's ML infrastructure."
)
embeddings = embed_model.get_text_embedding(
"Vertex AI is a managed machine learning (ML) platform provided by Google Cloud. It allows data scientists and developers to build, deploy, and scale machine learning models efficiently, leveraging Google's ML infrastructure."
)
In [ ]:
Copied!
embeddings[:10]
embeddings[:10]
Out[ ]:
[0.011612358, 0.01030837, -0.04710829, -0.030719217, 0.027658276, -0.031597693, 0.012065322, -0.037609763, 0.02321099, 0.012868305]
调用 get_text_embedding_batch
¶
In [ ]:
Copied!
embeddings = embed_model.get_text_embedding_batch(
[
"Vertex AI is a managed machine learning (ML) platform provided by Google Cloud. It allows data scientists and developers to build, deploy, and scale machine learning models efficiently, leveraging Google's ML infrastructure.",
"Vertex is integrated with llamaIndex",
]
)
embeddings = embed_model.get_text_embedding_batch(
[
"Vertex AI is a managed machine learning (ML) platform provided by Google Cloud. It allows data scientists and developers to build, deploy, and scale machine learning models efficiently, leveraging Google's ML infrastructure.",
"Vertex is integrated with llamaIndex",
]
)
In [ ]:
Copied!
len(embeddings)
len(embeddings)
Out[ ]:
2