Azure OpenAI から Gemini API に移行する

このページでは、Microsoft Azure OpenAI から Vertex AI の Gemini API に移行するために必要な手順について説明します。

Gemini API は、 Google Cloud コンソールを使用して生成モデルを作成、トレーニングできるクラウドベースのフルマネージド サービスです。大規模言語モデル(LLM)にアクセスして、chatbot、コンテンツ生成ツール、クリエイティブ ツールなど、さまざまなアプリケーションを作成できます。

前提条件

OpenAI サービスを Microsoft Azure OpenAI から Vertex AI の Gemini API に移行するには、まず Google Cloud プロジェクトと開発環境を作成する必要があります。詳細については、プロジェクトと開発環境を設定するをご覧ください。

Gemini API に移行する

Microsoft Azure の OpenAI プロジェクトから Gemini API に移行する方法については、次のトピックをご覧ください。

同等の Gemini API パラメータを使用する

以下に、一般的な Azure OpenAI パラメータと PaLM API パラメータの対応表を示します。

OpenAI パラメータ Gemini API パラメータ 説明 有効な値
prompt prompt プロンプトとは、レスポンスを受け取るために言語モデルに送信される自然言語リクエストです。プロンプトには、モデルを完了または続行するための質問、手順、コンテキスト情報、例、テキストを含めることができます。 テキスト
temperature temperature 温度は、レスポンス生成時のサンプリングに使用されます。レスポンスの生成は、topPtopK が適用された場合に行われます。温度は、トークン選択のランダム性の度合いを制御します。温度が低いほど、確定的で自由度や創造性を抑えたレスポンスが求められるプロンプトに適しています。一方、温度が高いと、より多様で創造的な結果を導くことができます。温度が 0 の場合、確率が最も高いトークンが常に選択されます。この場合、特定のプロンプトに対するレスポンスはほとんど確定的ですが、わずかに変動する可能性は残ります。

モデルが返すレスポンスが一般的すぎたり、短すぎたり、フォールバック(代替)レスポンスが返ってきたりする場合は、Temperature を高くしてみてください。

0.01.0

max_tokens maxOutputTokens レスポンスで生成できるトークンの最大数。1 トークンは約 4 文字です。100 トークンは約 60~80 語に相当します。

レスポンスを短くしたい場合は小さい値を、長くしたい場合は大きい値を指定します。

1-8192(OpenAI)

18192(Gemini API)

利用不可 topK トップ K は、モデルが出力用にトークンを選択する方法を変更します。Top-K が 1 の場合、次に選択されるトークンは、モデルの語彙内のすべてのトークンで最も確率の高いものであることになります(グリーディ デコードとも呼ばれます)。Top-K が 3 の場合は、最も確率が高い上位 3 つのトークンから次のトークン選択されることになります(温度を使用します)。

トークン選択のそれぞれのステップで、最も高い確率を持つ Top-K のトークンがサンプリングされます。その後、トークンは Top-P に基づいてさらにフィルタされ、最終的なトークンは温度サンプリングを使って選択されます。

ランダムなレスポンスを減らしたい場合は小さい値を、ランダムなレスポンスを増やしたい場合は大きい値を指定します。

140

top_p topP Top-P は、モデルが出力用にトークンを選択する方法を変更します。トークンは、確率の合計が Top-P 値に等しくなるまで、確率の高いものから低いものへと選択されます。たとえば、トークン A、B、C の確率が 0.3、0.2、0.1 で、Top-P 値が 0.5 だとします。このとき、モデルは次のトークンとして A か B を温度を使って選択し、C は候補から外します。

ランダムなレスポンスを減らしたい場合は小さい値を、ランダムなレスポンスを増やしたい場合は大きい値を指定します。

0.01.0

stop stop_sequences 停止シーケンスはスペースを含む一連の文字で、モデルは停止シーケンスを検出するとレスポンスの生成を停止します。このシーケンスはレスポンスには含まれません。停止シーケンスは 5 つまで追加できます。 配列内の停止シーケンス(例: ["###"])。

同等の Gemini API モデルを使用する

次の表に、使用可能な基盤モデルを示します。

説明 OpenAI エンドポイント Gemini API LLM エンドポイント
テキスト 自然言語による指示に対応できるようファインチューニングされていて、さまざまな言語タスクに適しています。 gpt-3.5-turbo または gpt-4 gemini-1.0-pro
チャット マルチターンの会話向けにファインチューニングされています。 gpt-3.5-turbo または gpt-4 gemini-1.0-pro

Vertex AI での Gemini API のインストール、インポート、認証

Vertex AI SDK for Python を使用して、Vertex AI で Gemini API のインストール、インポート、認証を行います。以下に、Vertex AI SDK for Python のメソッドと Azure OpenAI のメソッドの対応表を示します。

Vertex AI に Gemini API をインストールする

Azure OpenAI

$ pip install --upgrade openai

Vertex AI の Gemini API

$ pip install google-cloud-aiplatform

Vertex AI に Gemini API をインポートする

Azure OpenAI

import openai

Vertex AI の Gemini API

from vertexai.preview.generative_models import GenerativeModel

Vertex AI で Gemini API を認証する

Azure OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")

Vertex AI の Gemini API

from google.colab import auth as google_auth
google_auth.authenticate_user()

Vertex AI の Gemini API と Azure の比較とサンプルコード

Vertex AI SDK for Python を使用してテキストを生成する

Azure OpenAI

from openai import OpenAI

client = OpenAI()

response = client.completions.create(
    prompt="Write an article about the potential of AI",
    max_tokens=8192,
    temperature=0.3,
    model="gpt-4")

print(f"Response from Model: {response['choices'][0]['text']}")

Vertex AI の Gemini API

from vertexai.preview.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")
generation_config = {
    "max_output_tokens": 8192,
    "temperature": 0.9,
    "top_p": 1}

responses = model.generate_content(
    "Write an article about the potential of AI",
    generation_config=generation_config,
    stream=True)

for response in responses:
    print(response.text)

Vertex AI SDK for Python でチャット補完を使用する

Azure OpenAI

from openai import OpenAI

client = OpenAI()

parameters = {
    "model":"gpt-4",
    "temperature": 0.2,
    "max_tokens": 256,
    "top_p": 0.95}

chat_completion = client.chat.completions.create(
    messages=[
      {"role": "user", "name":"example_user", "content": "Hello! Can you write a 300 word article on the history of AI?"}
      ]
    ,
    **parameters)
response = chat_completion['choices'][0]
print(f"Response from Model: {response.text}")

chat_completion = client.chat.completions.create(
    messages=[
      {"role": "user", "name":"example_user", "content": "Could you give me a catchy title for the paper?"}
      ]
    ,
    **parameters)
response = chat_completion['choices'][0]
print(f"Response from Model: {response.text}")

Vertex AI の Gemini API

from vertexai.preview.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")
chat = model.start_chat()

responses = chat.send_message(
    content="Hello! Can you write a 300 word article on the history of AI?",
    stream=True)

for response in responses:
    print(response.text)

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

for response in responses:
    print(response.text)

Vertex AI SDK for Python を使用してコードを生成する

Azure OpenAI

from openai import OpenAI

client = OpenAI()

response = client.completions.create(
    prompt="Write a Python code to read a CSV file in pandas, calculate the average for a specific column, and then sort the data in descending order for that column",
    max_tokens=8192,
    temperature=0.3,
    model="gpt-4")

print(f"Response from Model: {response['choices'][0]['text']}")

Vertex AI の Gemini API

from vertexai.preview.generative_models import GenerativeModel

model = GenerativeModel("gemini-1.0-pro")
generation_config = {
    "max_output_tokens": 8192,
    "temperature": 0.9,
    "top_p": 1,
    }

responses = model.generate_content(
    contents="Write a Python code to read a CSV file in pandas, calculate the average for a specific column, and then sort the data in descending order for that column",
    generation_config=generation_config,
    stream=True)

for response in responses:
    print(response.text)

プロンプトを Gemini モデルに移行する

以前に Azure OpenAI で使用したプロンプト セットがある場合は、Vertex AI Prompt Optimizer(プレビュー)を使用して、Google モデルで使用するように最適化できます。

次のステップ