인증

OpenAI Python 라이브러리를 사용하려면 OpenAI SDK를 설치합니다.

pip install openai

Chat Completions API로 인증하려면 클라이언트 설정을 수정하거나 Google 인증 및 Vertex AI 엔드포인트를 사용하도록 환경 구성을 변경하면 됩니다. 더 쉬운 방법을 선택하고 Gemini 모델을 호출할지 아니면 자체 배포된 Model Garden 모델을 호출할지에 따라 설정 단계를 따르세요.

Model Garden의 특정 모델과 지원되는 Hugging Face 모델은 요청을 처리하기 전에 먼저 Vertex AI 엔드포인트에 배포해야 합니다. Chat Completions API에서 이러한 자체 배포 모델을 호출할 때는 엔드포인트 ID를 지정해야 합니다. 기존 Vertex AI 엔드포인트를 나열하려면 gcloud ai endpoints list 명령어를 사용합니다.

클라이언트 설정

Python에서 프로그래매틱 방식으로 Google 사용자 인증 정보를 가져오려면 google-auth Python SDK를 사용하면 됩니다.

pip install google-auth requests

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import openai

from google.auth import default
import google.auth.transport.requests

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"
# location = "us-central1"

# Programmatically get an access token
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
credentials.refresh(google.auth.transport.requests.Request())
# Note: the credential lives for 1 hour by default (https://cloud.google.com/docs/authentication/token-types#at-lifetime); after expiration, it must be refreshed.

##############################
# Choose one of the following:
##############################

# If you are calling a Gemini model, set the ENDPOINT_ID variable to use openapi.
ENDPOINT_ID = "openapi"

# If you are calling a self-deployed model from Model Garden, set the
# ENDPOINT_ID variable and set the client's base URL to use your endpoint.
# ENDPOINT_ID = "YOUR_ENDPOINT_ID"

# OpenAI Client
client = openai.OpenAI(
    base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/{ENDPOINT_ID}",
    api_key=credentials.token,
)

기본적으로 서비스 계정 액세스 토큰은 1시간 동안 유효합니다. 서비스 계정 액세스 토큰의 수명을 연장하거나 정기적으로 토큰을 새로고침하고 openai.api_key 변수를 업데이트할 수 있습니다.

환경 변수

Google Cloud CLI를 설치합니다. OpenAI 라이브러리는 기본 클라이언트의 인증 및 엔드포인트를 변경하기 위해 OPENAI_API_KEYOPENAI_BASE_URL 환경 변수를 읽을 수 있습니다. 다음 변수를 설정합니다.

$ export PROJECT_ID=PROJECT_ID
$ export LOCATION=LOCATION
$ export OPENAI_API_KEY="$(gcloud auth application-default print-access-token)"

Gemini 모델을 호출하려면 MODEL_ID 변수를 설정하고 openapi 엔드포인트를 사용합니다.

$ export MODEL_ID=MODEL_ID
$ export OPENAI_BASE_URL="https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/openapi"

Model Garden에서 자체 배포된 모델을 호출하려면 ENDPOINT 변수를 설정하고 URL에 이를 대신 사용합니다.

$ export ENDPOINT=ENDPOINT_ID
$ export OPENAI_BASE_URL="https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT}"

다음으로 클라이언트를 초기화합니다.

client = openai.OpenAI()

Gemini Chat Completions API는 OAuth를 사용하여 단기 액세스 토큰으로 인증합니다. 기본적으로 서비스 계정 액세스 토큰은 1시간 동안 유효합니다. 서비스 계정 액세스 토큰의 수명을 연장하거나 정기적으로 토큰을 새로고침하고 openai.api_key 변수를 업데이트할 수 있습니다.

사용자 인증 정보 새로고침

다음 예시는 필요에 따라 사용자 인증 정보를 자동으로 새로고침하는 방법을 보여줍니다.

Python

from typing import Any

import google.auth
import google.auth.transport.requests
import openai


class OpenAICredentialsRefresher:
    def __init__(self, **kwargs: Any) -> None:
        # Set a placeholder key here
        self.client = openai.OpenAI(**kwargs, api_key="PLACEHOLDER")
        self.creds, self.project = google.auth.default(
            scopes=["https://www.googleapis.com/auth/cloud-platform"]
        )

    def __getattr__(self, name: str) -> Any:
        if not self.creds.valid:
            self.creds.refresh(google.auth.transport.requests.Request())

            if not self.creds.valid:
                raise RuntimeError("Unable to refresh auth")

            self.client.api_key = self.creds.token
        return getattr(self.client, name)



    # TODO(developer): Update and un-comment below lines
    # project_id = "PROJECT_ID"
    # location = "us-central1"

    client = OpenAICredentialsRefresher(
        base_url=f"https://{location}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{location}/endpoints/openapi",
    )

    response = client.chat.completions.create(
        model="google/gemini-2.0-flash-001",
        messages=[{"role": "user", "content": "Why is the sky blue?"}],
    )

    print(response)

다음 단계