Firebase Genkit

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

โลโก้ Genkit

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

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

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

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

คุณลักษณะหลัก

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

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

1. หลากหลายโมเดล อินเทอร์เฟซเดียว

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

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

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
โมเดลการฝัง: การฝังข้อความ Gecko
vertexai โมเดลที่ AI สร้างขึ้น: 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 ขั้นตอนแรก