Firebase Genkit

Genkit เป็นเฟรมเวิร์กที่ออกแบบมาเพื่อช่วยคุณสร้างแอปพลิเคชันและฟีเจอร์ที่ทำงานด้วยระบบ AI โดยมีไลบรารีโอเพนซอร์สสำหรับ Node.js และ Go รวมถึงเครื่องมือสำหรับนักพัฒนาแอปสำหรับการทดสอบและแก้ไขข้อบกพร่อง

เอกสารนี้ครอบคลุมถึง Genkit สำหรับ Node.js หากคุณเป็นนักพัฒนาซอฟต์แวร์ Go โปรดดูเอกสารประกอบเกี่ยวกับ Genkit Go

คุณสามารถติดตั้งใช้งานและเรียกใช้ไลบรารี Genkit ได้ทุกที่ที่รองรับ Node.js โดยได้รับการออกแบบมาให้ทำงานร่วมกับ Generative AI Model API หรือฐานข้อมูลเวกเตอร์ทั้งหมด แม้ว่าเราจะมีการผสานรวมสำหรับ Firebase และ Google Cloud แต่คุณก็ใช้ Genkit แยกจากบริการของ Google ได้

เริ่มใช้งาน

ความสามารถที่สำคัญ

API แบบรวมสำหรับการสร้าง AI ใช้ API เดียวเพื่อสร้างหรือสตรีมเนื้อหาจากโมเดล AI ต่างๆ ใช้งานได้กับการตั้งค่าอินพุต/เอาต์พุตแบบหลายโมดัลและโมเดลที่กำหนดเอง
การสร้างที่มีโครงสร้าง สร้างหรือสตรีมออ��เจ็กต์ที่มีโครงสร้าง (เช่น JSON) ที่มีการตรวจสอบในตัว ทำให้การผสานรวมกับแอปง่ายขึ้นและแปลงข้อมูลที่ไม่มีโครงสร้างให้เป็นรูปแบบที่ใช้งานได้
การเรียกใช้เครื่องมือ ให้โมเดล AI เรียกใช้ฟังก์ชันและ API ของคุณเป็นเครื่องมือทำงานให้เสร็จสมบูรณ์ โมเดลจะตัดสินใจว่าจะใช้เครื่องมือใดและเมื่อใด
การสร้างแบบเสริมการดึงข้อมูล ปรับปรุงความแม่นยำและความเกี่ยวข้องของผลลัพธ์ที่สร้างขึ้นด้วยการผสานรวมข้อมูลของคุณ API แบบง่ายช่วยให้คุณฝัง จัดทำดัชนี และเรียกข้อมูลจากแหล่งที่มาต่างๆ ได้
เทมเพลตพรอมต์ สร้างพรอมต์ที่มีประสิทธิภาพซึ่งรวมเทมเพลต Rich Text การกำหนดเทมเพลต การตั้งค่าโมเดล การสนับสนุนแบบหลายโมดัล และการผสานรวมเครื่องมือ ทั้งหมดนี้อยู่ในไฟล์พรอมต์ขนาดกะทัดรัดที่เรียกใช้ได้

ดูตัวอย่างโค้ดต่อไปนี้เพื่อหาแนวคิดที่เป็นรูปธรรมเกี่ยวกับวิธีใช้ความสามารถเหล่านี้ในโค้ด

รุ่นพื้นฐาน

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 มีอินเทอร์เฟซบรรทัดคำสั่ง (CLI) และ UI สำหรับนักพัฒนาซอฟต์แวร์ภายในเพื่อช่วยให��สร้างแอปพลิเคชัน AI ได้ง่ายขึ้น เครื่องมือเหล่านี้ช่วยให้คุณทำสิ่งต่อไปนี้ได้

  • การทดสอบ: ทดสอบและปรับแต่งฟังก์ชัน พรอมต์ และการค้นหาของ AI
  • แก้ไขข้อบกพร่อง: ค้นหาและแก้ไขปัญหาเกี่ยวกับการติดตามการดำเนินการโดยละเอียด
  • ประเมิน: ประเมินผลลัพธ์ที่สร้างขึ้นจากกรอบการทดสอบหลายกรณี

เชื่อมต่อกับเรา

ขั้นตอนถัดไป

ดูวิธีสร้างแอปพลิเคชัน AI แรกด้วย Genkit ในคู่มือเริ่มต้นใช้งาน