Vertex AI 外掛程式

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

  • 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、要發出 Vertex API 要求的地區,以及 Google Cloud 專案憑證。

  • 如要指定 Google Cloud 專案 ID,您可以在 vertexAI() 設定中設定 projectId,或是設定 GCLOUD_PROJECT 環境變數。如果您是從 Google Cloud 環境 (Cloud Functions、Cloud Run 等) 執行流程,GCLOUD_PROJECT 會自動設為環境的專案 ID。

  • 如要指定 API 位置,您可以在 vertexAI() 設定中設定 location,或是設定 GCLOUD_LOCATION 環境變數。

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

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

      • 如果您是從 Google Cloud 環境 (Cloud Functions、Cloud Run 等) 執行流程,系統會自動完成此設定。

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

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

用量

生成式 AI 模型

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

import { geminiPro, geminiProVision, imagen2 } from '@genkit-ai/vertexai';

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

const llmResponse = await generate({
  model: geminiPro,
  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',
      modelGardenModels: [claude3Haiku, claude3Sonnet, claude3Opus],
    }),
  ],
});

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

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

評估工具

如要使用 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 指令執行。