시작하기

Firebase Genkit를 시작하려면 Genkit CLI를 설치하고 Node.js 프로젝트에서 genkit init를 실행하세요. 이 페이지의 나머지 부분에서 방법을 확인하시기 바랍니다.

요구사항

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 (Google AI)

      가장 간단하게 시작하는 방법은 Google AI Gemini API를 사용하는 것입니다. 해당 리전에서 사용할 수 있는지 확인하세요.

      Google AI Studio를 사용하여 Gemini API를 위한 API 키를 생성합니다. 그런 다음 GOOGLE_GENAI_API_KEY 환경 변수를 키로 설정합니다.

      export GOOGLE_GENAI_API_KEY=<your API key>
      

      Gemini (Vertex AI)

      Google AI Gemini API가 지원되지 않는 리전이라면 Gemini와 다른 모델도 제공하는 Vertex AI API를 사용해 보세요 결제가 사용 설정된 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 명령어는 LLM이 특정 테마로 레스토랑 항목을 추천하도록 요청하는 단일 흐름 menuSuggestionFlow를 정의하는 샘플 소스 파일 index.ts를 만듭니다.

    이 파일은 다음과 같습니다 (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();
    

    Genkit를 사용하여 앱의 AI 기능을 빌드하면 입력 전처리, 보다 정교한 프롬프트 구성, 검색 증강 생성 (RAG)을 위한 외부 정보 소스 통합 등의 여러 단계로 흐름을 만들 수 있습니다.

  4. 이제 Genkit 기능과 샘플 프로젝트를 머신에서 로컬로 실행하고 탐색할 수 있습니다. Genkit 개발자 UI를 다운로드하여 시작합니다.

    genkit start
    

    Genkit 개발자 UI에 오신 것을 환영합니다.

    이제 Genkit 개발자 UI가 컴퓨터에서 실행됩니다. 다음 단계에서 모델이나 흐름을 실행하면 머신은 흐름의 단계가 함께 작동하도록 하는 데 필요한 조정 작업을 수행합니다. Gemini API와 같은 외부 서비스에 대한 호출은 라이브 서버에서 계속 실행됩니다.

    또한 개발 환경에 있으므로 Genkit는 트레이스와 흐름 상태를 로컬 파일에 저장합니다.

  5. genkit start 명령어를 실행하면 Genkit 개발자 UI가 자동으로 다운로드되고 열립니다.

    개발자 UI를 사용하면 정의한 흐름과 구성한 모델을 보고 실행하고 이전 실행의 트레이스를 검사할 수 있습니다. 다음 기능 중 일부를 사용해 보세요.

    • Run 탭에는 정의한 모든 흐름과 플러그인에서 구성한 모델의 목록이 표시됩니다.

      menuSuggestionFlow를 클릭하고 입력 텍스트 (예: "cat")로 실행해 보세요. 모두 순조롭게 진행되면 고양이 테마 식당의 메뉴 추천과 함께 보상이 받을 수 있습니다.

    • Inspect 탭에 흐름 실행 기록이 표시됩니다. 각 흐름에서 흐름에 전달된 매개변수와 실행 중인 각 단계의 트레이스를 확인할 수 있습니다.

다음 단계

Firebase, Cloud Run 또는 Node.js 플랫폼을 사용하여 Genkit 앱을 빌드하고 배포하는 방법을 알아보세요.