Firebase Genkit 是一種開放原始碼架構,可協助您建構、部署及監控可用於正式環境的 AI 技術輔助應用程式。
Genkit 專為應用程式開發人員打造,可協助您以熟悉的模式和模式,輕鬆將強大的 AI 功能整合至應用程式。這個平台是由 Firebase 背後的同一個團隊打造,運用我們在全球數百萬名開發人員打造的工具中汲取經驗。
透過 Genkit 建立可產生自訂內容的應用程式、使用語意搜尋、處理非結構化輸入內容、使用業務資料回答問題、自動進行決策、自動化調度管理工具呼叫,以及執行其他更多作業!
Genkit 目前支援以 JavaScript/TypeScript (Node.js) 進行伺服器端開發,且在積極開發期間支援 Go。
前往其 GitHub 存放區,追蹤應用程式的開發程序,或自行做出貢獻。
主要功能
Genkit 會逐步協助您完成 AI 開發旅程的每一個步驟,從原型設計到正式環境監控,現在有許多值得討論的主題。
為幫助你踏出第一步,以下我們推薦你可能會喜歡的 10 項 Genkit 主要功能:
1. 眾多模型和單一介面
Genkit 提供外掛程式,可讓你立即存取熱門模型,並透過靈活彈性的模型抽象化,輕鬆整合任何模型 API,並使用社群維護的模型。試用新模型就像變更單一引數一樣簡單,但每個模型皆可指定自訂設定。
import { geminiPro } from '@genkit-ai/vertexai';
import { ollama } from 'genkitx-ollama';
import { generate } from '@genkit-ai/ai';
function flipACoin(a, b) {
return Math.random() > 0.5 ? a : b;
}
const result = await generate({
model: flipACoin(geminiPro, 'ollama/gemma'),
config: { temperature: 0.3, maxOutputTokens: 200 },
prompt: 'What makes you the best LLM out there?',
});
console.log(result.text());
2. 結構化輸出內容
使用 Zod 結構定義,透過 Genkit 產生強類型資料。這可協助您分析非結構化文字、產生創意內容、選取工作,並以結構化的類型安全物件將結果傳回應用程式。
import { generate } from "@genkit-ai/ai";
import { geminiPro } from "@genkit-ai/vertexai";
import { z } from "zod";
const CreatureSchema = z.object({
name: z.string().describe('the name of the creature'),
hitPoints: z.number().describe('hit points, between 5 and 100'),
attacks: z.array(z.object({
name: z.string(),
damage: z.number().describe('amount of damage, between 2 and 25'),
})).describe('3 attacks the creature can use')
});
const createCreature = defineFlow({
name: "createCreature",
inputSchema: z.string(),
outputSchema: CreatureSchema,
},
(habitat) => {
const result = await generate({
model: geminiPro,
prompt: `You are a brilliant RPG designer. Generate a creature that lives in ${habitat}.`,
output: {schema: CreatureSchema}
});
// strongly typed and ready to go
return result.output();
}
)
console.log(await createCreature("a developer conference"));
3. 多模態、多媒體
Genkit 為支援文字、資料和任意媒體的內容提供通用格式,這樣一來,您就能將 Genkit 用於執行任何生成式工作 (例如生成圖像),而不只是 LLM。
import { imagen2, geminiProVision } from '@genkit-ai/vertexai';
import { generate } from '@genkit-ai/ai';
const imageResult = await generate({
model: imagen2,
prompt: 'Generate an image of a very specific historical time and place.',
});
const generatedImage = imageResult.media();
const descriptionResult = await generate({
model: geminiProVision,
prompt: [
{
text: 'What is the historical time and place represented in this picture?',
},
{ media: generatedImage },
],
});
console.log(descriptionResult.text());
4. 提供 LLM 工具
Genkit 提供各種工具,讓你可以透過 LLM 輕鬆執行函式呼叫。有了這些工具,AI 就能擷取資料、顯示 UI、寫入資料庫,或執行任何可編寫程式碼的動作。
import { generate, defineTool } from '@genkit-ai/ai';
import { geminiPro } from '@genkit-ai/vertexai';
import { z } from 'zod';
const createReminder = defineTool(
{
name: 'createReminder',
description: 'Use this to create reminders for things in the future',
inputSchema: z.object({
time: z
.string()
.describe('ISO timestamp string, e.g. 2024-04-03T12:23:00Z'),
reminder: z.string().describe('the content of the reminder'),
}),
outputSchema: z.number().describe('the ID of the created reminder'),
},
(reminder) => db.reminders.create(reminder)
);
const searchNotes = defineTool(
{
name: 'searchNotes',
description: "Use this to search the user's notes for people or phrases",
inputSchema: z.string().describe('the search query'),
outputSchema: z.object({ notes: z.array(NoteSchema) }),
},
(query) => db.notes.search(query)
);
const result = await generate({
model: geminiPro,
tools: [createReminder, searchNotes],
prompt: `
You are a note-taking assistant. Using the tools available, try to answer the provided query.
If you create a reminder, describe in text the reminder you created as a response.
Query: I took a note about a meeting with Anna - can you set a reminder for the time?
`,
});
console.log(result.text());
5. 使用 Dotprompt 管理提示
提示工程不只是調整文字而已。您使用的模型、您提供的參數,以及您要求的格式,都會影響輸出的品質。Genkit 提供 Dotprompt 這個提示檔案格式,可將所有內容整合到單一檔案,方便進行測試和整理。
---
model: vertexai/gemini-1.0-pro
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}}.
6. 在本機執行資料流
生成式 AI 的結果有很多種,因此實驗非常重要。本機 Genkit 開發人員 UI 可讓您與模型和擷取工具等重要 AI 元件互動,也可以手動測試端對端流程 (包括您編寫的所有自訂程式碼)。
7. 檢查追蹤記錄
由於隨機性和隱藏的程序,使用 AI 對複雜的多步驟工作流程進行偵錯可能會是一項挑戰。Genkit 提供開發人員 UI 的追蹤檢查器,方便您檢查每個模型呼叫的追蹤記錄,並在流程中逐步執行對應作業。它可以查看實際工作環境的追蹤記錄,甚至算繪圖片!
8. 開放與可擴展
AI 生態系統的成長速度比任何團隊能跟上的速度還要快。Genkit 有開放的外掛程式模型,可提供針對新模型、擷取器等預先建構的整合項目。雖然 Genkit 團隊維護了一小部分的官方外掛程式,但任何人都可將自己的 Genkit 外掛程式發布至 NPM。
找不到所需的特定整合外掛程式嗎?別擔心!Genkit 的抽象化機制具有彈性,可讓您輕鬆建構整合到架構的自訂元件,例如這個自訂 Firestore 擷取器:
import { embed } from '@genkit-ai/ai/embedder';
import { Document, defineRetriever } from '@genkit-ai/ai/retriever';
import { textEmbeddingGecko } from '@genkit-ai/vertexai';
import {
FieldValue,
VectorQuery,
VectorQuerySnapshot,
} from '@google-cloud/firestore';
import { Firestore } from 'firebase-admin/firestore';
import * as z from 'zod';
import { augmentedPrompt } from './prompt';
const QueryOptions = z.object({
k: z.number().optional(),
});
const firestoreArtifactsRetriever = defineRetriever(
{
name: 'firestore/artifacts',
configSchema: QueryOptions,
},
async (input, options) => {
const embedding = await embed({
embedder: textEmbeddingGecko,
content: input,
});
const db = new Firestore();
const coll = db.collection('vectors' /* your collection name */);
const vectorQuery: VectorQuery = coll.findNearest(
'embedding' /* the name of the field that contains the vector */,
FieldValue.vector(embedding),
{
limit: options.k ?? 3,
distanceMeasure: 'COSINE',
}
);
const vectorQuerySnapshot: VectorQuerySnapshot = await vectorQuery.get();
return {
documents: vectorQuerySnapshot.docs.map((doc) =>
// doc.data() represents the Firestore document. You may process
// it as needed to generate a Genkit document object, depending on your
// storage format.
Document.fromText(doc.data().content.text)
),
};
}
);
9. 專為實際工作環境打造
輕鬆將流程部署到任何可提供 Express.js 應用程式的平台。Genkit 已完全以 OpenTelemetry 完成檢測,並且使用自訂中繼資料,提供企業級的生產監控功能。
您也可以使用 Google Cloud 和 Firebase 的官方外掛程式,將資料匯出至 Google Cloud 作業套件,並與 Firebase 服務整合,例如 Cloud Functions for Firebase、Firebase 驗證、App Check 和 Firestore。
10. 授權與安全性處理
建構公開的應用程式時,請務必保護儲存在系統中的資料。處理 LLM 時,必須進行額外檢查,確保模型只存取應有的資料,工具呼叫的範圍則適當,限定為叫用 LLM 的使用者,而且只有經過驗證的用戶端應用程式叫用這個流程。
Genkit 提供用於管理授權政策和背景資訊的機制。
import { defineFlow, runFlow } from '@genkit-ai/flow';
export const selfSummaryFlow = defineFlow(
{
name: 'selfSummaryFlow',
inputSchema: z.object({uid: z.string()}),
outputSchema: z.string(),
authPolicy: (auth, input) => {
if (!auth) {
throw new Error('Authorization required.');
}
if (input.uid !== auth.uid) {
throw new Error('You may only summarize your own profile data.');
}
}
},
async (input) => { ... });
整合項目
Genkit 會透過外掛程式系統,與 AI 模型、向量資料庫、遙測平台等項目整合。以下是 Genkit 團隊維護的外掛程式:
官方外掛程式 | |
---|---|
googleai |
生成式模型:Gemini Pro、Gemini 1.5 Pro、Gemini Pro Vision 嵌入模型:Gecko 文字嵌入 |
vertexai |
生成式模型:Gemini Pro、Gemini Pro Vision、Gemini 1.5 Flash、Gemini 1.5 Pro、Imagen2、Anthropic Claude 3 嵌入模型:Gecko 文字嵌入 Evaluators:Vertex AI 評估 |
ollama |
生成式模型:許多本機模型,包括 Gemma、Llama 3、Mistral 等 |
chroma |
向量資料庫:ChromaDB |
pinecone |
向量資料庫:Pinecone |
google-cloud |
監控工具:Google Cloud Trace、Google Cloud Logging |
firebase |
Cloud 部署項目:Cloud Functions、Firebase 驗證、App Check 向量資料庫:Cloud Firestore 向量儲存庫 |
langchain |
在 Genkit 流程中使用 LangChain 鏈結和公用程式 |
開始探索
閱讀入門指南,瞭解如何安裝 Genkit 及執行第一個 AI 流程。