ปลั๊กอิน Firebase มีการผสานรวมกับบริการ Firebase เพื่อให้คุณสร้างแอปพลิเคชัน AI ที่ชาญฉลาดและปรับขนาดได้ ฟีเจอร์หลักๆ มีดังนี้
- ที่เก็บเวกเตอร์ของ Firestore: ใช้ Firestore ในการจัดทําดัชนีและการดึงข้อมูลด้วยเวกเตอร์ที่ฝัง
- การวัดประสิทธิภาพ: ส่งออกการวัดประสิทธิภาพไปยังชุดเครื่องมือการดำเนินการของ Google Cloud ที่ขับเคลื่อนคอนโซลการตรวจสอบ Firebase Genkit
การติดตั้ง
ติดตั้งปลั๊กอิน Firebase ด้วย npm โดยทำดังนี้
npm install @genkit-ai/firebase
ข้อกำหนดเบื้องต้น
การตั้งค่าโปรเจ็กต์ Firebase
- ผลิตภัณฑ์ Firebase ทั้งหมดต้องใช้โปรเจ็กต์ Firebase คุณสามารถสร้างโปรเจ็กต์ใหม่หรือเปิดใช้ Firebase ในโปรเจ็กต์ Google Cloud ที่มีอยู่ได้โดยใช้คอนโซล Firebase
- หากใช้เวิร์กโฟลว์กับ Cloud Functions ให้อัปเกรดโปรเจ็กต์ Firebase เป็นแพ็กเกจ Blaze
- หากต้องการเรียกใช้โค้ดในเครื่องซึ่งส่งออกข้อมูลการวัดผล คุณต้องติดตั้งเครื่องมือ Google Cloud CLI
เริ่มต้น Firebase Admin SDK
คุณต้องเริ่มต้น Firebase Admin SDK ในแอปพลิเคชัน โดยไม่ได้รับการจัดการโดยอัตโนมัติจากปลั๊กอิน
import { initializeApp } from 'firebase-admin/app';
initializeApp({
projectId: 'your-project-id',
});
ปลั๊กอินกำหนดให้คุณระบุรหัสโปรเจ็กต์ Firebase คุณสามารถระบุรหัสโปรเจ็กต์ Firebase ได้ด้วยวิธีใดวิธีหนึ่งต่อไปนี้
ตั้งค่า
projectId
ในออบเจ็กต์การกําหนดค่าinitializeApp()
ตามที่แสดงในข้อมูลโค้ดด้านบนตั้งค่าตัวแปรสภาพแวดล้อม
GCLOUD_PROJECT
หากคุณเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud Functions, Cloud Run และอื่นๆ) ระบบจะตั้งค่าGCLOUD_PROJECT
เป็นรหัสโปรเจ็กต์ของสภาพแวดล้อมโดยอัตโนมัติหากตั้งค่า
GCLOUD_PROJECT
คุณจะละเว้นพารามิเตอร์การกําหนดค่าในinitializeApp()
ได้
ข้อมูลเข้าสู่ระบบ
หากต้องการระบุข้อมูลเข้าสู่ระบบ Firebase คุณจะต้องตั้งค่าข้อมูลเข้าสู่ระบบเริ่มต้นของแอปพลิเคชัน Google Cloud ด้วย วิธีระบุข้อมูลเข้าสู่ระบบ
หากคุณเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud Functions, Cloud Run และอื่นๆ) ระบบจะตั้งค่านี้โดยอัตโนมัติ
สำหรับสภาพแวดล้อมอื่นๆ ให้ทำดังนี้
- สร้างข้อมูลเข้าสู่ระบบของบัญชีบริการสําหรับโปรเจ็กต์ Firebase และดาวน์โหลดไฟล์คีย์ JSON ซึ่งทำได้ในหน้าบัญชีบริการของคอนโซล Firebase
- ตั้งค่าตัวแปรสภาพแวดล้อม
GOOGLE_APPLICATION_CREDENTIALS
เป็นเส้นทางไฟล์ของไฟล์ JSON ที่มีคีย์บัญชีบริการ หรือจะตั้งค่าตัวแปรสภาพแวดล้อมGCLOUD_SERVICE_ACCOUNT_CREDS
เป็นเนื้อหาของไฟล์ JSON ก็ได้
ฟีเจอร์และการใช้งาน
การส่งข้อมูลทางไกล
การตรวจสอบ Genkit ของ Firebase ทำงานด้วยชุดเครื่องมือการดำเนินการของ Google Cloud ซึ่งจะต้องเปิดใช้ API ที่เกี่ยวข้องกับการวัดผลสำหรับโปรเจ็กต์ของคุณ โปรดดูรายละเอียดเพิ่มเติมในเอกสารประกอบปลั๊กอิน Google Cloud
มอบบทบาทต่อไปนี้ให้กับ"บัญชีบริการประมวลผลเริ่มต้น" ในคอนโซล IAM ของ Google Cloud
- ผู้เขียนเมตริกการตรวจสอบ (roles/monitoring.metricWriter)
- Agent ของ Cloud Trace (roles/cloudtrace.agent)
- ผู้เขียนบันทึก (roles/logging.logWriter)
วิธีเปิดใช้การเรียกใช้การส่งออกข้อมูลการวัดและส่งข้อมูลทางไกล enableFirebaseTelemetry()
import { enableFirebaseTelemetry } from '@genkit-ai/firebase';
enableFirebaseTelemetry({
forceDevExport: false, // Set this to true to export telemetry for local runs
});
ปลั๊กอินนี้จะแชร์ตัวเลือกการกําหนดค่ากับปลั๊กอิน Google Cloud
การค้นหาเวกเตอร์ใน Cloud Firestore
คุณสามารถใช้ Cloud Firestore เป็นพื้นที่เก็บข้อมูลเวกเตอร์สําหรับการจัดทําดัชนีและการดึงข้อมูล RAG
ส่วนนี้ประกอบด้วยข้อมูลเฉพาะเกี่ยวกับปลั๊กอิน firebase
และฟีเจอร์การค้นหาเวกเตอร์ของ Cloud Firestore ดูรายละเอียดเพิ่มเติมเกี่ยวกับการใช้ RAG โดยใช้ Genkit ได้ในหน้าการสร้างที่เพิ่มการดึงข้อมูล
การใช้ GCLOUD_SERVICE_ACCOUNT_CREDS
และ Firestore
หากคุณใช้ข้อมูลเข้าสู่ระบบของบัญชีบริการโดยการส่งข้อมูลเข้าสู่ระบบโดยตรงผ่าน GCLOUD_SERVICE_ACCOUNT_CREDS
และกำลังใช้ Firestore เป็นเวกเตอร์สตอเรจด้วย คุณจะต้องส่งข้อมูลเข้าสู่ระบบไปยังอินสแตนซ์ Firestore โดยตรงในระหว่างการเริ่มต้น มิเช่นนั้นระบบอาจเริ่มต้นใช้งาน Singleton ด้วยข้อมูลเข้าสู่ระบบเริ่มต้นของแอปพลิเคชัน ทั้งนี้ขึ้นอยู่กับลําดับการเริ่มต้นใช้งานของปลั๊กอิน
import {initializeApp} from "firebase-admin/app";
import {getFirestore} from "firebase-admin/firestore";
const app = initializeApp();
let firestore = getFirestore(app);
if (process.env.GCLOUD_SERVICE_ACCOUNT_CREDS) {
const serviceAccountCreds = JSON.parse(process.env.GCLOUD_SERVICE_ACCOUNT_CREDS);
const authOptions = { credentials: serviceAccountCreds };
firestore.settings(authOptions);
}
กำหนดเครื่องมือดึงข้อมูล Firestore
ใช้ defineFirestoreRetriever()
เพื่อสร้างเครื่องมือดึงข้อมูลสําหรับการค้นหาที่อิงตามเวกเตอร์ของ Firestore
import { defineFirestoreRetriever } from '@genkit-ai/firebase';
import { initializeApp } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';
const app = initializeApp();
const firestore = getFirestore(app);
const retriever = defineFirestoreRetriever(ai, {
name: 'exampleRetriever',
firestore,
collection: 'documents',
contentField: 'text', // Field containing document content
vectorField: 'embedding', // Field containing vector embeddings
embedder: yourEmbedderInstance, // Embedder to generate embeddings
distanceMeasure: 'COSINE', // Default is 'COSINE'; other options: 'EUCLIDEAN', 'DOT_PRODUCT'
});
เรียกดูเอกสาร
หากต้องการดึงข้อมูลเอกสารโดยใช้เครื่องมือดึงข้อมูลที่กําหนด ให้ส่งอินสแตนซ์เครื่องมือดึงข้อมูลและตัวเลือกการค้นหาไปยัง ai.retrieve
const docs = await ai.retrieve({
retriever,
query: 'search query',
options: {
limit: 5, // Options: Return up to 5 documents
where: { category: 'example' }, // Optional: Filter by field-value pairs
collection: 'alternativeCollection', // Optional: Override default collection
},
});
ตัวเลือกการเรียกข้อมูลที่ใช้ได้
ตัวเลือกต่อไปนี้สามารถส่งไปยังช่อง options
ใน ai.retrieve
limit
: (number) ระบุจำนวนเอกสารสูงสุดที่จะดึงข้อมูล ค่าเริ่มต้นคือ10
where
: (Record<string, any>) เพิ่มตัวกรองเพิ่มเติมตามช่อง Firestore ตัวอย่างwhere: { category: 'news', status: 'published' }
collection
: (สตริง) ลบล้างคอลเล็กชันเริ่มต้นที่ระบุในการกําหนดค่าเครื่องมือดึงข้อมูลซึ่งมีประโยชน์สำหรับการค้นหาคอลเล็กชันย่อยหรือสลับระหว่าง
คอลเล็กชัน
ป้อนข้อมูล Firestore ด้วยเนื้อหาที่ฝัง
หากต้องการป้อนข้อมูลในคอลเล็กชัน Firestore ให้ใช้เครื่องมือสร้างการฝังร่วมกับ Admin SDK ตัวอย่างเช่น สคริปต์การส่งผ่านข้อมูลเมนูจากหน้าการสร้างที่เพิ่มการดึงข้อมูลสามารถปรับให้เหมาะกับ Firestore ได้ดังนี้
import { genkit } from 'genkit';
import { vertexAI, textEmbedding004 } from "@genkit-ai/vertexai";
import { applicationDefault, initializeApp } from "firebase-admin/app";
import { FieldValue, getFirestore } from "firebase-admin/firestore";
import { chunk } from "llm-chunk";
import pdf from "pdf-parse";
import { readFile } from "fs/promises";
import path from "path";
// Change these values to match your Firestore config/schema
const indexConfig = {
collection: "menuInfo",
contentField: "text",
vectorField: "embedding",
embedder: textEmbedding004,
};
const ai = genkit({
plugins: [vertexAI({ location: "us-central1" })],
});
const app = initializeApp({ credential: applicationDefault() });
const firestore = getFirestore(app);
export async function indexMenu(filePath: string) {
filePath = path.resolve(filePath);
// Read the PDF.
const pdfTxt = await extractTextFromPdf(filePath);
// Divide the PDF text into segments.
const chunks = await chunk(pdfTxt);
// Add chunks to the index.
await indexToFirestore(chunks);
}
async function indexToFirestore(data: string[]) {
for (const text of data) {
const embedding = (await ai.embed({
embedder: indexConfig.embedder,
content: text,
}))[0].embedding;
await firestore.collection(indexConfig.collection).add({
[indexConfig.vectorField]: FieldValue.vector(embedding),
[indexConfig.contentField]: text,
});
}
}
async function extractTextFromPdf(filePath: string) {
const pdfFile = path.resolve(filePath);
const dataBuffer = await readFile(pdfFile);
const data = await pdf(dataBuffer);
return data.text;
}
Firestore อาศัยดัชนีในการค้นหาคอลเล็กชันอย่างรวดเร็วและมีประสิทธิภาพ (โปรดทราบว่า "ดัชนี" ในที่นี้หมายถึงดัชนีฐานข้อมูล ไม่ใช่ตัวจัดทําดัชนีและตัวดึงข้อมูลของ Genkit)
ตัวอย่างก่อนหน้านี้กำหนดให้ต้องจัดทำดัชนีฟิลด์ embedding
จึงจะทำงานได้
วิธีสร้างดัชนี
เรียกใช้คําสั่ง
gcloud
ที่อธิบายไว้ในส่วนสร้างดัชนีเวกเตอร์แบบช่องเดียวของเอกสาร Firestoreคำสั่งจะมีลักษณะดังต่อไปนี้
gcloud alpha firestore indexes composite create --project=your-project-id \ --collection-group=yourCollectionName --query-scope=COLLECTION \ --field-config=vector-config='{"dimension":"768","flat": "{}"}',field-path=yourEmbeddingField
อย่างไรก็ตาม การกําหนดค่าการจัดทำดัชนีที่ถูกต้องจะขึ้นอยู่กับการค้นหาที่คุณทําและรูปแบบการฝังที่คุณใช้
หรือเรียกใช้
ai.retrieve()
แล้ว Firestore จะแสดงข้อผิดพลาดพร้อมคำสั่งที่ถูกต้องในการสร้างดัชนี
ดูข้อมูลเพิ่มเติม
- ดูการพูดคุยทั่วไปเกี่ยวกับเครื่องมือจัดทําดัชนีและเครื่องมือดึงข้อมูลใน Genkit ได้ที่หน้าการสร้างที่เพิ่มการดึงข้อมูล
- ดูข้อมูลเพิ่มเติมเกี่ยวกับฟีเจอร์การค้นหาเวกเตอร์ได้ที่การค้นหาด้วยการฝังเวกเตอร์ในเอกสารประกอบของ Cloud Firestore
ทำให้โฟลว์ใช้งานได้เป็น Cloud Functions
หากต้องการทำให้โฟลว์ใช้งานได้ด้วย Cloud Functions ให้ใช้การรองรับ genkit ในตัวของไลบรารี Firebase Functions เมธอด onCallGenkit
ช่วยให้คุณสร้างฟังก์ชันที่เรียกใช้ได้จากโฟลว์ โดยรองรับสตรีมมิงและคําขอ JSON โดยอัตโนมัติ คุณใช้ SDK ของไคลเอ็นต์ Cloud Functions เพื่อเรียกใช้ฟังก์ชันเหล่านี้ได้
import { onCallGenkit } from 'firebase-functions/https';
import { defineSecret } from 'firebase-functions/params';
export const exampleFlow = ai.defineFlow({
name: "exampleFlow",
}, async (prompt) => {
// Flow logic goes here.
return response;
}
);
// WARNING: This has no authentication or app check protections.
// See github.com/firebase/genkit/blob/main/docs/auth.md for more information.
export const example = onCallGenkit({ secrets: [apiKey] }, exampleFlow);
เผยแพร่ขั้นตอนโดยใช้ Firebase CLI โดยทำดังนี้
firebase deploy --only functions