Vertex AI의 PaLM API에서 Gemini API로 마이그레이션

이 가이드에서는 PaLM API를 사용하는 방식에서 Gemini API를 사용하는 방식으로 Python용 Vertex AI SDK 코드를 마이그레이션하는 방법을 보여줍니다. Gemini로 텍스트, 멀티턴 대화(채팅), 코드를 생성할 수 있습니다. Gemini 출력은 PaLM 출력과 다를 수 있으므로 마이그레이션한 후 응답을 확인하세요. 자세한 내용은 Vertex AI SDK의 멀티모달 클래스 소개를 참조하세요.

Gemini와 PaLM의 차이점

Gemini와 PaLM 모델은 다음과 같은 몇 가지 차이점이 있습니다.

  • 응답 구조가 다릅니다. Gemini 응답 구조에 대한 자세한 내용은 Gemini API 모델 참조 응답 본문을 참조하세요.

  • 안전 카테고리가 다릅니다. Gemini와 PaLM 안전 설정의 차이점에 대한 자세한 내용은 Gemini와 다른 모델 계열의 주요 차이점을 참조하세요.

  • Gemini는 코드 완성을 수행할 수 없습니다. 코드 완성 애플리케이션을 만들어야 하는 경우 code-gecko 모델을 사용합니다. 자세한 내용은 Codey 코드 완성 모델을 참조하세요.

  • 코드 생성의 경우 Gemini의 인용 차단율이 더 높습니다.

  • 모델의 응답 신뢰도를 나타내는 Codey 코드 생성 모델의 신뢰도 점수는 Gemini에 노출되지 않습니다.

Gemini 모델을 사용하도록 PaLM 코드 업데이트

GenerativeModel 클래스의 메서드는 대부분 PaLM 클래스의 메서드와 동일합니다. 예를 들어 GenerativeModel.start_chat를 사용하여 PaLM에 해당하는 ChatModel.start_chat를 바꿉니다. 하지만 Google Cloud는 항상 Gemini를 개선하고 업데이트하므로 약간의 차이가 발생할 수 있습니다. 자세한 내용은 Python SDK 참조를 확인하세요.

PaLM API에서 Gemini API로 마이그레이션하려면 다음 코드 수정이 필요합니다.

  • 모든 PaLM 모델 클래스의 경우 Gemini의 GenerativeModel 클래스를 사용합니다.

  • GenerativeModel 클래스를 사용하려면 다음 import 문을 실행합니다.

    from vertexai.generative_models import GenerativeModel

  • Gemini 모델을 로드하려면 from_pretrained 메서드를 사용하는 대신 GenerativeModel 생성자를 사용합니다. 예를 들어 Gemini 1.0 Pro 모델을 로드하려면 GenerativeModel(gemini-1.0-pro)을 사용합니다.

  • Gemini에서 텍스트를 생성하려면 PaLM 모델에 사용되는 predict 메서드 대신 GenerativeModel.generate_content 메서드를 사용합니다. 예를 들면 다음과 같습니다.

   model = GenerativeModel("gemini-1.0-pro-002")
   response = model.generate_content("Write a short poem about the moon")

Gemini와 PaLM 클래스 비교

각 PaLM 모델 클래스는 Gemini의 GenerativeModel 클래스로 대체됩니다. 다음 표에서는 PaLM 모델에서 사용되는 클래스와 Gemini의 해당 클래스를 보여줍니다.

PaLM 모델 PaLM 모델 클래스 Gemini 모델 클래스
text-bison TextGenerationModel GenerativeModel
chat-bison ChatModel GenerativeModel
code-bison CodeGenerationModel GenerativeModel
codechat-bison CodeChatModel GenerativeModel

일반 설정 안내

Vertex AI의 PaLM API 및 Gemini API 모두 설정 프로세스는 동일합니다. 자세한 내용은 Python용 Vertex AI SDK 소개를 참조하세요. 다음은 Python용 Vertex AI SDK를 설치하는 짧은 코드 샘플입니다.

pip install google-cloud-aiplatform
import vertexai
vertexai.init(project="PROJECT_ID", location="LOCATION")

이 샘플 코드에서 PROJECT_ID를 Google Cloud 프로젝트 ID로 바꾸고 LOCATION을 Google Cloud 프로젝트의 위치(예: us-central1)로 바꿉니다.

Gemini 및 PaLM 코드 샘플

다음 각 코드 샘플 쌍에는 PaLM 코드와 PaLM 코드에서 마이그레이션된 Gemini 코드가 나란히 포함됩니다.

텍스트 생성: 기본

다음 코드 샘플은 텍스트 생성 모델을 만들기 위한 PaLM API와 Gemini API의 차이점을 보여줍니다.

PaLM Gemini

from vertexai.language_models import TextGenerationModel

model = TextGenerationModel.from_pretrained("text-bison@002")

response = model.predict(prompt="The opposite of hot is")
print(response.text) #  'cold.'
        

from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")

responses = model.generate_content("The opposite of hot is")

for response in responses:
    print(response.text)
        

매개변수를 사용하여 텍스트 생성

다음 코드 샘플은 선택적 매개변수를 사용하여 텍스트 생성 모델을 만들기 위한 PaLM API와 Gemini API의 차이점을 보여줍니다.

PaLM Gemini

from vertexai.language_models import TextGenerationModel

model = TextGenerationModel.from_pretrained("text-bison@002")

prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

response = model.predict(
    prompt=prompt,
    temperature=0.1,
    max_output_tokens=800,
    top_p=1.0,
    top_k=40
)

print(response.text)
        

from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")

prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

responses = model.generate_content(
    prompt,
    generation_config={
        "temperature": 0.1,
        "max_output_tokens": 800,
        "top_p": 1.0,
        "top_k": 40,
    }
  )

for response in responses:
    print(response.text)
        

채팅

다음 코드 샘플은 채팅 모델을 만들기 위한 PaLM API와 Gemini API의 차이점을 보여줍니다.

PaLM Gemini

from vertexai.language_models import ChatModel

model = ChatModel.from_pretrained("chat-bison@002")

chat = model.start_chat()

print(
    chat.send_message(
        """
Hello! Can you write a 300 word abstract for a research paper I need to write about the impact of AI on society?
"""
    )
)

print(
    chat.send_message(
        """
Could you give me a catchy title for the paper?
"""
    )
)
        

from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")

chat = model.start_chat()

responses = chat.send_message(
        """
Hello! Can you write a 300 word abstract for a research paper I need to write about the impact of AI on society?
"""
    )

for response in responses:
   print(response.text)

responses = chat.send_message(
        """
Could you give me a catchy title for the paper?
"""
    )

for response in responses:
   print(response.text)
        

코드 생성

다음 코드 샘플은 윤년 여부를 예측하는 함수를 만들기 위한 PaLM API와 Gemini API의 차이점을 보여줍니다.

Codey Gemini

from vertexai.language_models import CodeGenerationModel

model = CodeGenerationModel.from_pretrained("code-bison@002")

response = model.predict(
        prefix="Write a function that checks if a year is a leap year."
    )

print(response.text)
        

from vertexai.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro-002")

response = model.generate_content("Write a function that checks if a year is a leap year.")

print(response.text)
        

다음 단계