Firebase Genkit

Genkit は、AI を活用したアプリケーションと機能の構築に役立つように設計されたフレームワークです。Node.js および Go 用のオープンソース ライブラリに加え、テストとデバッグ用のデベロッパー ツールも用意されています。

このドキュメントでは、Node.js 用の Genkit について説明します。Go 開発者の方は、Genkit Go のドキュメントをご覧ください。

Node.js がサポートされている場所ならどこでも、Genkit ライブラリをデプロイして実行できます。あらゆる生成 AI モデル API やベクトル データベースと連携するように設計されています。Firebase と Google Cloud のインテグレーションも提供していますが、Genkit は Google サービスから独立して使用できます。

始めましょう

主な機能

AI 生成のための統合 API 1 つの API を使用して、さまざまな AI モデルからコンテンツを生成またはストリーミングできます。マルチモーダル入出力設定とカスタムモデル設定と連携します。
構造化生成 組み込みの検証機能を使用して、構造化オブジェクト(JSON など)を生成またはストリーミングできます。アプリとの統合を簡素化し、非構造化データを使用可能な形式に変換します。
ツールの呼び出し AI モデルがタスクを完了するためのツールとして関数と API を呼び出すようにします。いつ、どのツールを使用するかはモデルが判断します。
検索拡張生成 データを統合することで、生成された出力の精度と関連性を向上させます。シンプルな API を使用して、さまざまなソースからの情報の埋め込み、インデックス登録、取得を行うことができます。
プロンプト テンプレート リッチテキスト テンプレート、モデル設定、マルチモーダル サポート、ツール統合を��む効果�������ロンプトを、コンパクトで実行可能なプロンプト ファイル内にすべて作成します。

これらの機能をコードで使用する具体的な方法については、次のコードサンプルを参照してください。

基本生成

import { generate } from `@genkit-ai/ai`;
import { gemini15Flash, claude3Sonnet, llama31 } from '@genkit-ai/vertexai';
import { gpt4o } from 'genkitx-openai';

// Use the same API to generate content from many models
const result = await generate({
    model: gemini15Flash, // Or use claude3Sonnet, llama31, gpt4o
    prompt: 'What makes you the best LLM out there?',
});

構造化生成

import { generate } from `@genkit-ai/ai`;
import { gemini15Flash } from `@genkit-ai/googleai`;
import { z } from `zod`;

const result = await generate({
    model: gemini15Flash,
    prompt: 'Create a brief profile for a character in a fantasy video game.',
    // Specify output structure using Zod schema
    output: {
        schema: z.object({
            name: z.string(),
            role: z.enum(['knight', 'mage', 'archer']),
            backstory: z.string(),
            attacks: z.array(z.object({
              name: z.string(),
              damage: z.number().describe('amount of damage, between 2 and 25'),
            })).describe('3 attacks the character can use')
        })
    }
});

ツールの呼び出し

import { generate, defineTool } from `@genkit-ai/ai`;
import { gemini15Flash } from `@genkit-ai/googleai`;
import { z } from `zod`;

// Define tool to get weather data for a given location
const lookupWeather = defineTool({
    name: 'lookupWeather',
    description: 'Get the current weather in a location.',
    // Define input and output schema so the model knows how to use the tool
    inputSchema: z.object({
        location: z.string().describe('The location to get the weather for.'),
    }),
    outputSchema: z.object({
        temperature: z.number().describe('The current temperature in Fahrenheit.'),
        condition: z.string().describe('A brief description of the weather conditions.'),
    }),
    async (input) => {
        // Insert weather lookup API code
    }
});

const result = await generate({
    model: gemini15Flash,
    tools: [lookupWeather], // Give the model a list of tools it can call
    prompt: 'What is the weather like in New York? ',
});

取得

import { generate, retrieve } from `@genkit-ai/ai`;
import { devLocalRetrieverRef } from '@genkit-ai/dev-local-vectorstore';
import { gemini15Flash } from `@genkit-ai/googleai`;

// Sample assumes Genkit documentation has been chunked, stored, and indexed in 
// local vectorstore in previous step.

// Reference to a local vector database storing Genkit documentation
const retriever = devLocalRetrieverRef('genkitQA');

const query = 'How do I retrieve relevant documents in Genkit?'

// Consistent API to retrieve most relevant documents based on semantic similarity to query
const docs = await retrieve({
    retriever: retriever,
    query: query,
    options: { limit: 5 },
});

const result = await generate({
    model: gemini15Flash
    prompt: 'Use the provided context from the Genkit documentation to answer this query: ${query}',
    context: docs // Pass retrieved documents to the model
});

プロンプト テンプレート

---
model: vertexai/gemini-1.5-flash
config:
  temperature: 0.9
input:
  schema:
    properties:
      location: {type: string}
      style: {type: string}
      name: {type: string}
    required: [location]
  default:
    location: a restaurant
---

You are the most welcoming AI assistant and are currently working at {{location}}.

Greet a guest{{#if name}} named {{name}}{{/if}}{{#if style}} in the style of {{style}}{{/if}}.

開発ツール

Genkit は、AI アプリケーションの構築を容易にするコマンドライン インターフェース(CLI)とローカルのデベロッパー UI を提供します。これらのツールを使うと、次のことを行えます。

  • テスト: AI 関数、プロンプト、クエリをテストして改良します。
  • デバッグ: 詳細な実行トレースで問題を見つけて修正します。
  • 評価: 生成された結果を複数のテストケースで評価します。

ソーシャル メディアで

  • コミュニティに参加する: Discord サーバーで、最新情報の入手、質問の投稿、作品の共有ができます。
  • フィードバックを送信する: GitHub の Issue Tracker を使用して問題を報告したり、新機能を提案したりできます。

次のステップ

スタートガイドで、Genkit を使用して最初の AI アプリケーションを構築する方法を確認する。