genkit

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Firebase Genkit

Firebase Genkit

When to use this skill

何时使用该Skill

  • AI workflow orchestration: Building multi-step AI pipelines with type-safe inputs/outputs
  • Flow-based APIs: Wrapping LLM calls into deployable HTTP endpoints
  • Tool calling / agents: Equipping models with custom tools and implementing agentic loops
  • RAG pipelines: Retrieval-augmented generation with vector databases (Pinecone, pgvector, Firestore, Chroma, etc.)
  • Multi-agent systems: Coordinating multiple specialized AI agents
  • Streaming responses: Real-time token-by-token output for chat or long-form content
  • Firebase/Cloud Run deployment: Deploying AI functions to Google Cloud
  • Prompt management: Managing prompts as versioned
    .prompt
    files with Dotprompt

  • AI工作流编排:构建具备类型安全输入/输出的多步骤AI管道
  • 基于流程的API:将大语言模型调用封装为可部署的HTTP端点
  • 工具调用/Agent:为模型配备自定义工具并实现智能体循环
  • RAG管道:结合向量数据库(Pinecone、pgvector、Firestore、Chroma等)的检索增强生成
  • 多Agent系统:协调多个专业AI智能体
  • 流式响应:为聊天或长文本内容提供实时逐Token输出
  • Firebase/Cloud Run部署:将AI函数部署到Google Cloud
  • 提示词管理:使用Dotprompt将提示词作为版本化的
    .prompt
    文件进行管理

Installation & Setup

安装与设置

Step 1: Install the Genkit CLI

步骤1:安装Genkit CLI

bash
undefined
bash
undefined

npm (recommended for JavaScript/TypeScript)

npm(推荐用于JavaScript/TypeScript)

npm install -g genkit-cli
npm install -g genkit-cli

macOS/Linux binary

macOS/Linux二进制包

curl -sL cli.genkit.dev | bash
undefined
curl -sL cli.genkit.dev | bash
undefined

Step 2: Create a TypeScript project

步骤2:创建TypeScript项目

bash
mkdir my-genkit-app && cd my-genkit-app
npm init -y
npm pkg set type=module
npm install -D typescript tsx
npx tsc --init
mkdir src && touch src/index.ts
bash
mkdir my-genkit-app && cd my-genkit-app
npm init -y
npm pkg set type=module
npm install -D typescript tsx
npx tsc --init
mkdir src && touch src/index.ts

Step 3: Install Genkit core and a model plugin

步骤3:安装Genkit核心库和模型插件

bash
undefined
bash
undefined

Core + Google AI (Gemini) — free tier, no credit card required

核心库 + Google AI(Gemini)——免费层级,无需信用卡

npm install genkit @genkit-ai/google-genai
npm install genkit @genkit-ai/google-genai

Or: Vertex AI (requires GCP project)

或:Vertex AI(需要GCP项目)

npm install genkit @genkit-ai/vertexai
npm install genkit @genkit-ai/vertexai

Or: OpenAI

或:OpenAI

npm install genkit genkitx-openai
npm install genkit genkitx-openai

Or: Anthropic (Claude)

或:Anthropic(Claude)

npm install genkit genkitx-anthropic
npm install genkit genkitx-anthropic

Or: Ollama (local models)

或:Ollama(本地模型)

npm install genkit genkitx-ollama
undefined
npm install genkit genkitx-ollama
undefined

Step 4: Configure API Key

步骤4:配置API密钥

bash
undefined
bash
undefined

Google AI (Gemini)

Google AI(Gemini)

export GEMINI_API_KEY=your_key_here
export GEMINI_API_KEY=your_key_here

OpenAI

OpenAI

export OPENAI_API_KEY=your_key_here
export OPENAI_API_KEY=your_key_here

Anthropic

Anthropic

export ANTHROPIC_API_KEY=your_key_here

---
export ANTHROPIC_API_KEY=your_key_here

---

Core Concepts

核心概念

Initializing Genkit

初始化Genkit

typescript
import { googleAI } from '@genkit-ai/google-genai';
import { genkit } from 'genkit';

const ai = genkit({
  plugins: [googleAI()],
  model: googleAI.model('gemini-2.5-flash'), // default model
});
typescript
import { googleAI } from '@genkit-ai/google-genai';
import { genkit } from 'genkit';

const ai = genkit({
  plugins: [googleAI()],
  model: googleAI.model('gemini-2.5-flash'), // 默认模型
});

Defining Flows

定义流程

Flows are the core primitive: type-safe, observable, deployable AI functions.
typescript
import { genkit, z } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';

const ai = genkit({ plugins: [googleAI()] });

// Input/output schemas with Zod
const SummaryInputSchema = z.object({
  text: z.string().describe('Text to summarize'),
  maxWords: z.number().optional().default(100),
});

const SummaryOutputSchema = z.object({
  summary: z.string(),
  keyPoints: z.array(z.string()),
});

export const summarizeFlow = ai.defineFlow(
  {
    name: 'summarizeFlow',
    inputSchema: SummaryInputSchema,
    outputSchema: SummaryOutputSchema,
  },
  async ({ text, maxWords }) => {
    const { output } = await ai.generate({
      model: googleAI.model('gemini-2.5-flash'),
      prompt: `Summarize the following text in at most ${maxWords} words and extract key points:\n\n${text}`,
      output: { schema: SummaryOutputSchema },
    });

    if (!output) throw new Error('No output generated');
    return output;
  }
);

// Call the flow
const result = await summarizeFlow({
  text: 'Long article content here...',
  maxWords: 50,
});
console.log(result.summary);
流程是核心基础单元:具备类型安全、可观测、可部署的AI函数。
typescript
import { genkit, z } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';

const ai = genkit({ plugins: [googleAI()] });

// 使用Zod定义输入/输出模式
const SummaryInputSchema = z.object({
  text: z.string().describe('需要总结的文本'),
  maxWords: z.number().optional().default(100),
});

const SummaryOutputSchema = z.object({
  summary: z.string(),
  keyPoints: z.array(z.string()),
});

export const summarizeFlow = ai.defineFlow(
  {
    name: 'summarizeFlow',
    inputSchema: SummaryInputSchema,
    outputSchema: SummaryOutputSchema,
  },
  async ({ text, maxWords }) => {
    const { output } = await ai.generate({
      model: googleAI.model('gemini-2.5-flash'),
      prompt: `将以下文本总结为最多${maxWords}个单词,并提取关键点:\n\n${text}`,
      output: { schema: SummaryOutputSchema },
    });

    if (!output) throw new Error('未生成输出内容');
    return output;
  }
);

// 调用流程
const result = await summarizeFlow({
  text: '长文章内容...',
  maxWords: 50,
});
console.log(result.summary);

Generating Content

生成内容

typescript
// Simple text generation
const { text } = await ai.generate({
  model: googleAI.model('gemini-2.5-flash'),
  prompt: 'Explain quantum computing in one sentence.',
});

// Structured output
const { output } = await ai.generate({
  prompt: 'List 3 programming languages with their use cases',
  output: {
    schema: z.object({
      languages: z.array(z.object({
        name: z.string(),
        useCase: z.string(),
      })),
    }),
  },
});

// With system prompt
const { text: response } = await ai.generate({
  system: 'You are a senior TypeScript engineer. Be concise.',
  prompt: 'What is the difference between interface and type in TypeScript?',
});

// Multimodal (image + text)
const { text: description } = await ai.generate({
  prompt: [
    { text: 'What is in this image?' },
    { media: { url: 'https://example.com/image.jpg', contentType: 'image/jpeg' } },
  ],
});
typescript
// 简单文本生成
const { text } = await ai.generate({
  model: googleAI.model('gemini-2.5-flash'),
  prompt: '用一句话解释量子计算。',
});

// 结构化输出
const { output } = await ai.generate({
  prompt: '列出3种编程语言及其应用场景',
  output: {
    schema: z.object({
      languages: z.array(z.object({
        name: z.string(),
        useCase: z.string(),
      })),
    }),
  },
});

// 结合系统提示词
const { text: response } = await ai.generate({
  system: '你是一名资深TypeScript工程师,请保持回答简洁。',
  prompt: 'TypeScript中的interface和type有什么区别?',
});

// 多模态(图片+文本)
const { text: description } = await ai.generate({
  prompt: [
    { text: '这张图片里有什么?' },
    { media: { url: 'https://example.com/image.jpg', contentType: 'image/jpeg' } },
  ],
});

Streaming Flows

流式流程

typescript
export const streamingFlow = ai.defineFlow(
  {
    name: 'streamingFlow',
    inputSchema: z.object({ topic: z.string() }),
    streamSchema: z.string(),        // type of each chunk
    outputSchema: z.object({ full: z.string() }),
  },
  async ({ topic }, { sendChunk }) => {
    const { stream, response } = ai.generateStream({
      prompt: `Write a detailed essay about ${topic}.`,
    });

    for await (const chunk of stream) {
      sendChunk(chunk.text);         // stream each token to client
    }

    const { text } = await response;
    return { full: text };
  }
);

// Client-side consumption
const stream = streamingFlow.stream({ topic: 'AI ethics' });
for await (const chunk of stream.stream) {
  process.stdout.write(chunk);
}
const finalOutput = await stream.output;
typescript
export const streamingFlow = ai.defineFlow(
  {
    name: 'streamingFlow',
    inputSchema: z.object({ topic: z.string() }),
    streamSchema: z.string(),        // 每个分块的类型
    outputSchema: z.object({ full: z.string() }),
  },
  async ({ topic }, { sendChunk }) => {
    const { stream, response } = ai.generateStream({
      prompt: `写一篇关于${topic}的详细文章。`,
    });

    for await (const chunk of stream) {
      sendChunk(chunk.text);         // 将每个Token流式传输给客户端
    }

    const { text } = await response;
    return { full: text };
  }
);

// 客户端消费流
const stream = streamingFlow.stream({ topic: 'AI伦理' });
for await (const chunk of stream.stream) {
  process.stdout.write(chunk);
}
const finalOutput = await stream.output;

Tool Calling (Agents)

工具调用(Agent)

typescript
import { z } from 'genkit';

// Define tools
const getWeatherTool = ai.defineTool(
  {
    name: 'getWeather',
    description: 'Get current weather for a city',
    inputSchema: z.object({ city: z.string() }),
    outputSchema: z.object({ temp: z.number(), condition: z.string() }),
  },
  async ({ city }) => {
    // Call real weather API
    return { temp: 22, condition: 'sunny' };
  }
);

const searchWebTool = ai.defineTool(
  {
    name: 'searchWeb',
    description: 'Search the web for information',
    inputSchema: z.object({ query: z.string() }),
    outputSchema: z.string(),
  },
  async ({ query }) => {
    // Call search API
    return `Search results for: ${query}`;
  }
);

// Agent flow with tools
export const agentFlow = ai.defineFlow(
  {
    name: 'agentFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.string(),
  },
  async ({ question }) => {
    const { text } = await ai.generate({
      prompt: question,
      tools: [getWeatherTool, searchWebTool],
      returnToolRequests: false, // auto-execute tools
    });
    return text;
  }
);
typescript
import { z } from 'genkit';

// 定义工具
const getWeatherTool = ai.defineTool(
  {
    name: 'getWeather',
    description: '获取城市当前天气',
    inputSchema: z.object({ city: z.string() }),
    outputSchema: z.object({ temp: z.number(), condition: z.string() }),
  },
  async ({ city }) => {
    // 调用真实天气API
    return { temp: 22, condition: 'sunny' };
  }
);

const searchWebTool = ai.defineTool(
  {
    name: 'searchWeb',
    description: '在网络上搜索信息',
    inputSchema: z.object({ query: z.string() }),
    outputSchema: z.string(),
  },
  async ({ query }) => {
    // 调用搜索API
    return `搜索结果:${query}`;
  }
);

// 集成工具的Agent流程
export const agentFlow = ai.defineFlow(
  {
    name: 'agentFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.string(),
  },
  async ({ question }) => {
    const { text } = await ai.generate({
      prompt: question,
      tools: [getWeatherTool, searchWebTool],
      returnToolRequests: false, // 自动执行工具
    });
    return text;
  }
);

Prompts with Dotprompt

使用Dotprompt管理提示词

Manage prompts as versioned
.prompt
files:
undefined
将提示词作为版本化的
.prompt
文件进行管理:
undefined

src/prompts/summarize.prompt

src/prompts/summarize.prompt


model: googleai/gemini-2.5-flash input: schema: text: string style?: string output: schema: summary: string sentiment: string

Summarize the following text in a {{style, default: "professional"}} tone:
{{text}}
Return JSON with summary and sentiment (positive/negative/neutral).

```typescript
// Load and use dotprompt
const summarizePrompt = ai.prompt('summarize');
const { output } = await summarizePrompt({
  text: 'Article content here...',
  style: 'casual',
});

model: googleai/gemini-2.5-flash input: schema: text: string style?: string output: schema: summary: string sentiment: string

将以下文本以{{style, default: "professional"}}风格进行总结:
{{text}}
返回包含summary和sentiment(positive/negative/neutral)的JSON格式结果。

```typescript
// 加载并使用dotprompt
const summarizePrompt = ai.prompt('summarize');
const { output } = await summarizePrompt({
  text: '文章内容...',
  style: 'casual',
});

RAG — Retrieval-Augmented Generation

RAG——检索增强生成

typescript
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { textEmbedding004 } from '@genkit-ai/google-genai';

const ai = genkit({
  plugins: [
    googleAI(),
    devLocalVectorstore([{
      indexName: 'documents',
      embedder: textEmbedding004,
    }]),
  ],
});

// Index documents
await ai.index({
  indexer: devLocalVectorstoreIndexer('documents'),
  docs: [
    { content: [{ text: 'Document 1 content...' }], metadata: { source: 'doc1' } },
    { content: [{ text: 'Document 2 content...' }], metadata: { source: 'doc2' } },
  ],
});

// RAG flow
export const ragFlow = ai.defineFlow(
  {
    name: 'ragFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.string(),
  },
  async ({ question }) => {
    // Retrieve relevant documents
    const docs = await ai.retrieve({
      retriever: devLocalVectorstoreRetriever('documents'),
      query: question,
      options: { k: 3 },
    });

    // Generate answer grounded in retrieved docs
    const { text } = await ai.generate({
      system: 'Answer questions using only the provided context.',
      prompt: question,
      docs,
    });

    return text;
  }
);
typescript
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { textEmbedding004 } from '@genkit-ai/google-genai';

const ai = genkit({
  plugins: [
    googleAI(),
    devLocalVectorstore([{
      indexName: 'documents',
      embedder: textEmbedding004,
    }]),
  ],
});

// 索引文档
await ai.index({
  indexer: devLocalVectorstoreIndexer('documents'),
  docs: [
    { content: [{ text: '文档1内容...' }], metadata: { source: 'doc1' } },
    { content: [{ text: '文档2内容...' }], metadata: { source: 'doc2' } },
  ],
});

// RAG流程
export const ragFlow = ai.defineFlow(
  {
    name: 'ragFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.string(),
  },
  async ({ question }) => {
    // 检索相关文档
    const docs = await ai.retrieve({
      retriever: devLocalVectorstoreRetriever('documents'),
      query: question,
      options: { k: 3 },
    });

    // 基于检索到的文档生成答案
    const { text } = await ai.generate({
      system: '仅使用提供的上下文回答问题。',
      prompt: question,
      docs,
    });

    return text;
  }
);

Chat Sessions

聊天会话

typescript
export const chatFlow = ai.defineFlow(
  {
    name: 'chatFlow',
    inputSchema: z.object({ message: z.string(), sessionId: z.string() }),
    outputSchema: z.string(),
  },
  async ({ message, sessionId }) => {
    const session = ai.loadSession(sessionId) ?? ai.createSession({ sessionId });
    const chat = session.chat({
      system: 'You are a helpful assistant.',
    });

    const { text } = await chat.send(message);
    return text;
  }
);
typescript
export const chatFlow = ai.defineFlow(
  {
    name: 'chatFlow',
    inputSchema: z.object({ message: z.string(), sessionId: z.string() }),
    outputSchema: z.string(),
  },
  async ({ message, sessionId }) => {
    const session = ai.loadSession(sessionId) ?? ai.createSession({ sessionId });
    const chat = session.chat({
      system: '你是一个乐于助人的助手。',
    });

    const { text } = await chat.send(message);
    return text;
  }
);

Multi-Agent Systems

多Agent系统

typescript
// Specialist agents
const researchAgent = ai.defineFlow(
  { name: 'researchAgent', inputSchema: z.string(), outputSchema: z.string() },
  async (query) => {
    const { text } = await ai.generate({
      system: 'You are a research expert. Gather facts and cite sources.',
      prompt: query,
      tools: [searchWebTool],
    });
    return text;
  }
);

const writerAgent = ai.defineFlow(
  { name: 'writerAgent', inputSchema: z.string(), outputSchema: z.string() },
  async (brief) => {
    const { text } = await ai.generate({
      system: 'You are a professional writer. Write clear, engaging content.',
      prompt: brief,
    });
    return text;
  }
);

// Orchestrator delegates to specialists
export const contentPipelineFlow = ai.defineFlow(
  {
    name: 'contentPipelineFlow',
    inputSchema: z.object({ topic: z.string() }),
    outputSchema: z.string(),
  },
  async ({ topic }) => {
    const research = await researchAgent(`Research: ${topic}`);
    const article = await writerAgent(`Write an article based on: ${research}`);
    return article;
  }
);

typescript
// 专业Agent
const researchAgent = ai.defineFlow(
  { name: 'researchAgent', inputSchema: z.string(), outputSchema: z.string() },
  async (query) => {
    const { text } = await ai.generate({
      system: '你是一名研究专家,收集事实并引用来源。',
      prompt: query,
      tools: [searchWebTool],
    });
    return text;
  }
);

const writerAgent = ai.defineFlow(
  { name: 'writerAgent', inputSchema: z.string(), outputSchema: z.string() },
  async (brief) => {
    const { text } = await ai.generate({
      system: '你是一名专业作家,撰写清晰、引人入胜的内容。',
      prompt: brief,
    });
    return text;
  }
);

// 编排器分配任务给专业Agent
export const contentPipelineFlow = ai.defineFlow(
  {
    name: 'contentPipelineFlow',
    inputSchema: z.object({ topic: z.string() }),
    outputSchema: z.string(),
  },
  async ({ topic }) => {
    const research = await researchAgent(`研究主题:${topic}`);
    const article = await writerAgent(`基于以下内容撰写文章:${research}`);
    return article;
  }
);

Developer Tools

开发者工具

CLI Commands

CLI命令

bash
undefined
bash
undefined

Start Developer UI + connect to your app

启动开发者UI并连接到你的应用

genkit start -- npx tsx --watch src/index.ts genkit start -o -- npx tsx src/index.ts # auto-open browser
genkit start -- npx tsx --watch src/index.ts genkit start -o -- npx tsx src/index.ts # 自动打开浏览器

Run a specific flow from CLI

从CLI运行指定流程

genkit flow:run summarizeFlow '{"text": "Hello world", "maxWords": 10}'
genkit flow:run summarizeFlow '{"text": "Hello world", "maxWords": 10}'

Run with streaming output

以流式输出运行流程

genkit flow:run streamingFlow '{"topic": "AI"}' -s
genkit flow:run streamingFlow '{"topic": "AI"}' -s

Evaluate a flow

评估流程

genkit eval:flow ragFlow --input eval-inputs.json
genkit eval:flow ragFlow --input eval-inputs.json

View all commands

查看所有命令

genkit --help
genkit --help

Disable analytics telemetry

禁用分析遥测

genkit config set analyticsOptOut true
undefined
genkit config set analyticsOptOut true
undefined

Developer UI

开发者UI

The Developer UI runs at http://localhost:4000 and provides:
  • Flow runner: Execute flows with custom JSON inputs
  • Trace inspector: Visualize each step (generate, embed, retrieve, tool calls)
  • Prompt playground: Test prompts interactively
  • Model tester: Compare outputs across different models
  • Evaluator: Run evaluation datasets against flows
bash
undefined
  • 流程运行器:使用自定义JSON输入执行流程
  • 追踪检查器:可视化每个步骤(生成、嵌入、检索、工具调用)
  • 提示词 playground:交互式测试提示词
  • 模型测试器:对比不同模型的输出
  • 评估器:针对流程运行评估数据集
bash
undefined

Add npm script for convenience

添加便捷的npm脚本

package.json

package.json

"scripts": { "genkit:dev": "genkit start -- npx tsx --watch src/index.ts" }
npm run genkit:dev

---
"scripts": { "genkit:dev": "genkit start -- npx tsx --watch src/index.ts" }
npm run genkit:dev

---

Deployment

部署

Firebase Cloud Functions

Firebase Cloud Functions

typescript
import { onCallGenkit } from 'firebase-functions/https';
import { defineSecret } from 'firebase-functions/params';

const apiKey = defineSecret('GOOGLE_AI_API_KEY');

export const summarize = onCallGenkit(
  { secrets: [apiKey] },
  summarizeFlow
);
bash
firebase deploy --only functions
typescript
import { onCallGenkit } from 'firebase-functions/https';
import { defineSecret } from 'firebase-functions/params';

const apiKey = defineSecret('GOOGLE_AI_API_KEY');

export const summarize = onCallGenkit(
  { secrets: [apiKey] },
  summarizeFlow
);
bash
firebase deploy --only functions

Express.js Server

Express.js服务器

typescript
import express from 'express';
import { expressHandler } from 'genkit/express';

const app = express();
app.use(express.json());

app.post('/summarize', expressHandler(summarizeFlow));
app.post('/chat', expressHandler(chatFlow));

app.listen(3000, () => console.log('Server running on port 3000'));
typescript
import express from 'express';
import { expressHandler } from 'genkit/express';

const app = express();
app.use(express.json());

app.post('/summarize', expressHandler(summarizeFlow));
app.post('/chat', expressHandler(chatFlow));

app.listen(3000, () => console.log('服务器运行在3000端口'));

Cloud Run

Cloud Run

bash
undefined
bash
undefined

Build and deploy

构建并部署

gcloud run deploy genkit-app
--source .
--region us-central1
--set-env-vars GEMINI_API_KEY=$GEMINI_API_KEY

---
gcloud run deploy genkit-app
--source .
--region us-central1
--set-env-vars GEMINI_API_KEY=$GEMINI_API_KEY

---

Supported Plugins

支持的插件

Model Providers

模型提供商

PluginPackageModels
Google AI
@genkit-ai/google-genai
Gemini 2.5 Flash/Pro
Vertex AI
@genkit-ai/vertexai
Gemini, Imagen, Claude
OpenAI
genkitx-openai
GPT-4o, o1, etc.
Anthropic
genkitx-anthropic
Claude 3.5/3
AWS Bedrock
genkitx-aws-bedrock
Claude, Titan, etc.
Ollama
genkitx-ollama
Local models
DeepSeek
genkitx-deepseek
DeepSeek-R1
xAI (Grok)
genkitx-xai
Grok models
插件包名模型
Google AI
@genkit-ai/google-genai
Gemini 2.5 Flash/Pro
Vertex AI
@genkit-ai/vertexai
Gemini、Imagen、Claude
OpenAI
genkitx-openai
GPT-4o、o1等
Anthropic
genkitx-anthropic
Claude 3.5/3
AWS Bedrock
genkitx-aws-bedrock
Claude、Titan等
Ollama
genkitx-ollama
本地模型
DeepSeek
genkitx-deepseek
DeepSeek-R1
xAI (Grok)
genkitx-xai
Grok模型

Vector Databases

向量数据库

PluginPackage
Dev Local (testing)
@genkit-ai/dev-local-vectorstore
Pinecone
genkitx-pinecone
pgvector
genkitx-pgvector
Chroma
genkitx-chroma
Cloud Firestore
@genkit-ai/firebase
LanceDB
genkitx-lancedb

插件包名
Dev Local(测试用)
@genkit-ai/dev-local-vectorstore
Pinecone
genkitx-pinecone
pgvector
genkitx-pgvector
Chroma
genkitx-chroma
Cloud Firestore
@genkit-ai/firebase
LanceDB
genkitx-lancedb

Best Practices

最佳实践

  1. Always define input/output schemas — Use Zod objects for Dev UI labeled fields and API safety
  2. Use flows for all AI logic — Even simple calls; flows give you tracing and deployment for free
  3. Store API keys in environment variables — Never hardcode; use Firebase Secrets for production
  4. Use
    ai.run()
    to trace custom steps
    — Wrap non-Genkit code in
    ai.run()
    for trace visibility
  5. Stream long-form content — Use
    defineFlow
    with
    streamSchema
    +
    sendChunk
    for better UX
  6. Separate concerns with agents — Specialized subflows > one monolithic flow
  7. Use Dotprompt for team prompts
    .prompt
    files enable versioning, review, and reuse
  1. 始终定义输入/输出模式 —— 使用Zod对象实现Dev UI标签化字段和API安全
  2. 所有AI逻辑都使用流程 —— 即使是简单调用;流程可免费提供追踪和部署能力
  3. 将API密钥存储在环境变量中 —— 绝对不要硬编码;生产环境使用Firebase Secrets
  4. 使用
    ai.run()
    追踪自定义步骤
    —— 将非Genkit代码包裹在
    ai.run()
    中以实现追踪可见性
  5. 流式传输长文本内容 —— 结合
    defineFlow
    streamSchema
    sendChunk
    提升用户体验
  6. 使用Agent分离关注点 —— 专业子流程优于单一大型流程
  7. 使用Dotprompt管理团队提示词 ——
    .prompt
    文件支持版本控制、审核和复用

Constraints

约束条件

Must Do

必须遵守

  • Define schemas for all flow inputs and outputs
  • Handle
    null
    output from
    generate()
    — throw meaningful errors
  • Set
    GENKIT_ENV=dev
    when running flows separately from the dev server
  • Use
    onCallGenkit
    (not raw Cloud Functions) when deploying to Firebase
  • 为所有流程输入和输出定义模式
  • 处理
    generate()
    返回的
    null
    输出 —— 抛出有意义的错误
  • 当独立于开发服务器运行流程时,设置
    GENKIT_ENV=dev
  • 部署到Firebase时使用
    onCallGenkit
    (而非原生Cloud Functions)

Must Not Do

禁止操作

  • Never hardcode API keys in source code
  • Do not use
    generate()
    outside a flow if you need tracing/observability
  • Do not call
    genkit start
    without a command — always pass
    -- <your-run-command>
  • Avoid blocking the event loop in tool handlers — use
    async/await

  • 永远不要在源代码中硬编码API密钥
  • 如果需要追踪/可观测性,不要在流程外使用
    generate()
  • 不要在不带命令的情况下调用
    genkit start
    —— 始终传递
    -- <你的运行命令>
  • 避免在工具处理程序中阻塞事件循环 —— 使用
    async/await

References

参考资料

Examples

示例

Example 1: Minimal Flow

示例1:最小化流程

typescript
import { googleAI } from '@genkit-ai/google-genai';
import { genkit, z } from 'genkit';

const ai = genkit({ plugins: [googleAI()] });

export const helloFlow = ai.defineFlow(
  {
    name: 'helloFlow',
    inputSchema: z.object({ name: z.string() }),
    outputSchema: z.string(),
  },
  async ({ name }) => {
    const { text } = await ai.generate(`Say hello to ${name} in a creative way.`);
    return text;
  }
);

// Run it
const greeting = await helloFlow({ name: 'World' });
console.log(greeting);
typescript
import { googleAI } from '@genkit-ai/google-genai';
import { genkit, z } from 'genkit';

const ai = genkit({ plugins: [googleAI()] });

export const helloFlow = ai.defineFlow(
  {
    name: 'helloFlow',
    inputSchema: z.object({ name: z.string() }),
    outputSchema: z.string(),
  },
  async ({ name }) => {
    const { text } = await ai.generate(`用创意的方式向${name}问好。`);
    return text;
  }
);

// 运行流程
const greeting = await helloFlow({ name: 'World' });
console.log(greeting);

Example 2: Full RAG + Agent Pipeline

示例2:完整RAG + Agent管道

typescript
import { googleAI, textEmbedding004 } from '@genkit-ai/google-genai';
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { genkit, z } from 'genkit';

const ai = genkit({
  plugins: [
    googleAI(),
    devLocalVectorstore([{ indexName: 'kb', embedder: textEmbedding004 }]),
  ],
});

// Index knowledge base documents
const indexKnowledgeBase = ai.defineFlow(
  { name: 'indexKB', inputSchema: z.array(z.string()) },
  async (texts) => {
    await ai.index({
      indexer: devLocalVectorstoreIndexer('kb'),
      docs: texts.map(text => ({ content: [{ text }] })),
    });
  }
);

// Answer questions using RAG
export const answerFlow = ai.defineFlow(
  {
    name: 'answerFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.object({ answer: z.string(), sources: z.number() }),
  },
  async ({ question }) => {
    const docs = await ai.retrieve({
      retriever: devLocalVectorstoreRetriever('kb'),
      query: question,
      options: { k: 5 },
    });

    const { text } = await ai.generate({
      system: 'Answer only from the provided context. If unsure, say so.',
      prompt: question,
      docs,
    });

    return { answer: text, sources: docs.length };
  }
);
typescript
import { googleAI, textEmbedding004 } from '@genkit-ai/google-genai';
import { devLocalVectorstore } from '@genkit-ai/dev-local-vectorstore';
import { genkit, z } from 'genkit';

const ai = genkit({
  plugins: [
    googleAI(),
    devLocalVectorstore([{ indexName: 'kb', embedder: textEmbedding004 }]),
  ],
});

// 索引知识库文档
const indexKnowledgeBase = ai.defineFlow(
  { name: 'indexKB', inputSchema: z.array(z.string()) },
  async (texts) => {
    await ai.index({
      indexer: devLocalVectorstoreIndexer('kb'),
      docs: texts.map(text => ({ content: [{ text }] })),
    });
  }
);

// 使用RAG回答问题
export const answerFlow = ai.defineFlow(
  {
    name: 'answerFlow',
    inputSchema: z.object({ question: z.string() }),
    outputSchema: z.object({ answer: z.string(), sources: z.number() }),
  },
  async ({ question }) => {
    const docs = await ai.retrieve({
      retriever: devLocalVectorstoreRetriever('kb'),
      query: question,
      options: { k: 5 },
    });

    const { text } = await ai.generate({
      system: '仅使用提供的上下文回答问题。如果不确定,请直接说明。',
      prompt: question,
      docs,
    });

    return { answer: text, sources: docs.length };
  }
);

Example 3: Multi-Model Comparison

示例3:多模型对比

typescript
import { googleAI } from '@genkit-ai/google-genai';
import { openAI } from 'genkitx-openai';
import { genkit, z } from 'genkit';

const ai = genkit({ plugins: [googleAI(), openAI()] });

export const compareModelsFlow = ai.defineFlow(
  {
    name: 'compareModelsFlow',
    inputSchema: z.object({ prompt: z.string() }),
    outputSchema: z.object({ gemini: z.string(), gpt4o: z.string() }),
  },
  async ({ prompt }) => {
    const [geminiResult, gptResult] = await Promise.all([
      ai.generate({ model: googleAI.model('gemini-2.5-flash'), prompt }),
      ai.generate({ model: 'openai/gpt-4o', prompt }),
    ]);

    return {
      gemini: geminiResult.text,
      gpt4o: gptResult.text,
    };
  }
);
typescript
import { googleAI } from '@genkit-ai/google-genai';
import { openAI } from 'genkitx-openai';
import { genkit, z } from 'genkit';

const ai = genkit({ plugins: [googleAI(), openAI()] });

export const compareModelsFlow = ai.defineFlow(
  {
    name: 'compareModelsFlow',
    inputSchema: z.object({ prompt: z.string() }),
    outputSchema: z.object({ gemini: z.string(), gpt4o: z.string() }),
  },
  async ({ prompt }) => {
    const [geminiResult, gptResult] = await Promise.all([
      ai.generate({ model: googleAI.model('gemini-2.5-flash'), prompt }),
      ai.generate({ model: 'openai/gpt-4o', prompt }),
    ]);

    return {
      gemini: geminiResult.text,
      gpt4o: gptResult.text,
    };
  }
);