Google Cloud Vertex.AI API 访问

您需要在您的帐户中创建一个 Google Cloud 项目并启用 Vertex.AI 服务。作为访问令牌,您可以运行 gcloud auth print-access-token。使用这些服务将在您的 Google Cloud 帐户中产生费用。

所有以下存储过程都可以包含以下 APOC 配置,即在 apoc.conf 中或通过 docker 环境变量设置。APOC 配置

描述

默认值

apoc.ml.vertexai.url

OpenAI 端点基础 URL

endpoint 配置参数值

此外,它们可以包含以下配置键作为最后一个参数。

表 1. 常用配置参数
描述 默认值

endpoint

类似于 apoc.ml.vertexai.url APOC 配置

https://{region}-aiplatform.googleapis.com/v1/projects/{project}/locations/{region}/publishers/google/models/{model}:{resource}

headers

用于添加或编辑 HTTP 默认请求头

{Content-Type: "application/json", Accept: "application/json", Authorization: "Bearer " + <$accessToken,即第二个参数> }

模型

Vertex AI 模型

取决于存储过程

区域

Vertex AI 区域

us-central1

资源

Vertex AI 资源(见下文)

取决于存储过程

temperature, maxOutputTokens, maxDecodeSteps, topP, topK

可以传递到 HTTP 请求中的可选参数。取决于使用的 API

{temperature: 0.3, maxOutputTokens: 256, maxDecodeSteps: 200, topP: 0.8, topK: 40}

我们可以将 endpoint 配置定义为完整的 URL,例如 https://us-central1-aiplatform.googleapis.com/v1/projects/myVertexAIProject/locations/us-central1/publishers/google/models/gemini-pro-vision:streamGenerateContent,或者通过其他配置替换的参数来定义。

例如,如果我们未定义 endpoint 配置,则将使用默认配置 https://{region}-aiplatform.googleapis.com/v1/projects/{project}/locations/{region}/publishers/google/models/{model}:{resource},其中

  • {model} 将是 model 配置定义的模型

  • {region}region 配置定义

  • {project} 由第三个参数 (project) 定义

  • {resource}resource 配置定义

或者,我们可以将 endpoint 定义为 https://us-central1-aiplatform.googleapis.com/v1/projects/{project}/locations/us-central1/publishers/google/models/gemini-pro-vision:streamGenerateContent,在这种情况下,我们只需用第三个参数替换 {project}

让我们看一些例子。

生成嵌入 API

此存储过程 apoc.ml.vertexai.embedding 可以接受一个文本字符串列表,并为每个字符串返回一行,其中嵌入数据为一个 768 维向量。它使用此处文档化的嵌入端点。

API 配额按项目/区域计算,您可以在配置映射中覆盖默认的 us-central1,例如 {region:'us-east4'}。GCP 区域可在以下链接找到:https://cloud.google.com/about/locations

附加配置被传递给 API,默认使用的模型是 textembedding-gecko

生成嵌入调用
CALL apoc.ml.vertexai.embedding(['Some Text'], $accessToken, $project, {region:'<region>'}) yield index, text, embedding;
表 2. 生成嵌入响应
索引 文本 嵌入

0

"一些文本"

[-0.0065358975, -7.9563365E-4, …​. -0.010693862, -0.005087272]

表 3. 参数
名称 描述

文本列表

文本字符串列表

访问令牌

Vertex.AI API 访问令牌

项目

Google Cloud 项目

配置

用于模型和 region 等其他请求参数的可选映射

表 4. 结果
名称 描述

索引

原始列表中的索引条目

文本

原始列表中的文本行

嵌入

textembedding-gecko 模型的 768 维浮点嵌入向量

文本补全 API

此存储过程 apoc.ml.vertexai.completion 可以继续/补全给定的文本。

它使用此处文档化的补全模型 API。

附加配置被传递给 API,默认使用的模型是 text-bison

文本补全调用
CALL apoc.ml.vertexai.completion('What color is the sky? Answer in one word: ', $apiKey, $project, {})
文本补全响应
{value={safetyAttributes={blocked=false, scores=[0.1], categories=[Sexual]},
recitationResult={recitations=[], recitationAction=NO_ACTION}, content=blue}}
表 5. 参数
名称 描述

提示词

要补全的文本

访问令牌

Vertex.AI API 访问令牌

项目

Google Cloud 项目

配置

用于模型、区域、温度、topK、topP、maxOutputTokens 等条目以及其他请求参数的可选映射

表 6. 结果
名称 描述

来自 Vertex.AI 的结果条目 (content, safetyAttributes(blocked, categories, scores), recitationResult(recitationAction, recitations))

聊天补全 API

此存储过程 apoc.ml.vertexai.chat 接受一个包含助手和用户之间聊天交流映射(带可选系统上下文)的列表,并返回流程中的下一条消息。

它使用此处文档化的聊天模型 API。

附加配置被传递给 API,默认使用的模型是 chat-bison

聊天补全调用
CALL apoc.ml.vertexai.chat(
/*messages*/
[{author:"user", content:"What planet do timelords live on?"}],
$apiKey, $project,
{temperature:0},
/*context*/ "Fictional universe of Doctor Who. Only answer with a single word!",
/*examples*/ [{input:{content:"What planet do humans live on?"}, output:{content:"Earth"}}])
yield value
聊天补全响应
{value={candidates=[{author=1, content=Gallifrey.}], safetyAttributes={blocked=false, scores=[0.1, 0.1, 0.1], categories=[Religion & Belief, Sexual, Toxic]}, recitationResults=[{recitations=[], recitationAction=NO_ACTION}]}}
表 7. 参数
名称 描述

消息

包含带有 `{author:"bot"

用户", content:"文本"}` 的指令映射列表

访问令牌

Vertex.AI API 访问令牌

项目

Google Cloud 项目

配置

用于区域、模型、温度、topK、topP、maxOutputTokens 等条目以及其他参数的可选映射

上下文

补全的可选上下文和系统提示

示例

表 8. 结果
名称 描述

来自 Vertex.AI 的结果条目(包含 candidates(author, content), safetyAttributes(categories, scores, blocked), recitationResults(recitationAction, recitations))

流式传输 API

此存储过程 apoc.ml.vertexai.stream 接受一个包含助手和用户之间内容交流映射(带可选系统上下文)的列表,并返回流程中的下一条消息。

默认情况下,它使用Gemini AI API

CALL apoc.ml.vertexai.stream([{role: "user", parts: [{text: "translate book in italian"}]}], '<accessToken>', '<projectID>')
表 9. 结果

{finishReason:"STOP", safetyRatings:[{probability:"NEGLIGIBLE", category:"HARM_CATEGORY_HARASSMENT"}, {probability:"NEGLIGIBLE", category:"HARM_CATEGORY_HATE_SPEECH"}, {probability:"NEGLIGIBLE", category:"HARM_CATEGORY_SEXUALLY_EXPLICIT"}, {probability:"NEGLIGIBLE", category:"HARM_CATEGORY_DANGEROUS_CONTENT"}], content:{role:"model", parts:[{text:"Libro"}]}}

我们可以调整参数,例如 temperature

CALL apoc.ml.vertexai.stream([{role: "user", parts: [{text: "translate book in italian"}]}], '<accessToken>', '<projectID>',
    {temperature: 0})

这对应于以下 Http 请求体,其中 maxOutputTokenstopPtopK 具有上面指定的默认值(常用配置参数

{
    "contents": [
        {
            "role": "user",
            "parts": [
                {
                    "text": "translate book in italian"
                }
            ]
        }
    ],
    "generation_config": {
        "temperature": 0,
        "maxOutputTokens": 256,
        "topP": 0.8,
        "topK": 40
    }
}

自定义 API

使用此存储过程,我们可以调用 Vertex AI 中可用的任何 API。

为了最大程度的灵活性,在这种情况下,第一个参数不会被修改,而是精确匹配 HTTP 请求体,并且返回类型为 ANY

Gemini Pro Vision 示例
CALL apoc.ml.vertexai.custom({
    contents: [
        {
            role: "user",
            parts: [
					{text: "What is this?"},
                    {inlineData: {
                        mimeType: "image/png",
                        data: '<base64Image>'}
                }
            ]
        }
    ]
},
"<accessToken>",
"<projectId>",
{model: 'gemini-pro-vision'}
)
表 10. 结果

[{usageMetadata: {promptTokenCount: 262, totalTokenCount: 272, candidatesTokenCount: 10}, candidates: [{content: {role: "model", parts: [{text: " 这是一张书的照片…​"}]}, finishReason: "STOP", safetyRatings: [{category: "HARM_CATEGORY_HARASSMENT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_HATE_SPEECH", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_DANGEROUS_CONTENT", probability│ │: "NEGLIGIBLE"}]}]}]

CALL apoc.ml.vertexai.custom({contents: {role: "user", parts: [{text: "translate book in italian"}]}},
    "<accessToken>",
    "<projectId>",
    {endpoint: "https://us-central1-aiplatform.googleapis.com/v1/projects/{project}/locations/us-central1/publishers/google/models/gemini-pro-vision:streamGenerateContent"}
)
表 11. 结果

[{usageMetadata: {promptTokenCount: 4, totalTokenCount: 5, candidatesTokenCount: 1}, candidates: [{content: {role: "model", parts: [{text: "libro"}]}, finishReason: "STOP", safetyRatings: [{category: "HARM_CATEGORY_HARASSMENT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_HATE_SPEECH", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_DANGEROUS_CONTENT", probability: "NEGLIGIBLE"}]}]}]

CALL apoc.ml.vertexai.custom({contents: {role: "user", parts: [{text: "translate book in italian"}]}},
    "<accessToken>",
    null,
    {endpoint: "https://us-central1-aiplatform.googleapis.com/v1/projects/vertex-project-413513/locations/us-central1/publishers/google/models/gemini-pro-vision:streamGenerateContent"}
)
表 12. 结果

[{usageMetadata: {promptTokenCount: 4, totalTokenCount: 5, candidatesTokenCount: 1}, candidates: [{content: {role: "model", parts: [{text: "libro"}]}, finishReason: "STOP", safetyRatings: [{category: "HARM_CATEGORY_HARASSMENT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_HATE_SPEECH", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", probability: "NEGLIGIBLE"}, {category: "HARM_CATEGORY_DANGEROUS_CONTENT", probability: "NEGLIGIBLE"}]}]}]

Gemini Flash 示例
CALL apoc.ml.vertexai.custom({
  "contents": [
    { "parts": [
        {
            "text": "translate the word 'book' in italian"
        }],
        "role": "user"
    }]
},
"<accessToken>",
"<projectId>",
{model: 'gemini-1.5-flash-001'}
)
表 13. 结果

{ "candidates" : [ { "finishReason" : "STOP", "safetyRatings" : [ { "severity" : "HARM_SEVERITY_NEGLIGIBLE", "probabilityScore" : 0.09484524, "category" : "HARM_CATEGORY_HATE_SPEECH", "severityScore" : 0.08882029, "probability" : "NEGLIGIBLE" }, { "severity" : "HARM_SEVERITY_NEGLIGIBLE", "probabilityScore" : 0.041307304, "category" : "HARM_CATEGORY_DANGEROUS_CONTENT", "severityScore" : 0.10687688, "probability" : "NEGLIGIBLE" }, { "severity" : "HARM_SEVERITY_NEGLIGIBLE", "probabilityScore" : 0.12808825, "category" : "HARM_CATEGORY_HARASSMENT", "severityScore" : 0.047691282, "probability" : "NEGLIGIBLE" }, { "severity" : "HARM_SEVERITY_NEGLIGIBLE", "probabilityScore" : 0.100348406, "category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT", "severityScore" : 0.10800066, "probability" : "NEGLIGIBLE" } ], "content" : { "parts" : [ { "text" : "意大利语中“书”这个词是 libro。\n" } ], "role" : "model" } } ], "usageMetadata" : { "totalTokenCount" : 21, "candidatesTokenCount" : 13, "promptTokenCount" : 8 } }

此外,我们可以使用不以 https://<region>-aiplatform.googleapis.com 开头的端点的其他 Google API,例如我们可以使用文本转语音 API

CALL apoc.ml.vertexai.custom(
{
  input:{
    text:'just a test'
  },
  voice:{
    languageCode:'en-US',
    name:'en-US-Studio-O'
  },
  audioConfig:{
    audioEncoding:'LINEAR16',
    speakingRate:1
  }
},
"<accessToken>",
"<projectId>",
{endpoint: "https://texttospeech.googleapis.com/v1/text:synthesize"})
© . All rights reserved.