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


이 가이드에서는 Vertex AI Gemini API를 앱에서 직접 Firebase용 Vertex AI SDK.

기본 요건

이 가이드에서는 개발자가 Flutter를 사용한 앱 개발에 익숙하다고 가정합니다.

  • 개발 환경과 웹 앱이 다음 사항을 충족하는지 확인합니다. 요구사항:

    • Dart 3.2.0 이상
  • (선택사항) 샘플 앱을 확인합니다.

    샘플 앱 다운로드

    SDK를 빠르게 사용해 보고 다양한 용도의 전체 구현을 확인할 수 있습니다. 자체 웹 앱이 없는 경우 샘플 앱을 사용하세요. 샘플 앱을 사용하려면 다음 작업을 완료해야 합니다. Firebase 프로젝트에 연결합니다.

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

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

  1. Firebase Console에서 Gemini로 빌드 페이지, 두 번째 카드를 클릭하면 작업을 수행할 수 있습니다 콘솔에 Vertex AI 탭이 표시되면 완료됩니다.

  2. 이 가이드의 다음 단계로 진행하여 앱에 SDK를 추가하세요.

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


2단계: SDK 추가

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

Flutter용 Vertex AI 플러그인 (firebase_vertexai)은 Vertex AI Gemini API에 대한 액세스 권한을 얻을 수 있습니다

  1. Flutter 프로젝트 디렉터리에서 다음 명령어를 실행하여 코어 플러그인을 설치합니다.

    flutter pub add firebase_core
    
  2. lib/main.dart 파일에서 Firebase core 플러그인 및 앞에서 생성한 구성 파일을 가져옵니다.

    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
    
  3. 또한 lib/main.dart 파일에서 구성 파일로 내보낸 DefaultFirebaseOptions 객체를 사용하여 Firebase를 초기화합니다.

    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    
  4. Flutter 애플리케이션을 다시 빌드합니다.

    flutter run
    
  5. Flutter 프로젝트 디렉터리에서 다음 명령어를 실행합니다.

    flutter pub add firebase_vertexai
  6. 완료되면 Flutter 프로젝트를 다시 빌드합니다.

    flutter run
    

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

API 호출을 하려면 먼저 Vertex AI를 초기화해야 합니다. 스토리지 서비스와 생성 모델을 빌드할 수 있습니다

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

// Initialize FirebaseApp
await Firebase.initializeApp();
// 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
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-1.5-flash');

시작 가이드를 완료했다면 Gemini 모델 및 (선택사항) 사용 사례와 앱에 적합한 위치를 선택합니다.

4단계: Vertex AI Gemini API 호출

이제 앱을 Firebase에 연결하고 SDK를 추가했으며 생성형 모델인 Vertex AI 서비스와 Vertex AI Gemini API를 호출할 준비가 되었습니다

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

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

await Firebase.initializeApp();
// 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
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-1.5-flash');

// Provide a prompt that contains text
final prompt = [Content.text('Write a story about a magic backpack.')];

// To generate text output, call generateContent with the text input
final response = await model.generateContent(prompt);
print(response.text);

가능한 작업

Gemini 모델 자세히 알아보기

자세히 알아보기: 다양한 사용 사례에 사용할 수 있는할당량 및 가격 책정에 대해 자세히 알아보세요.

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

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

또한 BigQuery ML을 사용하여 프롬프트와 모델 구성을 Vertex AI Studio


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