本頁面概述從 Microsoft Azure OpenAI 遷移至 Vertex AI 的 Gemini API 所需的步驟。
Gemini API 是全代管雲端服務,可讓您使用 Google Cloud 控制台建立及訓練生成模型。可讓您存取大型語言模型 (LLM),用於建立各種應用程式,包括聊天機器人、內容產生器和創意工具。
必要條件
如要將 OpenAI 服務從 Microsoft Azure OpenAI 遷移至 Vertex AI 中的 Gemini API,您必須先建立 Google Cloud 專案和開發環境。詳情請參閱「設定專案和開發環境」一文。
改用 Gemini API
請參閱下列主題,瞭解如何從 Microsoft Azure 中的 OpenAI 專案遷移至 Gemini API。
使用等同的 Gemini API 參數
以下列出一些常見的 Azure OpenAI 參數,以及 Gemini API 中的對應參數:
OpenAI 參數 | Gemini API 參數 | 說明 | 有效值 |
prompt |
prompt |
提示是提交給語言模型的自然語言要求,用來引導模型提供回覆。提示可以包含問題、指示、背景資訊、範例和文字,讓模型完成或繼續執行。 | 文字 |
temperature |
temperature |
溫度會在應用 topP 和 topK 時,用於回覆產生期間的取樣。溫度參數會決定選取詞元時的隨機程度。如果您想藉由提示生成較不具開放性和創意性的回覆,建議調低溫度參數。另一方面,如果溫度參數較高,則可能產生較多樣化或有創意的結果。如果溫度參數為 0 ,系統一律會選取可能性最高的詞元。在這種情況下,系統會根據特定提示提供的回覆,但仍可能出現少量變化。如果模型的回覆太普通、太短或提供了備用回覆,再試試看調高溫度參數。 |
0.0 到 1.0
|
max_tokens |
maxOutputTokens |
回覆內可以生成的符記數量上限。一個符記約為四個字元。100 個符記大約對應 60 到 80 個字詞。 如要取得較短的回覆,請指定較低的值;如要取得較長的回覆,請調高此值。 |
1-8192 (OpenAI)
|
無法使用 | topK |
「Top-K」會影響模型選取輸出符記的方式。如果「前 K 個」設為 1 ,代表下一個所選詞元是模型詞彙表的所有詞元中可能性最高者 (也稱為「貪婪解碼」)。如果「前 K 個」設為 3 ,則代表模型會依據溫度參數,從可能性最高的 3 個詞元中選取下一個詞元。在每個符記選取步驟中,模型會對機率最高的「Top-K」符記取樣,接著進一步根據「Top-P」篩選詞元,最後依 temperature 選出最終詞元。 如要取得較不隨機的回覆,請指定較低的值;如要取得較隨機的回覆,請調高此值。 |
1 到 40
|
top_p |
topP |
「Top-P」會影響模型選取輸出符記的方式。模型會按照可能性最高到最低的順序選取符記,直到所選符記的可能性總和等於 Top-P 值。舉例來說,假設詞元 A、B 和 C 的可能性分別為 0.3、0.2 和 0.1,而 Top-P 值為 0.5 ,模型會依據溫度參數選擇 A 或 B 做為下一個詞元,並排除 C 做為候選詞元。如要取得較不隨機的回覆,請指定較低的值;如要取得較隨機的回覆,請調高此值。 |
0.0 到 1.0
|
stop |
stop_sequences |
停止序列是一系列的字元 (包括空格),如果模型遇到這個序列,就會停止產生回應。這個序列不包含在回應中。您最多可以新增五個停止序列。 | 陣列中的停站序列,例如 ["###"] 。
|
使用等同的 Gemini API 模型
下表說明可用的基礎模型。
類型 | 說明 | OpenAI 端點 | Gemini API LLM 端點 |
文字 | 已進行微調,可遵循自然語言指示,適合多種語言任務。 | gpt-3.5-turbo 或 gpt-4
|
gemini-1.0-pro
|
Chat | 已針對多輪對話用途微調。 | gpt-3.5-turbo 或 gpt-4
|
gemini-1.0-pro
|
在 Vertex AI 中安裝、匯入及驗證 Gemini API
使用 Python 適用的 Vertex AI SDK 在 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 和 Azure 中的 Gemini API 比較和程式碼範例
使用 Python 適用的 Vertex AI SDK 產生文字
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)
搭配使用聊天完成功能與 Python 適用的 Vertex AI SDK
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)
使用 Python 適用的 Vertex AI SDK 產生程式碼
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 提示最佳化工具 (預先發布版),將提示集最佳化,以便搭配 Google 模型使用。
後續步驟
- 瞭解如何在 Vertex AI Studio 中測試提示。
- 進一步瞭解文字和即時通訊的提示設計。
- 進一步瞭解模型。