編寫 Genkit 外掛程式

Firebase Genkit 的功能可透過外掛程式擴充。Genkit 外掛程式是可設定的模組,可提供模型、擷取器、索引器、追蹤記錄儲存庫等。您已透過 Genkit 查看外掛程式的實際運作情形:

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

configureGenkit({
  plugins: [vertexAI({ projectId: 'my-project' })],
});

Vertex AI 外掛程式會採用設定 (例如使用者的 Google Cloud 專案 ID),並透過 Genkit 註冊資料庫註冊多種新模型、嵌入程式等。這個註冊資料庫為 Genkit 的本機 UI 執行及檢查模型、提示等內容,並可做為執行階段中已命名動作的查詢服務。

建立外掛程式

如要建立外掛程式,通常建議您建立新的 NPM 套件:

mkdir genkitx-my-plugin
cd genkitx-my-plugin
npm init -y
npm i --save @genkit-ai/core
npm i --save-dev typescript
npx tsc --init

接著,定義並匯出主要進入點中的外掛程式:

import { genkitPlugin } from '@genkit-ai/core';

interface MyPluginOptions {
  // add any plugin configuration here
}

export const myPlugin = genkitPlugin(
  'my-plugin',
  async (options: MyPluginOptions) => {
    // initialize your plugin here...
  }
);

外掛程式選項指南

一般來說,外掛程式應使用單一 options 引數,其中包含運作所需的任何外掛程式設定。針對需要密鑰值的外掛程式選項 (例如 API 金鑰),您應同時提供進行設定選項和預設環境變數的選項:

import { genkitPlugin, GenkitError } from '@genkit-ai/core';

interface MyPluginOptions {
  apiKey?: string;
}

export const myPlugin = genkitPlugin(
  'my-plugin',
  async (options: MyPluginOptions) => {
    const apiKey = options.apiKey || process.env.MY_PLUGIN_API_KEY;
    if (!apiKey)
      throw new GenkitError({
        source: 'my-plugin',
        status: 'INVALID_ARGUMENT',
        message:
          'Must supply either `options.apiKey` or set `MY_PLUGIN_API_KEY` environment variable.',
      });
    // ... continue initialization
  }
);

建構外掛程式

只需一個外掛程式,就能啟用 Genkit 中的許多新功能。例如,Vertex AI 外掛程式會啟用多個新模型及嵌入器。

模型外掛程式

Genkit 模型外掛程式會將一或多個生成式 AI 模型新增至 Genkit 登錄檔,模型代表任何能接收提示做為輸入及產生文字、媒體或資料輸出的生成式模型。一般來說,模型外掛程式會在初始化函式中發出一或多個 defineModel 呼叫。

自訂模型通常由三個元件組成:

  1. 定義模型功能的中繼資料。
  2. 包含模型支援的任何特定參數的設定結構定義。
  3. 實作模型接受 GenerateRequest 並傳回 GenerateResponse 的函式。

如要建構模型外掛程式,您需要使用 @genkit-ai/ai 套件:

npm i --save @genkit-ai/ai

整體來說,模型外掛程式看起來可能會像這樣:

import { genkitPlugin, GenkitError } from '@genkit-ai/core';
import { defineModel, GenerationCommonConfigSchema } from '@genkit-ai/ai/model';
import { simulateSystemPrompt } from '@genkit-ai/ai/model/middleware';
import { z } from 'zod';

export const myPlugin = genkitPlugin('my-plugin', async (options: {apiKey?: string}) => {
  defineModel({
    // be sure to include your plugin as a provider prefix
    name: 'my-plugin/my-model',
    // label for your model as shown in Genkit Developer UI
    label: 'My Awesome Model',
    // optional list of supported versions of your model
    versions: ['my-model-001', 'my-model-001'],
    // model support attributes
    supports: {
      multiturn: true, // true if your model supports conversations
      media: true, // true if your model supports multimodal input
      tools: true, // true if your model supports tool/function calling
      systemRole: true, // true if your model supports the system role
      output: ['text', 'media', 'json'], // types of output your model supports
    },
    // Zod schema for your model's custom configuration
    configSchema: GenerationCommonConfigSchema.extend({
      safetySettings: z.object({...}),
    }),
    // list of middleware for your model to use
    use: [simulateSystemPrompt()]
  }, async request => {
    const myModelRequest = toMyModelRequest(request);
    const myModelResponse = await myModelApi(myModelRequest);
    return toGenerateResponse(myModelResponse);
  });
});

轉換要求與回應

Genkit 模型外掛程式的主要工作是將 GenerateRequest 從 Genkit 通用格式轉換成可由模型 API 辨識及支援的格式,然後將模型的回應轉換為 Genkit 使用的 GenerateResponseData 格式。

某些情況下,您可能需要放大或操控資料,才能解決模型限制。舉例來說,如果您的模型原生支援 system 訊息,您可能需要將提示的系統訊息轉換為使用者/模型訊息組合。

模型參照

使用 defineModel 註冊模型後,在依名稱要求的情況下,可隨時使用該模型。然而,如要改善輸入和 IDE 自動完成功能,您可以從套件匯出模型參照,其中僅包含模型的中繼資料,但不包括模型的實作:

import { modelRef } from "@genkit-ai/ai/model";

export myModelRef = modelRef({
  name: "my-plugin/my-model",
  configSchema: MyConfigSchema,
  info: {
    // ... model-specific info
  },
})

呼叫 generate() 時,模型參照和字串模型名稱可以交替使用:

import { myModelRef } from 'genkitx-my-plugin';
import { generate } from '@genkit-ai/ai';

generate({ model: myModelRef });
// is equivalent to
generate({ model: 'my-plugin/my-model' });

遙測外掛程式

請參閱編寫 Genkit 遙測外掛程式

發布外掛程式

Genkit 外掛程式能以一般 NPM 套件的形式發布。為提升曝光度並盡可能提高一致性,套件應命名為 genkitx-{name},以指出該套件為 Genkit 外掛程式,且您應在 package.json 中加入下列大量與外掛程式相關的 keywords

  • genkit-plugin:一律在套件中加入這個關鍵字,以表示這是 Genkit 外掛程式。
  • genkit-model:如果套件定義了任何模型,請加入這個關鍵字。
  • genkit-retriever:如果套件定義了任何擷取器,請加入這個關鍵字。
  • genkit-indexer:如果套件定義了任何索引器,請加入這個關鍵字。
  • genkit-embedder:如果套件定義了任何索引器,請加入這個關鍵字。
  • genkit-tracestore:如果套件定義了任何追蹤記錄存放區,請加入這個關鍵字。
  • genkit-statestore:如果套件定義了任何狀態存放區,請加入這個關鍵字。
  • genkit-telemetry:如果套件定義了遙測提供者,請加入這個關鍵字。
  • genkit-deploy:如果套件內含協助雲端服務供應商部署 Genkit 應用程式的輔助程式,請加入這個關鍵字。
  • genkit-flow:如果套件可強化 Genkit 流程,請加入這個關鍵字。

提供擷取器、嵌入器和模型的外掛程式,其 package.json 可能如下所示:

{
  "name": "genkitx-my-plugin",
  "keywords": ["genkit-plugin", "genkit-retriever", "genkit-embedder", "genkit-model"],
  // ... dependencies etc.
}