시작하기

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

요구사항

Node.js 20 이상

권장사항: nvmnvm-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(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 명령어는 샘플 소스 파일 index.ts를 만듭니다. LLM이 제안하는 메시지를 표시하는 단일 흐름, menuSuggestionFlow를 정의합니다. 레스토랑의 항목을 검색합니다.

    이 파일은 다음과 같이 표시됩니다(플러그인 구성 단계가 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) 소스 등이 포함됩니다

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

    genkit start
    

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

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

    또한 개발 환경에 있으므로 Genkit는 trace 및 흐름 상태를 로컬 파일에 저장합니다.

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

    개발자 UI를 사용하면 정의한 흐름과 구성한 모델을 보고 이를 실행하며, 이전 실행의 trace를 검사할 수 있습니다. 다음과 같은 기능을 사용해 보세요.

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

      menuSuggestionFlow를 클릭하고 입력 텍스트 (예: "cat"). 순조롭게 진행되면 고양이를 위한 추천 메뉴를 드립니다 찾을 수 있습니다.

    • 검사 탭에 흐름 실행 기록이 표시됩니다. 각 흐름에 전달된 매개변수와 추적한 정보를 볼 수 있습니다.

다음 단계

Firebase로 Genkit 앱을 빌드하고 배포하는 방법을 알아보세요. Cloud Run 또는 모든 Node.js 플랫폼.