程式碼研究室簡介
1. 簡介
微調的重要性
基礎模型是針對一般用途訓練,有時無法順利執行任務。這可能是因為您要模型執行的工作是專門工作,僅使用提示設計就很難教導模型。在這種情況下,您可以使用模型調整功能,改善模型在特定任務上的效能。以及確保指令不夠充分時,模型還是能符合特定的輸出需求。大型語言模型 (LLM) 可提供大量資訊,並執行許多工作,但只有在接受專門訓練時才能發揮優異的表現。微調可訓練 LLM,讓您根據特定需求調整預先訓練的 LLM。
在本程式碼研究室中,您將瞭解如何針對 LLM 模型使用監督式調整方法進行微調。
監督式調整可透過教導新技能來提升模型效能。包含數百個標記範例的資料,可用於教導模型模仿所需行為或工作。我們會提供輸入文字 (提示) 和輸出文字 (回應) 的標記資料集,教導模型如何針對特定用途自訂回應。
如要進一步瞭解模型自訂功能,請參閱這篇文章。
建構項目
用途:產生新聞文章標題
假設您想自動產生新聞文章的標題。您可以使用 Vertex AI 微調大型語言模型,讓系統以特定樣式產生合適的摘要標題,並根據新聞頻道的規範自訂標題。
在本程式碼研究室中,您將執行下列操作:
- 使用
BBC FULLTEXT DATA
(由 BigQuery 公開資料集bigquery-public-data.bbc_news.fulltext
提供)。 - 將 LLM (
text-bison@002
) 微調為名為「bbc-news-summary-tuned
」的新微調模型,並將結果與基礎模型的回應進行比較。您可以在存放區中找到本程式碼研究室的 JSONL 範例檔案。您可以將檔案上傳至 Cloud Storage 值區,然後執行下列精細調整步驟: - 準備資料:請先準備新聞文章和相應標題的資料集,例如範例程式碼中使用的 BBC 新聞資料集。
- 微調預先訓練模型:選擇「
text-bison@002
」等基礎模型,然後使用 Vertex AI SDK for Python 搭配新聞資料微調模型。 - 評估結果:比較精修模型和基礎模型的成效,瞭解標題產生品質的改善情形。
- 部署及使用模型:透過 API 端點提供經過微調的模型,並開始自動產生新文章的標題。
2. 事前準備
- 在 Google Cloud 控制台的專案選取器頁面中,選取或建立 Google Cloud 專案。
- 請確認 Google Cloud 專案已啟用計費功能。瞭解如何檢查專案是否已啟用計費功能。
- 開啟 Colab Notebook,並登入與目前使用中的 Google Cloud 帳戶相同的帳戶。
3. 微調大型語言模型
本程式碼實驗室會使用 Python 適用的 Vertex AI SDK 微調模型。您也可以使用其他選項進行微調,包括 HTTP、CURL 指令、Java SDK 和控制台。
您可以透過 5 個步驟,精細調整及評估自訂回應模型。您可以參考 存放區中的 llm_fine_tuning_supervised.ipynb 檔案,查看完整程式碼。
4. 步驟 1:安裝及匯入依附元件
!pip install google-cloud-aiplatform
!pip install --user datasets
!pip install --user google-cloud-pipeline-components
請按照 存放區中的 .ipynb 檔案所示,完成其餘步驟。請務必將 PROJECT_ID 和 BUCKET_NAME 替換為您的憑證。
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
warnings.filterwarnings('ignore')
import vertexai
vertexai.init(project=PROJECT_ID, location=REGION)
import kfp
import sys
import uuid
import json
import pandas as pd
from google.auth import default
from datasets import load_dataset
from google.cloud import aiplatform
from vertexai.preview.language_models import TextGenerationModel, EvaluationTextSummarizationSpec
5. 步驟 2:準備及載入訓練資料
將 YOUR_BUCKET 替換為您的值區,然後將範例 TRAIN.jsonl 訓練資料檔案上傳至該值區。上述連結中提供的範例資料已針對此用途進行配置。
json_url = 'https://storage.googleapis.com/YOUR_BUCKET/TRAIN.jsonl'
df = pd.read_json(json_url, lines=True)
print (df)
這個步驟應會產生以下結果:
6. 步驟 3:微調大型語言模型
您可以在此時調整任何大型語言模型 (視支援情況而定)。不過,在這個程式碼片段中,我們會使用在前一個步驟載入的訓練資料資料框架,調整預先訓練的模型「text-bison@002」:
model_display_name = 'bbc-finetuned-model' # @param {type:"string"}
tuned_model = TextGenerationModel.from_pretrained("text-bison@002")
tuned_model.tune_model(
training_data=df,
train_steps=100,
tuning_job_location="europe-west4",
tuned_model_location="europe-west4",
)
這項作業需要幾小時才能完成。您可以使用結果中的管道工作連結,追蹤微調的進度。
7. 步驟 4:使用新的微調模型進行預測
精細調整工作完成後,您就能使用新模型進行預測。如要使用新微調模型進行預測,請按照下列步驟操作:
response = tuned_model.predict("Summarize this text to generate a title: \n Ever noticed how plane seats appear to be getting smaller and smaller? With increasing numbers of people taking to the skies, some experts are questioning if having such packed out planes is putting passengers at risk. They say that the shrinking space on aeroplanes is not only uncomfortable it it's putting our health and safety in danger. More than squabbling over the arm rest, shrinking space on planes putting our health and safety in danger? This week, a U.S consumer advisory group set up by the Department of Transportation said at a public hearing that while the government is happy to set standards for animals flying on planes, it doesn't stipulate a minimum amount of space for humans.")
print(response.text)
畫面上應會顯示下列結果:
如要使用基礎模型 (text-bison@002
) 進行預測並進行比較,請執行下列指令:
base_model = TextGenerationModel.from_pretrained("text-bison@002")
response = base_model.predict("Summarize this text to generate a title: \n Ever noticed how plane seats appear to be getting smaller and smaller? With increasing numbers of people taking to the skies, some experts are questioning if having such packed out planes is putting passengers at risk. They say that the shrinking space on aeroplanes is not only uncomfortable it it's putting our health and safety in danger. More than squabbling over the arm rest, shrinking space on planes putting our health and safety in danger? This week, a U.S consumer advisory group set up by the Department of Transportation said at a public hearing that while the government is happy to set standards for animals flying on planes, it doesn't stipulate a minimum amount of space for humans.")
print(response.text)
畫面上應會顯示下列結果:
雖然兩個產生的標題看起來都很適當,但第一個標題 (使用經過微調的模型產生) 更符合問題資料集中使用的標題樣式。
載入經過微調的模型
載入剛剛微調的模型可能會比較容易。但請注意,在步驟 3 中,它是在程式碼本身的範圍內叫用,因此仍會在變數 tuned_model 中保留經過調整的模型。不過,如果您想叫用先前調整過的模型,該怎麼做呢?
如要執行這項操作,您可以使用 Vertex AI Model Registry 中已部署的經過微調模型的完整端點網址,在 LLM 上叫用 get_tuned_model() 方法。請注意,在這種情況下,您輸入的是 PROJECT_NUMBER 和 MODEL_NUMBER,而不是各自的 ID。
tuned_model_1 = TextGenerationModel.get_tuned_model("projects/<<PROJECT_NUMBER>>/locations/europe-west4/models/<<MODEL_NUMBER>>")
print(tuned_model_1.predict("YOUR_PROMPT"))
8. 步驟 5:評估新的微調模型
評估是評估產生回覆的品質和關聯性的關鍵要素。這項作業包括檢查生成式語言模型的輸出內容,以判斷其是否連貫、準確,以及是否符合提供的提示。模型評估有助於找出有待改善之處、改善模型效能,並確保產生的文字符合品質和實用性標準。詳情請參閱說明文件。我們現在將瞭解如何取得經過微調的模型評估指標,並與基礎模型進行比較。
- 載入評估用資料集:
json_url = 'https://storage.googleapis.com/YOUR_BUCKET/EVALUATE.jsonl'
df = pd.read_json(json_url, lines=True)
print (df)
- 定義精修模型的文字摘要任務評估規格。
task_spec = EvaluationTextSummarizationSpec(
task_name = "summarization",
ground_truth_data=df
)
這項作業需要幾分鐘才能完成。您可以使用結果中的管道工作連結追蹤進度。完成後,您應該會看到下列評估結果:
評估結果中的 rougeLSum
指標會指定摘要的 ROUGE-L 分數。ROUGE-L 是一種以喚回率為依據的指標,用於評估摘要和參考摘要之間的重疊程度。計算方式為取兩個摘要之間最長的共同子序列 (LCS),然後除以參考摘要的長度。
提供的運算式中 rougeLSum
分數為 0.36600753600753694,表示摘要與參考摘要重疊 36.6%。
如果您對基準模型執行評估步驟,就會發現精修模型的摘要分數「相對」較高。
您可以在建立評估工作時指定的 Cloud Storage 輸出目錄中,找到評估結果。檔案名稱為 evaluation_metrics.json
。對於經過微調的模型,您也可以在 Google Cloud 控制台的 Vertex AI「Model Registry」頁面中查看評估結果。
10. 清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本程式碼研究室所用資源的費用,請按照下列步驟操作:
- 在 Google Cloud 控制台中前往「管理資源」頁面。
- 在專案清單中選取要刪除的專案,然後點按「刪除」。
- 在對話方塊中輸入專案 ID,然後按一下「Shut down」(關閉) 即可刪除專案。
- 或者,您也可以前往 Model Registry,前往「模型部署和測試」分頁標籤,然後取消部署端點並刪除已部署的調校模型。