準備訓練資料集以進行蒸餾 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

準備訓練資料集以進行蒸餾

在開始模型自訂任務之前,您需要至少準備訓練資料集。若要為自訂模型準備輸入資料集,您可以建立 .jsonl 檔案,其中每一行都是與記錄對應的 JSON 物件。您建立的檔案必須符合模型蒸餾和您選擇的模型格式。其中的記錄也必須符合大小要求。

提供輸入資料作為提示。Amazon Bedrock 會使用輸入資料從教師模型產生回應,並使用這些產生的回應來微調學生模型。如需詳細資訊,以便了解 Amazon Bedrock 使用的輸入,以及選擇最適合使用案例的選項,請參閱Amazon Bedrock 模型蒸餾功能如何運作。準備輸入資料集有幾個選項。

注意

Amazon Nova 模型對蒸餾有不同的要求。如需詳細資訊,請參閱 Amazon Nova 模型蒸餾

Amazon Bedrock 模型蒸餾功能支援的模型和區域 中列出的模型僅支援 text-to-text 模式。

在模型蒸餾期間,Amazon Bedrock 會產生合成資料集,用於針對特定使用案例微調學生模型。如需詳細資訊,請參閱Amazon Bedrock 模型蒸餾功能如何運作

您可以針對所需的使用案例格式化輸入提示,以最佳化合成資料產生程序。例如,如果蒸餾模型的使用案例是檢索增強生成 (RAG),您會以不同於您希望模型專注於代理程式使用案例的方式格式化提示。

以下範例說明如何格式化 RAG 或代理程式使用案例的輸入提示。

RAG prompt example
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": "You are a financial analyst charged with answering questions about 10K and 10Q SEC filings. Given the context below, answer the following question." } ], "messages": [ { "role": "user", "content": [ { "text": "<context>\nDocument 1: Multiple legal actions have been filed against us as a result of the October 29, 2018 accident of Lion Air Flight 610 and the March 10, 2019 accident of Ethiopian Airlines Flight 302.\n</context>\n\n<question>Has Boeing reported any materially important ongoing legal battles from FY2022?</question>" } ] } ] }
Agent prompt example
{ "schemaVersion": "bedrock-conversation-2024", "system": [ { "text": 'You are an expert in composing functions. You are given a question and a set of possible functions. Based on the question, you will need to make one or more function/tool calls to achieve the purpose. Here is a list of functions in JSON format that you can invoke. [ { "name": "lookup_weather", "description: "Lookup weather to a specific location", "parameters": { "type": "dict", "required": [ "city" ], "properties": { "location": { "type": "string", }, "date": { "type": "string", } } } } ]' } ], "messages": [ { "role": "user", "content": [ { "text": "What's the weather tomorrow?" } ] }, { "role": "assistant", "content": [ { "text": "[lookup_weather(location=\"san francisco\", date=\"tomorrow\")]" } ] } ] }

收集提示並以 .jsonl 檔案格式存放這些提示。JSONL 中的每筆記錄都必須使用下列結構。

  • 包含 schemaVersion 欄位,此欄位必須具有值 bedrock-conversion-2024

  • [選用] 包含系統提示,此提示指出向模型指派的角色。

  • messages 欄位中,包含使用者角色,其中包含提供給模型的輸入提示。

  • [選用] 在 messages 欄位中,包含內含所需回應的助理角色。

Anthropic 和 Meta Llama 模型僅支援單回合對話提示,這表示您只能有一個使用者提示。這些 Amazon Nova 模型支援多回合對話,可讓您在單一記錄中提供多個使用者和助理交換。

範例格式

{ "schemaVersion": "bedrock-conversation-2024", "system": [{ "text": "A chat between a curious User and an artificial intelligence Bot. The Bot gives helpful, detailed, and polite answers to the User's questions." }], "messages": [{ "role": "user", "content": [{ "text": "why is the sky blue" }] }, { "role": "assistant", "content": [{ "text": "The sky is blue because molecules in the air scatter blue light from the Sun more than other colors." }] } ] }}

驗證資料集

執行蒸餾任務前,您可以使用 Python 指令碼驗證輸入資料集。

建立蒸餾任務時,您可以讓 Amazon Bedrock 使用 CloudWatch Logs 調用日誌中的現有教師回應作為訓練資料。對於 Amazon Bedrock,調用日誌是模型調用的詳細記錄。

若要使用調用日誌進行模型蒸餾,將模型調用記錄設為開啟、使用其中一個模型調用操作,並確定您已將 Amazon S3 儲存貯體設定為日誌的目的地。您必須先提供 Amazon Bedrock 存取日誌的許可,才能開始模型蒸餾任務。如需設定調用日誌的詳細資訊,請參閱使用 Amazon CloudWatch Logs 監控模型調用

使用此選項時,您可以指定是否希望 Amazon Bedrock 僅使用提示,還是使用調用日誌中的提示-回應對。如果您希望 Amazon Bedrock 僅使用提示,則 Amazon Bedrock 可能會新增專屬資料合成技術,以從教師模型產生多樣化且更高品質的回應。如果您希望 Amazon Bedrock 使用提示-回應對,則 Amazon Bedrock 不會從教師模型重新產生回應。Amazon Bedrock 將直接使用調用日誌中的回應來微調學生模型。

重要

您可以向 Amazon Bedrock 提供最多 15K 個提示或提示-回應對,以微調學生模型。為了確保學生模型經過微調以符合特定需求,我們強烈建議您執行下列動作:

  • 如果您希望 Amazon Bedrock 僅使用提示,請確保在所有模型中至少產生 100 個提示-回應對。

  • 如果您希望 Amazon Bedrock 使用調用日誌中的回應,請確定調用日誌中至少有 100 個從模型產生的提示-回應對,這些內容與您選擇的教師模型完全相符。

您可以選擇使用其中一個模型調用操作,將請求中繼資料新增至調用日誌中的提示-回應對,然後使用這個內容來篩選日誌。Amazon Bedrock 可以使用篩選的日誌來微調學生模型。

若要使用多個請求中繼資料篩選日誌,請使用單一操作布林運算子 AND、OR 或 NOT。您無法合併操作。對於單一請求中繼資料篩選,請使用布林運算子 NOT。

將請求中繼資料新增至調用日誌中的提示和回應,以進行模型蒸餾

模型調用記錄功能會收集所有在 Amazon Bedrock 中使用的調用記錄、模型輸入資料 (提示) 及模型輸出資料 (回應)。如果您已啟用記錄,則只要透過任何 InvokeConverse API 操作與 Amazon Bedrock 基礎模型互動,就可以收集日誌。如果您希望 Amazon Bedrock 使用調用日誌中的提示和相關回應來微調學生模型,則必須讓 Amazon Bedrock 存取這些日誌。使用模型已產生的回應,可以更快地微調學生模型。使用來自調用日誌的回應也能夠提高模型蒸餾的成本效益,但 Amazon Bedrock 的專屬資料合成技術並未納入,這可能會帶來更高效能的蒸餾模型。

使用調用日誌,您可以識別您希望 Amazon Bedrock 用於模型蒸餾的提示-回應對。這些提示-回應對可以用於特定使用案例。若要能夠識別用於蒸餾的提示-回應對,您必須在調用模型或使用 Converse API 操作時,將請求中繼資料字串新增至提示-回應對。請求中繼資料的提供格式為:"priority" : "High"

當您繼續使用 InvokeConverse API 操作與模型互動時,會將互動和請求中繼資料記錄在調用日誌中。以下是來自 InvokeModel 呼叫的調用日誌範例,其中顯示請求中繼資料:

{ "schemaType": "ModelInvocationLog", "schemaVersion": "1.0", "timestamp": "2024-09-11T17:16:18Z", "accountId": "XXXXX", "identity": { "arn": "arn:aws:sts::XXXXXXX:assumed-role/Admin/XXXXXX" }, "region": "us-east-1", "requestId": "7ee514f3-9eff-467c-af75-8fd564ce58e5", "operation": "InvokeModel", "modelId": "meta.llama3-1-405b-instruct-v1:0", "input": { "inputContentType": "application/json", "inputBodyJson": { "prompt": "story of two dogs", "max_tokens_to_sample": 300 }, "inputTokenCount": 13 }, "output": { "outputContentType": "application/json", "outputBodyJson": { "type": "completion", "completion": " Here is a story about two dogs:\n\nRex and Charlie were best friends who lived next door to each other. Every morning, their owners would let them out into the backyard to play. Rex was a golden retriever with long, shaggy fur. He loved to run and chase sticks. Charlie was a corgi with short legs and a big smile. He was happy just sniffing the grass and rolling around. \n\nThe two dogs spent every day playing together. Rex would grab a stick and bring it over to Charlie, wagging his tail excitedly. Charlie would take the other end of the stick in his mouth and they'd have a friendly game of tug-of-war. After tiring themselves out, they'd flop down in the warm sunshine for a nap. \n\nAt lunchtime, their owners would call them in for food. Rex would gobble up his kibble hungrily while Charlie ate his at a more leisurely pace. After lunch, it was right back outside for more playtime. The afternoon sunbeams would light up their fur as they chased each other around the yard. \n\nWhen it started getting late, their owners called them in for the night. Rex and Charlie would head inside, tired but happy after another fun day of play. After slurping up fresh water from their bowls, they'd curl up on their beds, Rex's fluffy golden tail tucked over his nose and little", "stop_reason": "max_tokens", "stop": null }, "outputTokenCount": 300 }, "requestMetadata": { "project": "CustomerService", "intent": "ComplaintResolution", "priority": "High" } }

您可以在啟動模型蒸餾任務時,將調用日誌指定為輸入資料來源。您可以使用 API AWS CLI或 AWS SDK,在 Amazon Bedrock 主控台中啟動模型分割任務。

提供請求中繼資料的需求

請求中繼資料必須符合下列需求:

  • 以 JSON key:value 格式提供。

  • 金鑰和值對必須是最多 256 個字元的字串。

  • 提供最多 16 個鍵/值對。

使用請求中繼資料篩選器

您可以套用篩選條件以請求中繼資料,以便精選出要包含在蒸餾中用於微調學生模型的提示-回應對。例如,您可能只想包含具有「專案」:「CustomerService」和「優先順序」:「高」請求中繼資料的那些內容。

若要使用多個請求中繼資料篩選日誌,請使用單一布林運算子 AND、OR 或 NOT。您無法合併操作。對於單一請求中繼資料篩選,請使用布林運算子 NOT。

您可以指定調用日誌作為輸入資料來源,以及當您開始模型蒸餾任務時,要用來選取提示-回應對的篩選條件。您可以使用 API AWS CLI或 AWS SDK,在 Amazon Bedrock 主控台中啟動模型分割任務。如需詳細資訊,請參閱在 Amazon Bedrock 中提交模型蒸餾任務

驗證資料集

執行蒸餾任務前,您可以使用 Python 指令碼驗證輸入資料集。