Firebase Genkit

Firebase Genkit เป็นเฟรมเวิร์กโอเพนซอร์สที่ช่วยคุณสร้าง ติดตั้งใช้งาน และตรวจสอบแอปที่ทำงานด้วยระบบ AI ที่พร้อมสำหรับการใช้งานจริง

โลโก้ Genkit

Genkit ได้รับการออกแบบมาสำหรับนักพัฒนาแอปเพื่อช่วยให้คุณผสานรวมความสามารถของ AI ที่ทรงพลังเข้ากับแอปได้ง่ายๆ โดยใช้รูปแบบและกระบวนทัศน์ที่คุ้นเคย เราสร้างเครื่องมือนี้โดยทีมเบื้องหลัง Firebase โดยใช้ประโยชน์จากประสบการณ์ของเราในการสร้างเครื่องมือที่นักพัฒนาซอฟต์แวร์นับล้านทั่วโลกใช้งาน

ใช้ Genkit เพื่อสร้างแอปที่สร้างเนื้อหาที่กำหนดเอง ใช้การค้นหาความหมาย จัดการข้อมูลที่ไม่มีโครงสร้าง ตอบคำถามด้วยข้อมูลธุรกิจ ตัดสินใจได้อย่างอิสระ เรียบเรียงการเรียกเครื่องมือ และอีกมากมาย

ปัจจุบัน Genkit รองรับการพัฒนาฝั่งเซิร์ฟเวอร์ใน JavaScript/TypeScript (Node.js) โดยมีการรองรับ Go อยู่ระหว่างการพัฒนา

ลองติดตามการพัฒนาหรือมีส่วนร่วมของคุณเองที่ที่เก็บของ GitHub

ฟีเจอร์หลัก

Genkit พร้อมช่วยเหลือคุณในทุกขั้นตอนของการพัฒนา AI ตั้งแต่จุดเริ่มต้นของการสร้างต้นแบบไปจนถึงการตรวจสอบในเวอร์ชันที่ใช้งานจริง เราจึงมีเรื่องให้พูดถึงอีกมากมาย

เพื่อช่วยให้คุณเริ่มต้นใช้งานได้ เราขอแนะนำฟีเจอร์ Genkit ที่สำคัญ 10 รายการซึ่งเราคิดว่าคุณจะชอบ

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. เอาต์พุตที่มีโครงสร้าง

สร้างข้อมูลที่พิมพ์อย่างเข้มงวดด้วย Genkit โดยใช้สคีมา Zod วิธีนี้ช่วยให้คุณวิเคราะห์ข้อความที่ไม่มีโครงสร้าง สร้างเนื้อหาครีเอทีฟโฆษณา เลือกงาน และส่งผลลัพธ์กลับไปยังแอปในฐานะออบเจ็กต์ที่ปลอดภัยตามประเภทที่มีโครงสร้างได้

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 สำหรับโมเดลที่ทำงานแบบ Generative (เช่น การสร้างรูปภาพ) ไม่ใช่แค่ 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. เรียกใช้ขั้นตอนในเครื่อง

Generative AI สร้างผลลัพธ์ได้หลายรูปแบบ การทดลองจึงสำคัญสุดๆ UI สำหรับนักพัฒนาซอฟต์แวร์ Genkit ในเครื่องช่วยให้คุณโต้ตอบกับคอมโพเนนต์ AI ที่สำคัญได้ เช่น โมเดลและรีทรีฟเวอร์ ตลอดจนทดสอบโฟลว์ตั้งแต่ต้นจนจบด้วยตัวเอง รวมถึงโค้ดที่กำหนดเองทั้งหมดที่คุณเขียนไว้

7. ตรวจสอบการติดตาม

การแก้ไขข้อบกพร่องของเวิร์กโฟลว์หลายขั้นตอนที่ซับซ้อนด้วย AI อาจทำได้ยากเนื่องจากความสุ่มและกระบวนการที่ซ่อนอยู่ Genkit มีเครื่องมือตรวจสอบการติดตามใน UI ของนักพัฒนาซอฟต์แวร์ ซึ่งช่วยให้คุณตรวจสอบการติดตามสำหรับการเรียกใช้โมเดลแต่ละครั้งและขั้นตอนในกระบวนการได้ เครื่องมือนี้สามารถดูการติดตามข้อมูลจากการใช้งานจริงและแม้แต่การแสดงภาพ

8. เปิดกว้างและขยายได้

ระบบนิเวศ AI เติบโตเร็วกว่าที่ทีมใดทีมหนึ่งตามทันได้ Genkit มีโมเดลปลั๊กอินแบบเปิดที่มีการผสานรวมที่สร้างไว้ล่วงหน้ากับโมเดลใหม่ ตัวดึงข้อมูล และอีกมากมาย แม้ว่าทีม Genkit จะมีปลั๊กอินอย่างเป็นทางการจำนวนหนึ่งอยู่ แต่ทุกคนก็เผยแพร่ปลั๊กอิน Genkit ของตนเองไปยัง NPM ได้

หากไม่พบปลั๊กอินสำหรับการผสานรวมเฉพาะที่ต้องการ ก็ไม่มีปัญหา นามธรรมของ Genkit มีความยืดหยุ่นมากและทำให้สร้างคอมโพเนนต์ที่กำหนดเองซึ่งผสานรวมเข้ากับเฟรมเวิร์กได้อย่างง่ายดาย เช่น Firestore Fetchr ที่กำหนดเองนี้

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

ภาพหน้าจอ Cloud Trace

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 โมเดล Generative: Gemini Pro, Gemini 1.5 Pro, Gemini Pro Vision
โมเดลการฝัง: การฝังข้อความของตุ๊กแก
vertexai โมเดล Generative: Gemini Pro, Gemini Pro Vision, Gemini 1.5 Flash, Gemini 1.5 Pro, Imagen2, Anthropic Claude 3
โมเดลการฝัง: การฝังข้อความของ Gecko
ผู้ประเมิน: การประเมิน Vertex AI
ollama โมเดล Generative: โมเดลในพื้นที่จำนวนมาก เช่น Gemma, Llama 3, Mistral และอื่นๆ
chroma ฐานข้อมูลเวกเตอร์: ChromaDB
pinecone ฐานข้อมูลเวกเตอร์: Pinecone
google-cloud เครื่องมือการตรวจสอบ: Google Cloud Trace, Google Cloud Logging
firebase การทำให้ระบบคลาวด์ใช้งานได้: Cloud Functions, การตรวจสอบสิทธิ์ Firebase, App Check
ฐานข้อมูลเวกเตอร์: ร้านค้าเวกเตอร์ของ Cloud Firestore
langchain ใช้เชน LangChain และยูทิลิตีในขั้นตอน Genkit

เริ่มต้นใช้งาน

อ่านคู่มือเริ่มต้นใช้งานเพื่อดูวิธีติดตั้ง Genkit และเรียกใช้การทำงานของ AI แรก