このガイドでは、Vertex AI SDK for Python のコードを PaLM API から Gemini API に移行する方法について説明します。Gemini では、テキストの会話とマルチターンの会話(チャット)の両方を生成できますが、レスポンスが PaLM の出力と異なる可能性があるため、レスポンスを必ず確認してください。
コードを移行する
vertexai.preview.generative_models
から GenerativeModel
を呼び出すようにインポートを変更して、TextGenerationModel
クラスと ChatModel
クラスの使用を GenerativeModel
クラスに置き換えます。
from_pretrained
クラスメソッドを使用する代わりに、コンストラクタを使用して GenerativeModel
をインスタンス化します。TextGenerationModel.from_pretrained('text-bison')
は GenerativeModel('gemini-1.0-pro')
に置き換えます。
TextGenerationModel.predict
は GenerativeModel.generate_content
に置き換えます。
- メソッドについては、
GenerativeModel
から同じメソッドを呼び出します。たとえば、ChatModel.start_chat
を GenerativeModel.start_chat
に置き換えます。
このページの例に示すように構文を更新します。
一般的な設定手順
Vertex AI の PaLM API と Gemini API のいずれでも設定プロセスは同じです。
pip install google-cloud-aiplatform
import vertexai
vertexai.init(project="PROJECT_ID", location="LOCATION")
ここで
テキスト生成: 基本
次のコードサンプルは、テキスト生成モデルの作成における 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.preview.generative_models import GenerativeModel
model = GenerativeModel("gemini-1.0-pro")
responses = model.generate_content("The opposite of hot is", stream=True)
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.preview.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,
},
stream=True
)
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.preview.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?
""",
stream=True
)
for response in responses:
print(response.text)
responses = chat.send_message(
"""
Could you give me a catchy title for the paper?
""",
stream=True
)
for response in responses:
print(response.text)
|
次のステップ