用于调用 Google Vertex AI gRPC API 以运行诸如 Gemini 等生成式模型的类型化接口。
Google Vertex AI 运行 Google 的基础模型和生成式模型,包括 Gemini 系列。该 gRPC API 通过 google.cloud.aiplatform.v1.PredictionService 服务公开,主要方法包括 GenerateContent 和 Predict。
Vertex AI 使用区域性端点:主机为 <region>-aiplatform.googleapis.com(例如 us-central1-aiplatform.googleapis.com),端口为 443,通过 TLS 访问,并且 JWT 受众必须与同一主机匹配(例如 https://us-central1-aiplatform.googleapis.com/)。
请求通过 TsgcGRPCVertexAIGenerateContentRequest 构建,它会连同提示内容一起设置完全限定的 Model (projects/<id>/locations/<region>/publishers/google/models/<model>,例如 gemini-1.5-flash)。
下面的示例使用服务账户 JWT 进行身份验证,通过 TsgcHTTP2Client 将 TsgcGRPCClient 连接到区域性 Vertex AI 主机,设置授权 Bearer 元数据,并在 us-central1 中针对某个 Gemini 模型使用提示调用 GenerateContent:
oHTTP2 := TsgcHTTP2Client.Create(nil);
oHTTP2.Host := 'us-central1-aiplatform.googleapis.com';
oHTTP2.Port := 443;
oHTTP2.TLS := True;
oGRPC := TsgcGRPCClient.Create(nil);
oGRPC.Client := oHTTP2;
// service-account JWT authentication (audience must match the regional host)
oGRPC.GoogleCloudOptions.JWT.KeyFile := 'service-account.json';
oGRPC.GoogleCloudOptions.JWT.API_Endpoint := 'https://us-central1-aiplatform.googleapis.com/';
oGRPC.DefaultMetadata.AddValue('authorization', 'Bearer ' + oGRPC.GoogleCloudOptions.JWT.Token);
// build the typed request and call the method
oRequest := TsgcGRPCVertexAIGenerateContentRequest.Create;
try
oRequest.Model := 'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-1.5-flash';
oRequest.AddText('Write a haiku about the sea.');
oResponse := oGRPC.Call('google.cloud.aiplatform.v1.PredictionService', 'GenerateContent', oRequest.ToBytes);
ShowMessage(oResponse.DataString);
finally
oRequest.Free;
end;
| 名称 | 描述 |
|---|---|
| GenerateContent | 在给定提示的情况下,从生成式模型(例如 Gemini)生成内容。 |
| Predict | 针对已部署的模型运行预测。 |
示例文件夹 Demos/21.GRPC/17.Vertex_AI 中提供了一个可用的示例,演示如何针对区域端点进行身份验证,并使用 GenerateContent 方法从 Gemini 模型生成内容。