Firebase Genkit

Genkit is a framework designed to help you build AI-powered applications and features. It provides open source libraries for Node.js and Go, plus developer tools for testing and debugging.

This documentation covers Genkit for Node.js. If you're a Go developer, see the Genkit Go documentation.

You can deploy and run Genkit libraries anywhere Node.js is supported. It's designed to work with any generative AI model API or vector database. While we offer integrations for Firebase and Google Cloud, you can use Genkit independently of any Google services.

Get started

Key capabilities

Unified API for AI generation Use one API to generate or stream content from various AI models. Works with multimodal input/output and custom model settings.
Structured output Generate or stream structured objects (like JSON) with built-in validation. Simplify integration with your app and convert unstructured data into a usable format.
Tool calling Let AI models call your functions and APIs as tools to complete tasks. The model decides when and which tools to use.
Chat Genkit offers a chat-specific API that facilitates multi-turn conversations with AI models, which can be stateful and persistent.
Agents Create intelligent agents that use tools (including other agents) to help automate complex tasks and workflows.
Data retrieval Improve the accuracy and relevance of generated output by integrating your data. Simple APIs help you embed, index, and retrieve information from various sources.
Prompt templating Create effective prompts that include rich text templating, model settings, multimodal support, and tool integration - all within a compact, runnable prompt file.

See the following code samples for a concrete idea of how to use these capabilities in code:

Basic generation

import { genkit } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';

const ai = genkit({
  plugins: [googleAI()],
  model: gemini15Flash,  // Set default model
});

// Simple generation
const { text } = await ai.generate('Why is AI awesome?');
console.log(text);

// Streamed generation 
const { stream } = await ai.generateStream('Tell me a story');
for await (const chunk of stream) {
  console.log(chunk.text);
}

Structured output

import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';

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

const { output } = await ai.generate({
  prompt: 'Create a brief profile for a character in a fantasy video game.',
  // Specify output structure using Zod schema
  output: {
    format: 'json',  
    schema: z.object({
      name: z.string(),
      role: z.enum(['knight', 'mage', 'archer']),
      backstory: z.string(),
    }),
  },
});

console.log(output);

Function calling

import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';

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

// Define tool to get current weather for a given location
const getWeather = ai.defineTool(
  {
    name: "getWeather",
    description: "Gets the current weather in a given location",
    inputSchema: z.object({ 
      location: z.string().describe('The location to get the current weather for')
    }),
    outputSchema: z.string(),
  },
  async (input) => {
    // Here, we would typically make an API call or database query. For this
    // example, we just return a fixed value.
    return `The current weather in ${input.location} is 63��F and sunny.`;
  }
);

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

console.log(text);

Chat

import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';

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

const chat = ai.chat({ system: 'Talk like a pirate' });

let response = await chat.send('Hi, my name is Pavel');

response = await chat.send('What is my name?');
console.log(response.text);
// Ahoy there! Your name is Pavel, you scurvy dog

Agents

import { genkit, z } from 'genkit';
import { googleAI, gemini15Flash } from '@genkit-ai/googleai';

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

// Define prompts that represent specialist agents
const reservationAgent = ai.definePrompt(
  {
    name: 'reservationAgent',
    description: 'Reservation Agent can help manage guest reservations',
    tools: [reservationTool, reservationCancelationTool, reservationListTool],

  },
  `{{role "system"}} Help guests make and manage reservations`
);

const menuInfoAgent = ...
const complaintAgent = ...

// Define a triage agent that routes to the proper specialist agent
const triageAgent = ai.definePrompt(
  {
    name: 'triageAgent',
    description: 'Triage Agent',
    tools: [reservationAgent, menuInfoAgent, complaintAgent],
  },
  `{{role "system"}} You are an AI customer service agent for Pavel's Cafe.
  Greet the user and ask them how you can help. If appropriate, transfer to an
  agent that can better handle the request. If you cannot help the customer with
  the available tools, politely explain so.`
);

// Create a chat to enable multi-turn agent interactions
const chat = ai.chat(triageAgent);

chat.send('I want a reservation at Pavel\'s Cafe for noon on Tuesday.' );

Data retrieval

import { genkit } from 'genkit';
import { googleAI, gemini15Flash, textEmbedding004 } from '@genkit-ai/googleai';
import { devLocalRetrieverRef } from '@genkit-ai/dev-local-vectorstore';

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

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

// Consistent API to retrieve most relevant documents based on semantic similarity to query
const docs = await ai.retrieve(
  retriever: retriever,
  query: 'How old is bob?',
);

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

Prompt template

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

Development tools

Genkit provides a command-line interface (CLI) and a local Developer UI to make building AI applications easier. These tools help you:

  • Experiment: Test and refine your AI functions, prompts, and queries.
  • Debug: Find and fix issues with detailed execution traces.
  • Evaluate: Assess generated results across multiple test cases.

Connect with us

  • Join the community: Stay updated, ask questions, and share your work on our Discord server.
  • Provide feedback: Report issues or suggest new features using our GitHub issue tracker.

Next steps

Learn how to build your first AI application with Genkit in our Get started guide.