เริ่มเลย

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

ข้อกำหนด

Node.js 20 ขึ้นไป

คำแนะนำ: nvm และ เครื่องมือของ nvm-windows ซึ่งเป็นวิธีที่สะดวกในการติดตั้งโหนด

ติดตั้ง Genkit

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

npm i -g genkit

คำสั่งนี้จะติดตั้ง Genkit CLI ลงในไดเรกทอรีการติดตั้งโหนด เพื่อให้ใช้งานนอกโปรเจ็กต์โหนดได้

สร้างและสำรวจโปรเจ็กต์ตัวอย่าง

  1. สร้างโปรเจ็กต์โหนดใหม่โดยทำดังนี้

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

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

  2. เริ่มต้นโปรเจ็กต์ 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 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 คุณมีแนวโน้มที่จะ สร้างโฟลว์ที่มีหลายขั้นตอน เช่น การประมวลผลอินพุตล่วงหน้า และอื่นๆ การสร้างพรอมต์ที่ซับซ้อน การผสานรวมข้อมูลภายนอก แหล่งที่มาของการสร้างแบบ Augmented Reality (RAG) และอื่นๆ

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

    genkit start
    

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

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

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

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

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

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

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

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

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

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