이 빠른 시작에서는 원하는 언어별로 Google Gen AI SDK를 설치하고 첫 번째 API 요청을 만드는 방법을 보여줍니다. 샘플은 Vertex AI에 인증할 때 API 키를 사용하는지, 아니면 애플리케이션 기본 사용자 인증 정보(ADC)를 사용하는지에 따라 약간 달라집니다.
인증 방법 선택:
시작하기 전에
아직 ADC를 구성하지 않은 경우 다음 안내를 따르세요.
프로젝트 구성
프로젝트를 선택하고, 결제를 사용 설정하고, Vertex AI API를 사용 설정하고, gcloud CLI를 설치합니다.
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.
-
gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.
-
gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.
gcloud init
로컬 인증 사용자 인증 정보 만들기
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
필요한 역할
Vertex AI에서 Gemini API를 사용하기 위해 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 Vertex AI 사용자(roles/aiplatform.user
) IAM 역할을 부여해 달라고 요청하세요.
역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.
커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.
SDK 설치 및 환경 설정
로컬 머신에서 다음 탭 중 하나를 클릭하여 프로그래밍 언어의 SDK를 설치합니다.
Python Gen AI SDK
이 명령어를 실행하여 Gen AI SDK for Python을 설치하고 업데이트합니다.
pip install --upgrade google-genai
환경 변수를 설정합니다.
# Replace the `GOOGLE_CLOUD_PROJECT_ID` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT_ID export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
Go Gen AI SDK
이 명령어를 실행하여 Gen AI SDK for Go를 설치하고 업데이트합니다.
go get google.golang.org/genai
환경 변수를 설정합니다.
# Replace the `GOOGLE_CLOUD_PROJECT_ID` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT_ID export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
Node.js Gen AI SDK
이 명령어를 실행하여 Gen AI SDK for Node.js를 설치하고 업데이트합니다.
npm install @google/genai
환경 변수를 설정합니다.
# Replace the `GOOGLE_CLOUD_PROJECT_ID` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT_ID export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
Java Gen AI SDK
이 명령어를 실행하여 Gen AI SDK for Java를 설치하고 업데이트합니다.
Maven
pom.xml
에 다음을 추가합니다.
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>
환경 변수를 설정합니다.
# Replace the `GOOGLE_CLOUD_PROJECT_ID` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT_ID export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
REST
환경 변수를 설정합니다.
GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT_ID GOOGLE_CLOUD_LOCATION=global API_ENDPOINT=YOUR_API_ENDPOINT MODEL_ID="gemini-2.5-flash" GENERATE_CONTENT_API="generateContent"
첫 번째 요청하기
generateContent
메서드를 사용하여 Vertex AI의 Gemini API에 요청을 보냅니다.
Python
Go
Node.js
Java
REST
이 프롬프트 요청을 전송하려면 명령줄에서 curl 명령어를 실행하거나 애플리케이션에 REST 호출을 포함하세요.
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $(gcloud auth print-access-token)" "https://${API_ENDPOINT}/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/${GOOGLE_CLOUD_LOCATION}/publishers/google/models/${MODEL_ID}:${GENERATE_CONTENT_API}" -d $'{ "contents": { "role": "user", "parts": { "text": "Explain how AI works in a few words" } } }'
모델이 응답을 반환합니다. 응답은 여러 섹션으로 생성되고, 안전을 위해 각 섹션이 개별적으로 평가됩니다.
이미지 생성
Gemini는 대화형으로 이미지를 생성하고 처리할 수 있습니다. 텍스트, 이미지 또는 둘 다를 조합해 Gemini에 프롬프트를 제공하여 이미지 생성 및 수정과 같은 다양한 이미지 관련 작업을 실행할 수 있습니다. 다음 코드는 설명이 포함된 프롬프트를 기반으로 이미지를 생성하는 방법을 보여줍니다.
구성에 responseModalities: ["TEXT", "IMAGE"]
를 포함해야 합니다. 이러한 모델에서는 이미지 전용 출력이 지원되지 않습니다.
Python
Node.js
Java
이미지 이해
Gemini는 이미지를 이해할 수도 있습니다. 다음 코드는 이전 섹션에서 생성된 이미지를 사용하고 다른 모델을 사용하여 이미지에 대한 정보를 추론합니다.
Python
Go
Node.js
Java
코드 실행
Vertex AI의 Gemini API 코드 실행 기능을 통해 모델은 Python 코드를 생성 및 실행하고 최종 출력을 도출할 때까지 결과를 반복적으로 학습합니다. Vertex AI는 함수 호출과 유사하게 코드 실행을 도구로 제공합니다. 이 코드 실행 기능을 사용하면 코드 기반 추론의 이점을 활용하며 텍스트 출력을 생성하는 애플리케이션을 빌드할 수 있습니다. 예를 들면 다음과 같습니다.
Python
Go
Node.js
Java
코드 실행의 더 많은 예시는 코드 실행 문서를 참조하세요.
다음 단계
첫 번째 API 요청을 완료했으므로 프로덕션 코드에 더 고급 Vertex AI 기능을 설정하는 방법을 보여주는 다음 가이드를 살펴보세요.