編寫 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 模型外掛程式能在 Genkit 註冊資料庫中新增一或多個生成式 AI 模型。模型代表任何 能接收輸入提示並生成文字、媒體或資料模型 一般來說,模型外掛程式會在初始化函式中發出一或多個 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 Telemetry 外掛程式

發布外掛程式

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.
}