이 가이드에서는 Firebase용 Vertex AI SDK를 사용하여 앱에서 직접 Vertex AI Gemini API 호출을 시작하는 방법을 설명합니다.
기본 요건
이 가이드에서는 Android 스튜디오를 사용하여 Android용 앱을 개발하는 데 익숙하다고 가정합니다.
개발 환경과 Android 앱이 다음 요구사항을 충족하는지 확인합니다.
- Android 스튜디오 (최신 버전)
- Android 앱은 API 수준 21 이상을 타겟팅해야 합니다.
(선택사항) 샘플 앱을 확인합니다.
SDK를 빠르게 사용해 보거나, 다양한 사용 사례의 전체 구현을 살펴보거나, 자체 Android 앱이 없는 경우 샘플 앱을 사용할 수 있습니다. 샘플 앱을 사용하려면 Firebase 프로젝트에 연결해야 합니다.
1단계: Firebase 프로젝트 설정 및 Firebase에 앱 연결
이미 Firebase 프로젝트와 Firebase에 연결된 앱이 있는 경우
Firebase Console에서 Gemini로 빌드 페이지로 이동한 후 두 번째 카드를 클릭하여 다음 작업을 수행하는 데 도움이 되는 워크플로를 시작합니다. Vertex AI 콘솔에 탭이 표시되면 이러한 작업이 완료된 것입니다
사용한 만큼만 지불하는 Blaze 요금제를 사용하도록 프로젝트를 업그레이드합니다.
프로젝트에 다음 두 API를 사용 설정합니다.
aiplatform.googleapis.com
및firebaseml.googleapis.com
이 가이드의 다음 단계로 진행하여 앱에 SDK를 추가하세요.
아직 Firebase 프로젝트와 Firebase에 연결된 앱이 없는 경우
2단계: SDK 추가
Firebase 프로젝트가 설정되고 앱이 Firebase에 연결되었으면(이전 단계 참조) 이제 Firebase용 Vertex AI SDK를 앱에 추가할 수 있습니다.
Android용 Firebase용 Vertex AI SDK (firebase-vertexai
)를 사용하면 Vertex AI Gemini API에 액세스할 수 있습니다.
모듈 (앱 수준) Gradle 구성 파일(예: <project>/<app-module>/build.gradle.kts
)에서 Android용 Firebase용 Vertex AI SDK의 종속 항목을 추가합니다.
Kotlin+KTX
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")
}
Java
Java의 경우 라이브러리를 두 개 더 추가해야 합니다.
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")
// 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")
}
3단계: Vertex AI 서비스 및 생성 모델 초기화
API 호출을 하려면 먼저 Vertex AI 서비스와 생성 모델을 초기화해야 합니다.
Kotlin+KTX
Kotlin의 경우 이 SDK의 메서드는 정지 함수이며 코루틴 범위에서 호출해야 합니다.// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")
Java
Java의 경우 이 SDK의 스트리밍 메서드가 반응형 스트림 라이브러리에서Publisher
유형을 반환합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-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()
를 사용하여 텍스트 전용 프롬프트 요청에서 텍스트를 생성할 수 있습니다.
Kotlin+KTX
Kotlin의 경우 이 SDK의 메서드는 정지 함수이며 코루틴 범위에서 호출해야 합니다.// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-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
Java의 경우 이 SDK의 메서드는ListenableFuture
를 반환합니다.
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-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의 다른 기능 사용해 보기
- 응답을 스트리밍하는 방법을 포함하여 텍스트 전용 프롬프트에서 텍스트를 생성하는 방법에 대해 자세히 알아보세요.
- 멀티모달 프롬프트에서 텍스트, 이미지, PDF, 동영상, 오디오 등의 텍스트를 생성합니다.
- 멀티턴 대화 (채팅)를 빌드합니다.
- 함수 호출을 사용하여 생성 모델을 외부 시스템 및 정보에 연결합니다.
콘텐츠 생성 제어 방법 알아보기
- 권장사항, 전략, 예시 프롬프트 등 프롬프트 설계를 이해합니다.
- 온도 및 최대 출력 토큰과 같은 모델 매개변수를 구성합니다.
- 안전 설정을 사용하여 유해한 것으로 간주될 수 있는 응답을 받을 가능성을 조정합니다.
Firebase용 Vertex AI 사용 경험에 대한 의견 보내기