Vertex AI 外掛程式

Vertex AI 外掛程式提供多種 Google 生成式 AI 模型的介面 透過 Vertex AI API

  • Gemini 1.0 Pro 和 Gemini 1.0 Pro Vision 文字生成功能
  • 產生 Imagen2 圖片
  • Gecko 文字嵌入產生功能

並透過 Vertex AI Rapid Evaluation API 存取部分評估指標。

安裝

npm i --save @genkit-ai/vertexai

如果您想要在本機執行使用此外掛程式的流程,還需要 已安裝 Google Cloud CLI 工具

設定

如要使用這個外掛程式,請在呼叫 configureGenkit() 時指定:

import { vertexAI } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({ projectId: 'your-cloud-project', location: 'us-central1' }),
  ],
  // ...
});

外掛程式會要求您指定 Google Cloud 專案 ID,以及 region [區域] 以及要傳送 Vertex API 要求的 Google Cloud 專案 憑證

  • 指定 Google Cloud 專案 ID 的方式,是透過在projectId vertexAI() 設定或設定 GCLOUD_PROJECT 環境 變數。如果是從 Google Cloud 環境 (Cloud) 環境 函式、Cloud Run 等),GCLOUD_PROJECT 會自動設為 環境的專案 ID。

  • 指定 API 位置的方式,是透過在location vertexAI() 設定或設定 GCLOUD_LOCATION 環境 變數。

  • 如要提供 API 憑證,您必須設定 Google Cloud 應用程式 。

    1. 指定憑證的方法如下:

      • 如果是從 Google Cloud 環境 (Cloud) 環境 函式、Cloud Run 等),系統會自動設定此項目。

      • 在本機開發環境中,執行下列指令:

      gcloud auth application-default login
      
    2. 此外,請確認已授予帳戶「Vertex AI 使用者」這個 IAM 角色 (roles/aiplatform.user).查看 Vertex AI 存取權控管 文件。

用量

生成式 AI 模型

這個外掛程式會以靜態方式將參照匯出至支援的生成式 AI 模型:

import { gemini15Flash, gemini15Pro, imagen2 } from '@genkit-ai/vertexai';

您可以透過這些參照來指定 generate() 使用的模型:

const llmResponse = await generate({
  model: gemini15Flash,
  prompt: 'What should I do when I visit Melbourne?',
});

這個外掛程式也會以靜態方式匯出 Gecko 文字嵌入的參照 模型:

import { textEmbeddingGecko } from '@genkit-ai/vertexai';

您可以使用這項參照來指定哪些嵌入器是索引器或擷取器 使用方式。舉例來說,如果您使用 Chroma DB:

configureGenkit({
  plugins: [
    chroma([
      {
        embedder: textEmbeddingGecko,
        collectionName: 'my-collection',
      },
    ]),
  ],
});

或者,您也可以直接產生嵌入項目:

// import { embed, EmbedderArgument } from '@genkit-ai/ai/embedder';
const embedding = await embed({
  embedder: textEmbeddingGecko,
  content: 'How many widgets do you have in stock?',
});

Vertex AI 模型園地上的 Anthropic Claude 3

如果您可以在 Vertex AI Model Garden 中使用 Claude 3 模型 (haikusonnetopus),則可以與 Genkit 搭配使用。

以下是啟用 Vertex AI Model Garden 模型的設定範例:

import {
  vertexAI,
  claude3Haiku,
  claude3Sonnet,
  claude3Opus,
} from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      location: 'us-central1',
      modelGarden: {
        models: [claude3Haiku, claude3Sonnet, claude3Opus],
      },
    }),
  ],
});

然後使用這些模型做為一般模型:

const llmResponse = await generate({
  model: claude3Sonnet,
  prompt: 'What should I do when I visit Melbourne?',
});

Vertex AI Model Garden 中的 Llama 3.1 405b

如果您可以使用 Vertex AI Model Garden 中的 Llama 3.1 405b,即可搭配 Genkit 使用。

以下是啟用 Vertex AI Model Garden 模型的設定範例:

import { vertexAI, llama3 } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      location: 'us-central1',
      modelGarden: {
        models: [llama3],
      },
    }),
  ],
});

然後用做一般模型:

const llmResponse = await generate({
  model: llama3,
  prompt: 'Write a function that adds two numbers together',
});

評估工具

如要使用 Vertex AI Rapid Evaluation 評估工具,請在 vertexAI 外掛程式設定中新增 evaluation 區塊。

import { vertexAI, VertexAIEvaluationMetricType } from '@genkit-ai/vertexai';

export default configureGenkit({
  plugins: [
    vertexAI({
      projectId: 'your-cloud-project',
      location: 'us-central1',
      evaluation: {
        metrics: [
          VertexAIEvaluationMetricType.SAFETY,
          {
            type: VertexAIEvaluationMetricType.ROUGE,
            metricSpec: {
              rougeType: 'rougeLsum',
            },
          },
        ],
      },
    }),
  ],
  // ...
});

上述設定會新增 SafetyROUGE 指標的評估工具。這個範例說明兩種方法:Safety 指標使用預設規格,ROUGE 指標提供將 Rouge 類型設為 rougeLsum 的自訂規格。

您可以使用 genkit eval:run 指令,執行兩個評估器,並搭配使用相容的資料集 (也就是含有 outputreference 欄位的資料集)。由於 Safety 評估工具只需要 output,因此也可以使用 genkit eval:flow -e vertexai/safety 指令執行。