เริ่มเลย

หากต้องการเริ่มต้นใช้งาน Firebase Genkit ให้ติดตั้ง Genkit CLI และเรียกใช้ genkit init ในโปรเจ็กต์ Node.js เนื้อหาที่เหลือของหน้านี้จะแสดงวิธีการ

ข้อกำหนด

Node.js 20 ขึ้นไป

ขั้นตอน

  1. ติดตั้ง Genkit CLI โดยเรียกใช้คำสั่งต่อไปนี้:

    npm i -g genkit
    
  2. สร้างโปรเจ็กต์โหนดใหม่โดยทำดังนี้

    mkdir genkit-intro && cd genkit-intro
    npm init -y
    

    ดูที่Package.json และตรวจสอบว่าตั้งค่าช่อง main เป็น lib/index.js แล้ว

  3. เริ่มต้นโปรเจ็กต์ Genkit โดยทำดังนี้

    genkit init
    
    1. เลือกแพลตฟอร์มอื่นเป็นตัวเลือกแพลตฟอร์มการทำให้ใช้งานได้ (มีเทมเพลตสำหรับ Firebase Cloud Functions และ Google Cloud Run ด้วย)

    2. เลือกรุ่นของคุณ:

      Gemini (AI ของ Google)

      วิธีที่ง่ายที่สุดในการเริ่มต้นใช้งานคือการใช้ Google AI Gemini API ตรวจสอบว่า ฟีเจอร์นี้พร้อมให้บริการในภูมิภาคของคุณ

      สร้างคีย์ API สำหรับ Gemini API โดยใช้ Google AI Studio จากนั้นตั้งค่าตัวแปรสภาพแวดล้อม GOOGLE_GENAI_API_KEY เป็นคีย์ของคุณ

      export GOOGLE_GENAI_API_KEY=<your API key>
      

      Gemini (Vertex AI)

      หาก Google AI Gemini API ไม่พร้อมให้บริการในภูมิภาคของคุณ ให้พิจารณาใช้ Vertex AI API ที่มี Gemini และโมเดลอื่นๆ ด้วย คุณจะต้องมีโปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงิน, เปิดใช้ AI Platform API และตั้งค่าตัวแปรสภาพแวดล้อมเพิ่มเติมดังนี้

      gcloud services enable aiplatform.googleapis.com
      export GCLOUD_PROJECT=<your project ID>
      export GCLOUD_LOCATION=us-central1
      

      ดูราคาของ Vertex AI ได้ที่ https://cloud.google.com/vertex-ai/generative-ai/pricing

    3. เลือกคำตอบเริ่มต้นสำหรับคำถามที่เหลือ ซึ่งจะเริ่มต้นโฟลเดอร์โปรเจ็กต์ด้วยโค้ดตัวอย่าง

    คำสั่ง genkit init จะสร้างตัวอย่างไฟล์ต้นฉบับ index.ts ซึ่งกำหนดขั้นตอนเดียว ซึ่งก็คือ menuSuggestionFlow ที่แจ้งให้ LLM แนะนำรายการสำหรับร้านอาหารด้วยธีมที่กำหนด

    ไฟล์นี้มีลักษณะดังต่อไปนี้ (ขั้นตอนการกำหนดค่าปลั๊กอินอาจดูแตกต่างออกไปหากคุณเลือก Vertex AI)

    import * as z from 'zod';
    
    // Import the Genkit core libraries and plugins.
    import { generate } from '@genkit-ai/ai';
    import { configureGenkit } from '@genkit-ai/core';
    import { defineFlow, startFlowsServer } from '@genkit-ai/flow';
    import { googleAI } from '@genkit-ai/googleai';
    
    // Import models from the Google AI plugin. The Google AI API provides access to
    // several generative models. Here, we import Gemini 1.5 Flash.
    import { gemini15Flash } from '@genkit-ai/googleai';
    
    configureGenkit({
      plugins: [
        // Load the Google AI plugin. You can optionally specify your API key
        // by passing in a config object; if you don't, the Google AI plugin uses
        // the value from the GOOGLE_GENAI_API_KEY environment variable, which is
        // the recommended practice.
        googleAI(),
      ],
      // Log debug output to tbe console.
      logLevel: 'debug',
      // Perform OpenTelemetry instrumentation and enable trace collection.
      enableTracingAndMetrics: true,
    });
    
    // Define a simple flow that prompts an LLM to generate menu suggestions.
    export const menuSuggestionFlow = defineFlow(
      {
        name: 'menuSuggestionFlow',
        inputSchema: z.string(),
        outputSchema: z.string(),
      },
      async (subject) => {
        // Construct a request and send it to the model API.
        const llmResponse = await generate({
          prompt: `Suggest an item for the menu of a ${subject} themed restaurant`,
          model: gemini15Flash,
          config: {
            temperature: 1,
          },
        });
    
        // Handle the response from the model API. In this sample, we just convert
        // it to a string, but more complicated flows might coerce the response into
        // structured output or chain the response into another LLM call, etc.
        return llmResponse.text();
      }
    );
    
    // Start a flow server, which exposes your flows as HTTP endpoints. This call
    // must come last, after all of your plug-in configuration and flow definitions.
    // You can optionally specify a subset of flows to serve, and configure some
    // HTTP server options, but by default, the flow server serves all defined flows.
    startFlowsServer();
    

    เมื่อสร้างฟีเจอร์ AI ของแอปด้วย Genkit คุณมีแนวโน้มที่จะสร้างโฟลว์ที่มีหลายขั้นตอน เช่น การประมวลผลอินพุต การสร้างพรอมต์ที่ซับซ้อนยิ่งขึ้น การผสานรวมแหล่งข้อมูลภายนอกเพื่อสร้างด้วยการดึงข้อมูลเพิ่มเติม (RAG) และอีกมากมาย

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

    genkit start
    

    ยินดีต้อนรับสู่ UI สำหรับนักพัฒนาซอฟต์แวร์ Genkit

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

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

  5. UI นักพัฒนาซอฟต์แวร์ Genkit จะดาวน์โหลดและเปิดขึ้นโดยอัตโนมัติเมื่อคุณเรียกใช้คำสั่ง genkit start

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

    • ในแท็บเรียกใช้ คุณจะเห็นรายการขั้นตอนทั้งหมดที่คุณกำหนด รวมถึงโมเดลทั้งหมดที่ปลั๊กอินกำหนดค่า

      คลิก MenuRecommendationionFlow แล้วลองใช้กับอินพุตข้อความ (เช่น "cat") หากทุกอย่างดูดี คุณจะได้รับคำแนะนำเมนูสำหรับร้านอาหารธีมแมว

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

ขั้นตอนถัดไป

ดูวิธีสร้างและทำให้แอป Genkit ใช้งานได้ด้วย Firebase, Cloud Run หรือแพลตฟอร์ม Node.js