前往 ai.google.dev 查看 | 在 Google Colab 中執行 | 在 GitHub 上查看原始碼 |
在這個筆記本中,您將瞭解如何使用 curl 指令或 Python 要求 API 呼叫 Gemini API,開始使用 Gemini API 調整服務。以下將說明如何調整 Gemini API 文字產生服務背後的文字模型。
設定
驗證
Gemini API 可讓您根據自己的資料調整模型。由於 API 金鑰是您的資料和經過調整的模型,因此需要更嚴格的存取權控管機制。
在執行本教學課程前,您必須先為專案設定 OAuth。
在 Colab 中,最簡單的設定方式就是將 client_secret.json
檔案的內容複製到 Colab 的「Secrets Manager」(位於左側面板的金鑰圖示下方),並使用密鑰名稱 CLIENT_SECRET
。
這個 gcloud 指令會將 client_secret.json
檔案轉換為可用來驗證服務的憑證。
try:
from google.colab import userdata
import pathlib
pathlib.Path('client_secret.json').write_text(userdata.get('CLIENT_SECRET'))
# Use `--no-browser` in colab
!gcloud auth application-default login --no-browser --client-id-file client_secret.json --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning'
except ImportError:
!gcloud auth application-default login --client-id-file client_secret.json --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning'
You are authorizing client libraries without access to a web browser. Please run the following command on a machine with a web browser and copy its output back here. Make sure the installed gcloud version is 372.0.0 or newer. gcloud auth application-default login --remote-bootstrap="https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=87071151422-n1a3cb6c7fvkfg4gmhdtmn5ulol2l4be.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgenerative-language.tuning&state=QIyNibWSaTIsozjmvZEkVBo6EcoW0G&access_type=offline&code_challenge=76c1ZiGvKN8cvlYfj3BmbCwE4e7tvrlwaX3REUX25gY&code_challenge_method=S256&token_usage=remote" Enter the output of the above command: https://localhost:8085/?state=QIyNibWSaTIsozjmvZEkVBo6EcoW0G&code=4/0AeaYSHBKrY911S466QjKQIFODoOPXlO1mWyTYYdrbELIDV6Hw2DKRAyro62BugroSvIWsA&scope=https://www.googleapis.com/auth/cloud-platform%20https://www.googleapis.com/auth/generative-language.tuning Credentials saved to file: [/content/.config/application_default_credentials.json] These credentials will be used by any library that requests Application Default Credentials (ADC).
使用 CURL 呼叫 REST API
本節會提供用來呼叫 REST API 的 curl 陳述式範例。您將瞭解如何建立調整工作、查看工作狀態,以及完成後發出推論呼叫。
設定變數
為週期性值設定變數,以供其餘 REST API 呼叫使用。這個程式碼使用 Python os
程式庫設定環境變數,所有程式碼儲存格皆可存取。
這會因 Colab 筆記本環境而異。下一個程式碼儲存格中的程式碼相當於在 bash 終端機中執行指令。
export access_token=$(gcloud auth application-default print-access-token)
export project_id=my-project-id
export base_url=https://generativelanguage.googleapis.com
import os
access_token = !gcloud auth application-default print-access-token
access_token = '\n'.join(access_token)
os.environ['access_token'] = access_token
os.environ['project_id'] = "[Enter your project-id here]"
os.environ['base_url'] = "https://generativelanguage.googleapis.com"
可列出調整過的模型
列出目前可用的調整模型,驗證您的驗證設定。
curl -X GET ${base_url}/v1beta/tunedModels \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${access_token}" \
-H "x-goog-user-project: ${project_id}"
建立經過調整的模型
如要建立調整過的模型,您必須將資料集傳遞至 training_data
欄位中的模型。
在此範例中,您將調整模型,在序列中產生下一個數字。舉例來說,如果輸入是 1
,模型應輸出 2
。如果輸入是 one hundred
,則輸出內容應為 one hundred one
。
curl -X POST $base_url/v1beta/tunedModels \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${access_token}" \
-H "x-goog-user-project: ${project_id}" \
-d '
{
"display_name": "number generator model",
"base_model": "models/gemini-1.0-pro-001",
"tuning_task": {
"hyperparameters": {
"batch_size": 2,
"learning_rate": 0.001,
"epoch_count":5,
},
"training_data": {
"examples": {
"examples": [
{
"text_input": "1",
"output": "2",
},{
"text_input": "3",
"output": "4",
},{
"text_input": "-3",
"output": "-2",
},{
"text_input": "twenty two",
"output": "twenty three",
},{
"text_input": "two hundred",
"output": "two hundred one",
},{
"text_input": "ninety nine",
"output": "one hundred",
},{
"text_input": "8",
"output": "9",
},{
"text_input": "-98",
"output": "-97",
},{
"text_input": "1,000",
"output": "1,001",
},{
"text_input": "10,100,000",
"output": "10,100,001",
},{
"text_input": "thirteen",
"output": "fourteen",
},{
"text_input": "eighty",
"output": "eighty one",
},{
"text_input": "one",
"output": "two",
},{
"text_input": "three",
"output": "four",
},{
"text_input": "seven",
"output": "eight",
}
]
}
}
}
}' | tee tunemodel.json
{ "name": "tunedModels/number-generator-model-dzlmi0gswwqb/operations/bvl8dymw0fhw", "metadata": { "@type": "type.googleapis.com/google.ai.generativelanguage.v1beta.CreateTunedModelMetadata", "totalSteps": 38, "tunedModel": "tunedModels/number-generator-model-dzlmi0gswwqb" } } % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2280 0 296 100 1984 611 4098 --:--:-- --:--:-- --:--:-- 4720
取得調整後模型狀態
模型在訓練期間的狀態會設為 CREATING
,並於訓練完成後變更為 ACTIVE
。
以下是一些 Python 程式碼,用於剖析回應 JSON 中產生的模型名稱。如果您在終端機中執行這個程式碼,可以嘗試使用 bash JSON 剖析器剖析回應。
import json
first_page = json.load(open('tunemodel.json'))
os.environ['modelname'] = first_page['metadata']['tunedModel']
print(os.environ['modelname'])
tunedModels/number-generator-model-dzlmi0gswwqb
使用模型名稱再次發出 GET
要求,取得包含狀態欄位的模型中繼資料。
curl -X GET ${base_url}/v1beta/${modelname} \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${access_token}" \
-H "x-goog-user-project: ${project_id}" | grep state
"state": "ACTIVE", % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5921 0 5921 0 0 13164 0 --:--:-- --:--:-- --:--:-- 13157
執行推論
調整工作完成後,您就能使用該工作產生文字服務文字。請試著輸入羅馬數字,例如 63 (LXIII)
curl -X POST $base_url/v1beta/$modelname:generateContent \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${access_token}" \
-H "x-goog-user-project: ${project_id}" \
-d '{
"contents": [{
"parts": [{
"text": "LXIII"
}]
}]
}' 2> /dev/null
{ "candidates": [ { "content": { "parts": [ { "text": "LXIV" } ], "role": "model" }, "finishReason": "STOP", "index": 0, "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } ], "promptFeedback": { "safetyRatings": [ { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "NEGLIGIBLE" } ] } }
模型的輸出結果不一定正確。如果經過調整的模型效能未達要求標準,您可以嘗試新增更多高品質的範例、調整超參數,或是在範例中加入前置參數。您甚至可以根據自己建立的第一個模型,建立其他經過調整的模型。
請參閱調整指南,進一步瞭解如何改善效能。
透過 Python 要求呼叫 REST API
您可使用任何可讓您傳送 http 要求的程式庫呼叫其餘 API。下一組範例使用 Python 要求程式庫,並示範一些更進階的功能。
設定變數
access_token = !gcloud auth application-default print-access-token
access_token = '\n'.join(access_token)
project = '[Enter your project-id here]'
base_url = "https://generativelanguage.googleapis.com"
匯入 requests
程式庫。
import requests
import json
可列出調整過的模型
列出目前可用的調整模型,驗證您的驗證設定。
headers={
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json',
'x-goog-user-project': project
}
result = requests.get(
url=f'{base_url}/v1beta/tunedModels',
headers = headers,
)
result.json()
建立經過調整的模型
與 Curl 範例相同,您是透過 training_data
欄位傳入資料集。
operation = requests.post(
url = f'{base_url}/v1beta/tunedModels',
headers=headers,
json= {
"display_name": "number generator",
"base_model": "models/gemini-1.0-pro-001",
"tuning_task": {
"hyperparameters": {
"batch_size": 4,
"learning_rate": 0.001,
"epoch_count":5,
},
"training_data": {
"examples": {
"examples": [
{
'text_input': '1',
'output': '2',
},{
'text_input': '3',
'output': '4',
},{
'text_input': '-3',
'output': '-2',
},{
'text_input': 'twenty two',
'output': 'twenty three',
},{
'text_input': 'two hundred',
'output': 'two hundred one',
},{
'text_input': 'ninety nine',
'output': 'one hundred',
},{
'text_input': '8',
'output': '9',
},{
'text_input': '-98',
'output': '-97',
},{
'text_input': '1,000',
'output': '1,001',
},{
'text_input': '10,100,000',
'output': '10,100,001',
},{
'text_input': 'thirteen',
'output': 'fourteen',
},{
'text_input': 'eighty',
'output': 'eighty one',
},{
'text_input': 'one',
'output': 'two',
},{
'text_input': 'three',
'output': 'four',
},{
'text_input': 'seven',
'output': 'eight',
}
]
}
}
}
}
)
operation
<Response [200]>
operation.json()
{'name': 'tunedModels/number-generator-wl1qr34x2py/operations/41vni3zk0a47', 'metadata': {'@type': 'type.googleapis.com/google.ai.generativelanguage.v1beta.CreateTunedModelMetadata', 'totalSteps': 19, 'tunedModel': 'tunedModels/number-generator-wl1qr34x2py'} }
使用調整後模型的名稱設定變數,以便用於其他呼叫。
name=operation.json()["metadata"]["tunedModel"]
name
'tunedModels/number-generator-wl1qr34x2py'
取得調整後模型狀態
如要查看調整工作的進度,請查看狀態欄位。CREATING
表示調整工作仍在進行,ACTIVE
表示已訓練完成,且經過調整的模型可供使用。
tuned_model = requests.get(
url = f'{base_url}/v1beta/{name}',
headers=headers,
)
tuned_model.json()
以下程式碼會每 5 秒檢查一次狀態欄位,直到狀態不再處於 CREATING
狀態為止。
import time
import pprint
op_json = operation.json()
response = op_json.get('response')
error = op_json.get('error')
while response is None and error is None:
time.sleep(5)
operation = requests.get(
url = f'{base_url}/v1/{op_json["name"]}',
headers=headers,
)
op_json = operation.json()
response = op_json.get('response')
error = op_json.get('error')
percent = op_json['metadata'].get('completedPercent')
if percent is not None:
print(f"{percent:.2f}% - {op_json['metadata']['snapshots'][-1]}")
print()
if error is not None:
raise Exception(error)
100.00% - {'step': 19, 'epoch': 5, 'meanLoss': 1.402067, 'computeTime': '2024-03-14T15:11:23.766989274Z'}
執行推論
調整工作完成後,即可使用基本文字模型產生文字。請試著輸入日文數字,例如 6 (六)。
import time
m = requests.post(
url = f'{base_url}/v1beta/{name}:generateContent',
headers=headers,
json= {
"contents": [{
"parts": [{
"text": "六"
}]
}]
})
import pprint
pprint.pprint(m.json())
{'candidates': [{'content': {'parts': [{'text': '七'}], 'role': 'model'}, 'finishReason': 'STOP', 'index': 0, 'safetyRatings': [{'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability': 'NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability': 'NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability': 'LOW'}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability': 'NEGLIGIBLE'}]}], 'promptFeedback': {'safetyRatings': [{'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability': 'NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability': 'NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability': 'NEGLIGIBLE'}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability': 'NEGLIGIBLE'}]} }
模型的輸出結果不一定正確。如果經過調整的模型效能未達要求標準,您可以嘗試新增更多高品質的範例、調整超參數,或是在範例中加入前置參數。
結論
雖然訓練資料不含任何有關羅馬或日文數字的參照,但經過微調後,模型還是能夠產生良好的一般化情況。如此一來,就能根據用途調整模型。
後續步驟
如要瞭解如何透過 Gemini API 的 Python SDK 使用調整服務,請參閱使用 Python 調整快速入門導覽課程。如要瞭解如何在 Gemini API 中使用其他服務,請參閱 Python 快速入門導覽課程。