Vertex AI 外掛程式

Vertex AI 外掛程式為多種 AI 服務提供介面:

安裝

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 API 服務

以下是 Vertex AI 外掛程式中 Llama 3.1 405b 的設定範例:

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

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

然後用做一般模型:

const llmResponse = await generate({
  model: llama31,
  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 指令執行。

索引和擷取器

Genkit Vertex AI 外掛程式提供索引和擷取器實作項目 這項解決方案支援 Vertex AI Vector Search 服務。

(如要瞭解索引工具的使用方式,請參閱擷取評估產生作業頁面 和擷取器會用於 RAG 實作中)。

Vertex AI Vector Search 服務是一種文件索引,可與 您選擇的文件儲存庫 - 文件儲存庫內含 而 Vertex AI Vector Search 索引包含每份文件 以及文件儲存庫中文件的參照 Vertex AI Vector Search 將文件編入索引後, 回應搜尋查詢,然後在文件儲存庫產生索引清單。

Vertex AI 外掛程式使用的索引和擷取器實作項目 使用 Cloud Firestore 或 BigQuery 做為文件存放區外掛程式 可讓您實作的介面以支援其他文件存放區。

如何使用 Vertex AI Vector Search:

  1. 選擇嵌入模型。這個模型負責 以及文字嵌入進階使用者或許可以使用最佳化的嵌入模型 但對大多數使用者來說 text-embedding-004 模型適用於英文和 text-multilingual-embedding-002 模型適用於多語言文字。

  2. Vector Search 中 專區建立新索引。最重要的 設定如下:

    • 維度:指定維度產生的向量維度 分類模型text-embedding-004text-multilingual-embedding-002 模型會產生 768 的向量 維度。

    • 更新方法:選取串流更新。

    建立索引後,請部署至標準 (公開) 端點。

  3. 針對要使用的文件儲存區取得文件索引工具和擷取器:

    Cloud Firestore

    import {
      getFirestoreDocumentIndexer,
      getFirestoreDocumentRetriever
    } from '@genkit-ai/vertexai';
    
    import { initializeApp } from 'firebase-admin/app';
    import { getFirestore } from 'firebase-admin/firestore';
    
    initializeApp({ projectId: PROJECT_ID });
    const db = getFirestore();
    
    const firestoreDocumentRetriever: DocumentRetriever =
      getFirestoreDocumentRetriever(db, FIRESTORE_COLLECTION);
    const firestoreDocumentIndexer: DocumentIndexer =
      getFirestoreDocumentIndexer(db, FIRESTORE_COLLECTION);
    

    BigQuery

    import {
      getBigQueryDocumentIndexer,
      getBigQueryDocumentRetriever,
    } from '@genkit-ai/vertexai';
    import { BigQuery } from '@google-cloud/bigquery';
    
    const bq = new BigQuery({ projectId: PROJECT_ID });
    
    const bigQueryDocumentRetriever: DocumentRetriever =
      getBigQueryDocumentRetriever(bq, BIGQUERY_TABLE, BIGQUERY_DATASET);
    const bigQueryDocumentIndexer: DocumentIndexer =
      getBigQueryDocumentIndexer(bq, BIGQUERY_TABLE, BIGQUERY_DATASET);
    

    其他

    如要支援其他文件存放區,請自行提供實作項目 「DocumentRetriever」和「DocumentIndexer」:

    const myDocumentRetriever: DocumentRetriever = async (neighbors: Neighbor[]) => {
      // Return the documents referenced by `neighbors`.
      // ...
    }
    const myDocumentIndexer: DocumentIndexer = async (documents: Document[]) => {
      // Add `documents` to storage.
      // ...
    }
    

    如需範例,請參閱包含本機檔案的 Vertex AI 外掛程式擷取與索引工具範例

  4. vectorSearchOptions 區塊新增至 vertexAI 外掛程式設定:

    import { configureGenkit } from '@genkit-ai/core';
    import { vertexAI, textEmbedding004 } from '@genkit-ai/vertexai';
    
    configureGenkit({
      plugins: [
        vertexAI({
          projectId: PROJECT_ID,
          location: LOCATION,
          vectorSearchOptions: [
            {
              indexId: VECTOR_SEARCH_INDEX_ID,
              indexEndpointId: VECTOR_SEARCH_INDEX_ENDPOINT_ID,
              deployedIndexId: VECTOR_SEARCH_DEPLOYED_INDEX_ID,
              publicDomainName: VECTOR_SEARCH_PUBLIC_DOMAIN_NAME,
              documentRetriever: firestoreDocumentRetriever,
              documentIndexer: firestoreDocumentIndexer,
              embedder: textEmbedding004,
            },
          ],
        }),
      ],
    });
    

    請提供您在第一個步驟中選擇的嵌入器,並提供文件索引工具 您在上一個步驟中建立的擷取和擷取器

    如要將外掛程式設定為使用您先前建立的 Vector Search 索引, 您需要提供多個值;您可以在 Vector Search 中找到 「管理」部分:

    • indexId:列在「索引」分頁中
    • indexEndpointId:列於「索引端點」分頁
    • deployedIndexIdpublicDomainName:列在「已部署的索引」中 資訊」按一下已部署索引的名稱即可開啟頁面 在前述任一分頁上
  5. 完成所有設定後,請匯入擷取器和索引工具參照 外掛程式:

    import {
      vertexAiIndexerRef,
      vertexAiRetrieverRef,
    } from '@genkit-ai/vertexai';
    

    傳遞 index()retrieve() 的參照:

    await index({
      indexer: vertexAiIndexerRef({
        indexId: VECTOR_SEARCH_INDEX_ID,
      }),
      documents,
    });
    
    const res = await retrieve({
      retriever: vertexAiRetrieverRef({
        indexId: VECTOR_SEARCH_INDEX_ID,
      }),
      query: queryDocument,
    });
    

請參閱下列程式碼範例: