Firebase의 Vertex AI SDK를 사용하여 Gemini API 시작하기


이 가이드에서는 선택한 플랫폼의 Vertex AI in Firebase SDK를 사용하여 앱에서 직접 Vertex AI Gemini API를 호출하는 방법을 설명합니다.

기본 요건

이 가이드에서는 Android 스튜디오를 사용하여 Android용 앱을 개발하는 데 익숙하다고 가정합니다.

1단계: Firebase 프로젝트 설정 및 앱을 Firebase에 연결

Firebase 프로젝트와 Firebase에 연결된 앱이 이미 있는 경우

  1. Firebase 콘솔에서 Gemini로 빌드 페이지로 이동합니다.

  2. Vertex AI in Firebase 카드를 클릭하여 다음 작업을 완료하는 데 도움이 되는 워크플로를 실행합니다.

  3. 이 가이드의 다음 단계로 진행하여 앱에 SDK를 추가합니다.

아직 Firebase 프로젝트와 Firebase에 연결된 앱이 없는 경우

  1. Firebase Console에 로그인합니다.

  2. 프로젝트 만들기를 클릭하고 다음 옵션 중 하나를 사용합니다.

    • 옵션 1: '프로젝트 만들기' 워크플로의 첫 번째 단계에서 새 프로젝트 이름을 입력하여 완전히 새로운 Firebase 프로젝트 (및 기본 Google Cloud 프로젝트 자동 생성)를 만듭니다.

    • 옵션 2: '프로젝트 만들기' 워크플로의 첫 번째 단계에서 드롭다운 메뉴에서 Google Cloud 프로젝트 이름을 선택하여 기존 Google Cloud 프로젝트에 'Firebase를 추가'합니다.

    메시지가 표시되면 Vertex AI in Firebase SDK를 사용하기 위해 Google Analytics를 설정하지 않아도 됩니다.

  3. Firebase 콘솔에서 Gemini로 빌드 페이지로 이동합니다.

  4. Vertex AI in Firebase 카드를 클릭하여 다음 작업을 완료하는 데 도움이 되는 워크플로를 실행합니다.

  1. Console의 생성형 AI 워크플로를 계속 진행하여 앱을 Firebase에 연결합니다. 여기에는 다음 작업이 포함됩니다.

    • Firebase 프로젝트에 앱 등록

    • Firebase 구성 파일(google-services.json) 및 google-services Gradle 플러그인을 앱에 추가합니다.

  2. 이 가이드의 다음 단계에서는 앱에 Vertex AI in Firebase SDK를 추가하고 SDK 및 Gemini API 사용과 관련된 필수 초기화를 완료합니다.


2단계: SDK 추가

Firebase 프로젝트가 설정되고 앱이 Firebase에 연결되면(이전 단계 참고) 이제 앱에 Vertex AI in Firebase SDK를 추가할 수 있습니다.

Android용 Vertex AI in Firebase SDK (firebase-vertexai)는 Vertex AI Gemini API에 대한 액세스를 제공합니다.

모듈 (앱 수준) Gradle 파일(예: <project>/<app-module>/build.gradle.kts)에서 Android용 Vertex AI in Firebase 라이브러리의 종속 항목을 추가합니다. 라이브러리 버전 관리 제어에는 Firebase Android BoM을 사용하는 것이 좋습니다.

KotlinJava
dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.9.0"))

    // Add the dependency for the Vertex AI in Firebase library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-vertexai")
}

Java의 경우 라이브러리를 두 개 더 추가해야 합니다.

dependencies {
    // ... other androidx dependencies

    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.9.0"))

    // Add the dependency for the Vertex AI in Firebase library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-vertexai")

    // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
    implementation("com.google.guava:guava:31.0.1-android")

    // Required for streaming operations (to use `Publisher` from Reactive Streams)
    implementation("org.reactivestreams:reactive-streams:1.0.4")
}

Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전을 사용합니다.

Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

dependencies {
    // Add the dependency for the Vertex AI in Firebase library
    // When NOT using the BoM, you must specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-vertexai:16.1.0")
}

3단계: Vertex AI 서비스 및 생성형 모델 초기화

API를 호출하려면 먼저 Vertex AI 서비스와 생성 모델을 초기화해야 합니다.

KotlinJava
Kotlin의 경우 이 SDK의 메서드는 정지 함수이므로 코루틴 범위에서 호출해야 합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
Java의 경우 이 SDK의 스트리밍 메서드는 Reactive Streams 라이브러리Publisher 유형을 반환합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

시작 가이드를 완료한 후 사용 사례 및 앱에 적합한 Gemini 모델과 (선택사항) 위치를 선택하는 방법을 알아봅니다.

4단계: Vertex AI Gemini API 호출

이제 앱을 Firebase에 연결하고, SDK를 추가하고, Vertex AI 서비스와 생성형 모델을 초기화했으므로 Vertex AI Gemini API를 호출할 수 있습니다.

generateContent()를 사용하여 텍스트 전용 프롬프트 요청에서 텍스트를 생성할 수 있습니다.

KotlinJava
Kotlin의 경우 이 SDK의 메서드는 정지 함수이므로 코루틴 범위에서 호출해야 합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

// Provide a prompt that contains text
val prompt = "Write a story about a magic backpack."

// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(prompt)
print(response.text)
Java의 경우 이 SDK의 메서드는 ListenableFuture를 반환합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content prompt = new Content.Builder()
    .addText("Write a story about a magic backpack.")
    .build();

// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

또 뭘 할 수 있니?

Gemini 모델 자세히 알아보기

다양한 사용 사례에 사용할 수 있는 모델할당량 및 가격에 대해 알아보세요.

Gemini API의 다른 기능 사용해 보기

콘텐츠 생성을 제어하는 방법 알아보기

Vertex AI Studio를 사용하여 프롬프트 및 모델 구성을 실험할 수도 있습니다.


Vertex AI in Firebase 사용 경험에 관한 의견 보내기