Firebase Genkit

Genkit הוא מסגרת שמיועדת לעזור לכם ליצור אפליקציות ותכונות מבוססות-AI. הוא מספק ספריות בקוד פתוח ל-Node.js ול-Go, וגם כלים למפתחים לצורך בדיקה וניפוי באגים.

במסמך הזה מוסבר על Genkit ל-Node.js. אם אתם מפתחים ב-Go, כדאי לעיין במסמכי התיעוד של Genkit Go.

אפשר לפרוס ולהריץ ספריות Genkit בכל מקום שבו יש תמיכה ב-Node.js. הוא תוכנן לפעול עם הרבה ספקי מודלים של AI ומסדי נתונים של וקטורים. אנחנו מציעים שילובים ל-Firebase ול-Google Cloud, אבל אפשר להשתמש ב-Genkit ללא קשר לשירותי Google.

שנתחיל?

יכולות מפתחות

Unified API ליצירת AI שימוש ב-API אחד ליצירה או לשידור של תוכן ממגוון מודלים של AI. פועל עם קלט/פלט במגוון מודלים והגדרות מותאמות אישית של מודלים.
פלט מובנה יצירת אובייקטים מובְנים (כמו JSON) או שידור שלהם עם אימות מובנה. פשוטת את השילוב עם האפליקציה וממירה נתונים לא מובְנים לפורמט שאפשר להשתמש בו.
קריאה לכלי מאפשרים למודלים של AI להפעיל את הפונקציות וממשקי ה-API שלכם ככלים להשלמתם של משימות. המודל מחליט מתי להשתמש בכלים ובאילו כלים להשתמש.
Chat Genkit מציע ממשק API ספציפי לצ'אט שמאפשר לנהל שיחות עם מודלים של AI, שיכולים להיות עם מצב ועם שמירה.
סוכנים ליצור סוכני AI שמשתמשים בכלים (כולל סוכני AI אחרים) כדי לבצע אוטומציה של משימות מורכבות ותהליכי עבודה.
אחזור נתונים שילוב הנתונים שלכם יעזור לכם לשפר את הדיוק והרלוונטיות של הפלט שנוצר. ממשקי API פשוטים עוזרים להטמיע מידע ממקורות שונים, להוסיף אותו לאינדקס ולאחזר אותו.
יצירת תבניות להנחיות יצירת הנחיות יעילות שכוללות תבניות טקסט עשיר, הגדרות מודל, תמיכה במגוון מודלים ושילוב כלים – והכול בקובץ הנחיה קומפקטי שניתן להריץ.

כדי לקבל מושג קונקרטי לגבי השימוש ביכולות האלה בקוד, אפשר לעיין בדוגמאות הקוד הבאות:

יצירת קוד בסיסית

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

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

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

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

פלט מובנה

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

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

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);

קריאה לכלי

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

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

// 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.object({ 
      weatherReport: z.string().describe('Weather report of a particular location') 
    }),
  },
  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);

צ'אט

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

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

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

נציגים

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

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

// Define tools for your agents to use
const reservationTool = ai.defineTool( ... );
const reservationCancelationTool = ai.defineTool( ... );
const reservationListTool = ai.defineTool( ... );

// Define prompts that represent specialist agents
const reservationAgent = ai.definePrompt({
  name: 'reservationAgent',
  description: 'Reservation Agent can help manage guest reservations',
  tools: [reservationTool, reservationCancelationTool, reservationListTool],
  system: `Help guests make and manage reservations`
});
const menuInfoAgent = ai.definePrompt( ... );
const complaintAgent = ai.definePrompt( ... );

// Define a triage agent that routes to the proper specialist agent
const triageAgent = ai.definePrompt({
  name: 'triageAgent',
  description: 'Triage Agent',
  tools: [reservationAgent, menuInfoAgent, complaintAgent],
  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 conversational agent interactions
const chat = ai.chat(triageAgent);

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

אחזור נתונים

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

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

// 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
});

תבנית הנחיה

---
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) וממשק משתמש מקומי למפתחים, כדי להקל על פיתוח אפליקציות AI. הכלים האלה עוזרים לכם:

  • ניסוי: בדיקה ושיפור של הפונקציות, ההנחיות והשאילתות של ה-AI.
  • ניפוי באגים: איתור בעיות ותיקון שלהן באמצעות מעקב מפורט אחר ביצועים.
  • בדיקה: הערכת התוצאות שנוצרו במספר תרחישי בדיקה.

ליצור איתנו קשר

  • הצטרפות לקהילה: אתם יכולים להצטרף לשרת Discord שלנו כדי לקבל עדכונים, לשאול שאלות ולשתף את העבודות שלכם.
  • שליחת משוב: אתם יכולים לדווח על בעיות או להציע תכונות חדשות באמצעות כלי המעקב אחרי בעיות שלנו ב-GitHub.

השלבים הבאים

במדריך למתחילים מוסבר איך ליצור את אפליקציית ה-AI הראשונה שלכם באמצעות Genkit.