Firebase Genkit

Genkit 是一个框架,旨在帮助您构建依托 AI 技术的应用和功能。它提供适用于 Node.js 和 Go 的开源库,以及用于测试和调试的开发者工具。

本文档介绍了适用于 Node.js 的 Genkit。如果您是 Go 开发者,请参阅 Genkit Go 文档

您可以在支持 Node.js 的任何地方部署和运行 Genkit 库。它可与任何生成式 AI 模型 API 或矢量数据库搭配使用。虽然我们提供适用于 Firebase 和 Google Cloud 的集成,但您可以独立于任何 Google 服务来使用 Genkit。

开始

主要功能

用于 AI 生成的统一 API 只需使用一个 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 world's 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 提供了一个命令行界面 (CLI) 和本地开发者界面,以便您更轻松地构建 AI 应用。这些工具可帮助您:

  • 实验:测试并优化您的 AI 函数、提示和查询。
  • 调试:根据详细的执行跟踪记录查找并修复问题。
  • 评估:评估多个测试用例的生成结果。

与我们联系

  • 加入社区:在我们的 Discord 服务器上随时掌握最新动态、提出问题并分享您的作品。
  • 提供反馈:使用我们的 GitHub 问题跟踪器报告问题或提出新功能建议。

后续步骤

请参阅我们的入门指南,了解如何使用 Genkit 构建您的首个 AI 应用。