ปลั๊กอิน Firebase มีการผสานรวมหลายอย่างกับบริการ Firebase
- ตัวจัดทำดัชนีและรีทรีฟเวอร์โดยใช้ที่เก็บเวกเตอร์ของ Cloud Firestore
- ติดตามพื้นที่เก็บข้อมูลโดยใช้ Cloud Firestore
- การทำให้โฟลว์ใช้งานได้โดยใช้ Cloud Functions
- นโยบายการให้สิทธิ์สำหรับผู้ใช้การตรวจสอบสิทธิ์ Firebase
การติดตั้ง
npm i --save @genkit-ai/firebase
การกำหนดค่า
หากต้องการใช้ปลั๊กอินนี้ ให้ระบุปลั๊กอินเมื่อเรียกใช้ configureGenkit()
:
import {firebase} from "@genkit-ai/firebase";
configureGenkit({
plugins: [firebase({projectId: "your-firebase-project"})],
});
ปลั๊กอินกำหนดให้คุณต้องระบุรหัสโปรเจ็กต์ Firebase คุณระบุรหัสโปรเจ็กต์ Firebase ได้ด้วยวิธีใดวิธีหนึ่งต่อไปนี้
ตั้งค่า
projectId
ในออบเจ็กต์การกำหนดค่าfirebase()
ตั้งค่าตัวแปรสภาพแวดล้อม
GCLOUD_PROJECT
หากคุณเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud Functions, Cloud Run และอื่นๆ) ระบบจะตั้งค่าGCLOUD_PROJECT
เป็นรหัสโปรเจ็กต์ของสภาพแวดล้อมโดยอัตโนมัติหากตั้งค่า
GCLOUD_PROJECT
คุณจะละเว้นพารามิเตอร์การกําหนดค่าได้ ดังนี้firebase()
หากต้องการระบุข้อมูลเข้าสู่ระบบ Firebase คุณจะต้องตั้งค่าข้อมูลรับรองเริ่มต้นของแอปพลิเคชัน Google Cloud ด้วย วิธีระบุข้อมูลเข้าสู่ระบบ
หากคุณเรียกใช้โฟลว์จากสภาพแวดล้อม Google Cloud (Cloud Functions, Cloud Run และอื่นๆ) ระบบจะตั้งค่านี้โดยอัตโนมัติ
สำหรับสภาพแวดล้อมอื่นๆ:
- สร้างข้อมูลเข้าสู่ระบบบัญชีบริการสำหรับโปรเจ็กต์ Firebase และดาวน์โหลดไฟล์คีย์ JSON ทำได้ในหน้าบัญชีบริการของคอนโซล Firebase
- ตั้งค่าตัวแปรสภาพแวดล้อม
GOOGLE_APPLICATION_CREDENTIALS
เป็นเส้นทางไฟล์ของไฟล์ JSON ที่มีคีย์บัญชีบริการ
การใช้งาน
ปลั๊กอินนี้มีการผสานรวมหลายอย่างกับบริการ Firebase ซึ่งคุณสามารถ ใช้ร่วมกันหรือทีละรายการก็ได้
ร้านค้าเวกเตอร์ใน Cloud Firestore
คุณสามารถใช้ Cloud Firestore เป็นที่เก็บเวกเตอร์สำหรับการจัดทำดัชนีและการดึงข้อมูล RAG
ปลั๊กอิน firebase
มีฟังก์ชันความสะดวกในการกำหนดตัวทดลองใช้ Firestore defineFirestoreRetriever()
ดังนี้
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 yourRetrieverRef = defineFirestoreRetriever({
name: "yourRetriever",
firestore: getFirestore(app),
collection: "yourCollection",
contentField: "yourDataChunks",
vectorField: "embedding",
embedder: textEmbeddingGecko,
distanceMeasure: "COSINE", // "EUCLIDEAN", "DOT_PRODUCT", or "COSINE" (default)
});
หากต้องการใช้งาน ให้ส่งต่อไปยังฟังก์ชัน retrieve()
:
const docs = await retrieve({
retriever: yourRetrieverRef,
query: "look for something",
options: {limit: 5},
});
สำหรับการจัดทำดัชนี ให้ใช้เครื่องมือสร้างที่ฝังร่วมกับ Admin SDK ดังนี้
import {initializeApp} from "firebase-admin";
import {getFirestore, FieldValue} from "firebase-admin/firestore";
import {textEmbeddingGecko} from "@genkit-ai/vertexai";
import {embed} from "@genkit-ai/ai/embedder";
const app = initializeApp();
const firestore = getFirestore(app);
const indexConfig = {
collection: "yourCollection",
contentField: "yourDataChunks",
vectorField: "embedding",
embedder: textEmbeddingGecko,
};
async function indexToFirestore(content) {
const embedding = await embed({
embedder: indexConfig.embedder,
content,
});
await firestore.collection(indexConfig.collection).add({
[indexConfig.vectorField]: FieldValue.vector(embedding),
[indexConfig.contentField]: content,
});
}
Firestore ขึ้นอยู่กับดัชนีเพื่อให้การค้นหาคอลเล็กชันรวดเร็วและมีประสิทธิภาพ ตัวอย่างก่อนหน้านี้กำหนดให้ต้องจัดทำดัชนีช่อง embedding
จึงจะทำงานได้ เพื่อเรียกใช้ฟังก์ชัน แล้ว Firestore จะแสดงข้อผิดพลาดพร้อมคำสั่งเพื่อสร้างดัชนี เรียกใช้คำสั่งดังกล่าว ดัชนีของคุณควรพร้อมใช้งาน
ดูการพูดคุยทั่วไปเกี่ยวกับเครื่องมือจัดทำดัชนีและรีทรีฟเวอร์ในหน้า Retrieval-augmented Generation
พื้นที่เก็บข้อมูลการติดตามใน Cloud Firestore
คุณจะใช้ Cloud Firestore เพื่อจัดเก็บการติดตามได้โดยทำดังนี้
import {firebase} from "@genkit-ai/firebase";
configureGenkit({
plugins: [firebase()],
traceStore: "firebase",
enableTracingAndMetrics: true,
});
โดยค่าเริ่มต้น ปลั๊กอินจะจัดเก็บการติดตามในคอลเล็กชันที่ชื่อว่า genkit-traces
ในฐานข้อมูลเริ่มต้นของโปรเจ็กต์ หากต้องการเปลี่ยนการตั้งค่า ให้ทำดังนี้
firebase({
traceStore: {
collection: "your-collection";
databaseId: "your-db";
}
})
เมื่อใช้พื้นที่เก็บข้อมูลการติดตามแบบ Firestore คุณจะต้องเปิดใช้ TTL สำหรับเอกสารการติดตาม: https://firebase.google.com/docs/firestore/ttl
Cloud Functions
ปลั๊กอินนี้มีตัวสร้าง onFlow()
ซึ่งจะสร้างโฟลว์ที่สนับสนุนโดยฟังก์ชันที่ทริกเกอร์ด้วย HTTPS ของ Cloud สำหรับ Firebase ฟังก์ชันเหล่านี้สอดคล้องกับอินเทอร์เฟซฟังก์ชันที่เรียกใช้ได้ของ Firebase และคุณสามารถใช้ SDK ของไคลเอ็นต์ Cloud Functions ในการเรียกฟังก์ชันดังกล่าวได้
import {firebase} from "@genkit-ai/firebase";
import {onFlow, noAuth} from "@genkit-ai/firebase/functions";
configureGenkit({
plugins: [firebase()],
});
export const exampleFlow = onFlow(
{
name: "exampleFlow",
authPolicy: noAuth(), // WARNING: noAuth() creates an open endpoint!
},
async (prompt) => {
// Flow logic goes here.
return response;
}
);
ทำให้โฟลว์ใช้งานได้โดยใช้ Firebase CLI:
firebase deploy --only functions
ฟังก์ชัน onFlow()
มีตัวเลือกบางอย่างที่ไม่อยู่ใน defineFlow()
:
httpsOptions
: ออบเจ็กต์HttpsOptions
ที่ใช้กำหนดค่า Cloud Function:js export const exampleFlow = onFlow( { name: "exampleFlow", httpsOptions: { cors: true, }, // ... }, async (prompt) => { // ... } );
enforceAppCheck
: เมื่อtrue
ให้ปฏิเสธคำขอที่มีโทเค็น App Check ที่ขาดหายไปหรือไม่ถูกต้องconsumeAppCheckToken
: เมื่อtrue
ให้ทำให้โทเค็น App Check ไม่ถูกต้องหลังจากยืนยันแล้ว
Firebase Auth
ปลั๊กอินนี้มีฟังก์ชันตัวช่วยในการสร้างนโยบายการให้สิทธิ์เกี่ยวกับ Firebase Auth ดังนี้
import {firebaseAuth} from "@genkit-ai/firebase/auth";
export const exampleFlow = onFlow(
{
name: "exampleFlow",
authPolicy: firebaseAuth((user) => {
if (!user.email_verified) throw new Error("Requires verification!");
}),
},
async (prompt) => {
// ...
}
);
หากต้องการกำหนดนโยบายการตรวจสอบสิทธิ์ ให้ระบุ firebaseAuth()
ที่มีฟังก์ชันเรียกกลับที่ใช้ DecodedIdToken
เป็นพารามิเตอร์เพียงรายการเดียว ในฟังก์ชันนี้ ให้ตรวจสอบโทเค็นผู้ใช้และแสดงข้อผิดพลาด หากผู้ใช้มีคุณสมบัติไม่ตรงตามเกณฑ์ที่คุณต้องการ
โปรดดูการให้สิทธิ์และความสมบูรณ์สำหรับการสนทนาเกี่ยวกับหัวข้อนี้ที่ละเอียดยิ่งขึ้น