Firebase Genkit
Genkit ist ein Framework, mit dem Sie KI-basierte Anwendungen und Funktionen erstellen können. Es bietet Open-Source-Bibliotheken für Node.js und Go sowie Entwicklertools zum Testen und Debuggen.
Diese Dokumentation bezieht sich auf Genkit für Node.js. Wenn Sie Go-Entwickler sind, lesen Sie die Genkit Go-Dokumentation.
Sie können Genkit-Bibliotheken überall dort bereitstellen und ausführen, wo Node.js unterstützt wird. Sie ist für die Verwendung mit vielen Anbietern von KI-Modellen und Vektordatenbanken konzipiert. Wir bieten zwar Integrationen für Firebase und Google Cloud an, Sie können Genkit aber unabhängig von Google-Diensten verwenden.
Hauptmerkmale
Unified API für die KI-Generierung | Mit einer API Inhalte aus verschiedenen KI-Modellen generieren oder streamen Funktioniert mit multimodaler Eingabe/Ausgabe und benutzerdefinierten Modelleinstellungen. |
Strukturierte Ausgabe | Strukturierte Objekte (z. B. JSON) mit integrierter Validierung generieren oder streamen Sie können die Integration in Ihre App vereinfachen und unstrukturierte Daten in ein verwendbares Format umwandeln. |
Toolaufrufe | KI-Modelle können Ihre Funktionen und APIs als Tools aufrufen, um Aufgaben auszuführen. Das Modell entscheidet, wann und welche Tools verwendet werden sollen. |
Google Chat | Genkit bietet eine chatspezifische API, die Unterhaltungen mit mehreren Runden mit KI-Modellen ermöglicht, die zustands- und persistent sein können. |
Agents | Erstellen Sie intelligente Bots, die Tools (einschließlich anderer Bots) verwenden, um komplexe Aufgaben und Workflows zu automatisieren. |
Datenabruf | Durch die Einbindung Ihrer Daten lässt sich die Genauigkeit und Relevanz der generierten Ausgabe verbessern. Mithilfe einfacher APIs können Sie Informationen aus verschiedenen Quellen einbetten, indexieren und abrufen. |
Prompt-Vorlagen | Erstellen Sie effektive Prompts mit Rich-Text-Vorlagen, Modelleinstellungen, multimodaler Unterstützung und Tool-Integration – alles in einer kompakten, ausführbaren Promptdatei. |
In den folgenden Codebeispielen wird veranschaulicht, wie Sie diese Funktionen in Code verwenden:
Einfache Generierung
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);
}
Strukturierte Ausgabe
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);
Toolaufrufe
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);
Chat
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
Agents
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.');
Datenabruf
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
});
Prompt-Vorlage
---
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}}.
Entwicklungstools
Genkit bietet eine Befehlszeile und eine lokale Entwickler-UI, um das Erstellen von KI-Anwendungen zu vereinfachen. Mit diesen Tools können Sie Folgendes tun:
- Experimentieren:Testen und optimieren Sie Ihre KI-Funktionen, Prompts und Abfragen.
- Fehler beheben:Mit detaillierten Ausführungsabfolgen können Sie Probleme finden und beheben.
- Auswerten:Die generierten Ergebnisse werden in mehreren Testfällen bewertet.
Kontakt
- Treten Sie der Community bei:Bleiben Sie auf dem Laufenden, stellen Sie Fragen und teilen Sie Ihre Arbeit auf unserem Discord-Server.
- Feedback geben:Über unseren GitHub-Issue-Tracker können Sie Probleme melden oder neue Funktionen vorschlagen.
Nächste Schritte
In unserem Leitfaden für den Einstieg erfahren Sie, wie Sie Ihre erste KI-Anwendung mit Genkit erstellen.