LoginSignup
20
14

ローカルLLMでAlpacaデータのようなインストラクションデータを自動生成してみた

Last updated at Posted at 2024-03-26

はじめに

OpenAIのtext-davinchi-003を利用して自動生成されたインストラクションデータであるAlpacaデータがStanford大学から公開されてから約1年が経過しました。

このデータはインストラクションデータの生成にインパクトを与えましたが、OpenAIの利用規約の関係から商用利用ができないという問題を抱えていました。

当時は同じようなデータをローカルLLMで作成することは困難でした。しかし、2024年現在のローカルLLMならtext-davinchi-003でできていたこともできそうだと思い挑戦してみました。

生成したデータと生成に利用したコードは以下で公開しています。

2024/04/01追記
データの品質は低いですが、Rakuten/RakutenAI-7B-chatを使用して生成した日本語インストラクションデータを公開しました。

Alpacaデータとは

Alpacaデータとは、175個の人手で作成したインストラクションデータを、text-davinchi-003にfew shotで入力し自動的に作成されたインストラクションデータのことです。

LLMによる生成では似たようなデータが生成されやすいため、新たに生成したデータとseed task+これまで生成したデータのROUGEを計算し、しきい値以上のデータを無視するという対策も行っています。

詳しくは以下をご覧ください。

データ生成

モデル

データの生成にはgoogleのgemma-7b-itを使用します。

英語に強いという理由と体感的にインストラクション通りに動いてくれる気がするという理由でgemma-7b-itを選びました。

プロンプト

プロンプトはオリジナルのプロンプトをgemmaの入力に合わせて一部変更しました(ほぼオリジナルのままです)。

また、Few-shotは3〜5shotを使用しており、seed taskの中からランダムで選択しています。

以下は1shotの場合のプロンプト例になります。

prompt
<bos><start_of_turn>user
You are asked to come up with a set of 20 diverse task instructions. These task instructions will be given to a GPT model and we will evaluate the GPT model for completing the instructions.

Here are the requirements:
1. Try not to repeat the verb for each instruction to maximize diversity.
2. The language used for the instruction also should be diverse. For example, you should combine questions with imperative instrucitons.
3. The type of instructions should be diverse. The list should include diverse types of tasks like open-ended generation, classification, editing, etc.
2. A GPT language model should be able to complete the instruction. For example, do not ask the assistant to create any visual or audio output. For another example, do not ask the assistant to wake you up at 5pm or set a reminder because it cannot perform any action.
3. The instructions should be in English.
4. The instructions should be 1 to 2 sentences long. Either an imperative sentence or a question is permitted.
5. You should generate an appropriate input to the instruction. The input field should contain a specific example provided for the instruction. It should involve realistic data and should not contain simple placeholders. The input should provide substantial content to make the instruction challenging but should ideally not exceed 100 words.
6. Not all instructions require input. For example, when a instruction asks about some general information, "what is the highest peak in the world", it is not necssary to provide a specific context. In this case, we simply put "<noinput>" in the input field.
7. The output should be an appropriate response to the instruction and the input. Make sure the output is less than 100 words.
<start_of_turn>model
I see. List of 20 tasks:
###
1. Instruction: What is the relation between the given pairs?
1. Input:
"Night : Day :: Right : Left"
1. Output:
The relation between the given pairs is that they are opposites.
###
2. Instruction:

生成結果

今回は上記のモデルとプロンプトを使用して約13K件のデータを生成しました。本当は52K件を目指したかったのですが、計算資源の関係でデータを生成するのに時間がかかりオリジナルのものと比べるとデータ量が少なくなっています。

以下が生成したインストラクションデータになります。生成例とともに示す日本語は生成結果をDeepLで翻訳したものになります。

出力例1
instruction: Write a short story about a young boy who discovers his family's secret recipe for a delicious cake.
input: ""
output: "Once upon a time, in a small town nestled amidst rolling hills and fragrant meadows, there lived an imaginative young boy named Jack. One sunny afternoon as he sifted through an old trunk, he stumbled upon a handwritten recipe tucked meticulously within the folds of a worn leather binder. As he unfurled the parchment with trembling hands, he felt an overwhelming sensation of anticipation and joy. The recipe, passed down through generations, promised to unlock the secret to a mouthwatering cake that had tantalized taste buds for decades. With a sprinkle of imagination and a sprinkle of love, Jack embarked on a culinary journey to recreate the delectable treat, filled with longing and aspirations. The result was a masterpiece, a testament to the power of family traditions and the transformative power of food."

instruction: おいしいケーキの家族の秘密のレシピを発見した少年についての短い物語を書いてください。.
input: 
output: "昔々、なだらかな丘陵地帯と香り豊かな牧草地に囲まれた小さな町に、ジャックという名の想像力豊かな少年が住んでいた。ある晴れた日の午後、彼は古いトランクを漁っていると、使い古された革のバインダーの折り目に丁寧に挟まれた手書きのレシピを偶然見つけた。震える手で羊皮紙を広げると、彼は圧倒的な期待と喜びを感じた。代々受け継がれてきたそのレシピは、何十年もの間、舌をうならせてきた食欲をそそるケーキの秘密を解き明かすことを約束していた。ジャックは、想像力と愛情をひと振りして、あこがれと願望に満ちたおいしいお菓子を再現する料理の旅に出た。その結果、家族の伝統の力、そして食べ物が持つ変幻自在の力を証明する傑作が誕生した。"
出力例2
instruction: Convert kilometers to miles.
input: 5 kilometers
output: "3.1 miles"

instruction: キロメートルをマイルに変換する。
input: 5キロメートル
output: 3.1マイル
出力例3
instruction: Hypothermically list the mathematical operations in order of complexity.
input: 
output: Sure. Here's the order of complexity:\n1. Multiply\n2. Divide\n3. Addition\n4. Subtraction

instruction: 数学的操作を複雑な順に列挙する。
input: 
output: もちろん、複雑さの順番はこうだ。乗算。除算。足し算。引き算

生成結果を見ているといい感じのインストラクションデータができていそうです。

ただ、物語を書いてくださいのようなデータの数は結構多いという問題もありますので、今後生成データについては細かい分析を行う必要があると感じました。

評価

生成した結果を使用してモデルを学習し、オリジナルのAlpacaデータセットを用いて学習させたStanford AlpacaとAlpaca-LoRAと生成結果を比較して簡単な定性評価を行います。

学習に使用するモデルやパラメータはAlpaca-LoRAに合わせました。

結果

今回生成したデータで学習させたモデル名はGemma-Alpaca-LoRAとします。
日本語への翻訳はDeepLを使用しました。

出力1

Instruction: Tell me about alpacas.
アルパカについて教えてください。

Gemma Alpaca-LoRA: Alpacas are South American camelids. They are known for their soft, luxurious fleece. Alpacas are raised for their fleece, which is used to make clothing and blankets. Alpacas are also raised for their meat, which is considered a delicacy in some parts of the world. Alpacas are social animals that live in herds. They are known for their gentle nature and intelligence. Alpacas are also known for their ability to learn tricks and perform tasks. They are often used as therapy animals. Alpacas are native to South America, but they have been introduced to other parts of the world, including North America and Australia. Alpacas are considered to be a threatened species due to habitat loss and overgrazing. They are protected by law in many countries.
アルパカは南米のラクダ科の動物である。柔らかく贅沢なフリースで知られている。アルパカはそのフリースを衣類や毛布にするために飼育されている。また、アルパカは食肉用としても飼育されており、世界の一部では珍味とされている。アルパカは群れで生活する社会的動物である。穏やかな性格と知性で知られている。アルパカは芸や仕事を覚える能力でも知られている。アルパカはしばしばセラピー動物として使われる。アルパカは南アメリカ原産だが、北米やオーストラリアなど、世界の他の地域にも導入されている。アルパカは生息地の損失と過放牧により、絶滅危惧種とみなされている。多くの国では法律で保護されている。

Stanford Alpaca: Alpacas are small, fluffy animals related to camels and llamas. They are native to Peru and Bolivia, and were first domesticated around 5,000 years ago. They are kept mainly for their fine, soft fleece, which is used to make knitwear and other garments. Alpacas are herd animals and live in small family groups, led by an older male. They are highly social animals and can form strong bonds with their owners.
アルパカはラクダやリャマに似た、小さくてふわふわした動物だ。ペルーとボリビア原産で、約5000年前に初めて家畜化された。アルパカは主に、ニットなどの衣料品に使われる上質で柔らかい毛皮のために飼われている。アルパカは群れで生活する動物で、年配のオスを中心とした小さな家族グループで暮らす。社会性の高い動物で、飼い主と強い絆で結ばれている。

Alpaca-LoRA: Alpacas are members of the camelid family and are native to the Andes Mountains of South America. They are known for their soft, luxurious fleece, which is used to make clothing, blankets, and other items. Alpacas are herbivores and graze on grasses and other plants. They are social animals and live in herds of up to 20 individuals.
アルパカはラクダ科の動物で、南米のアンデス山脈が原産である。柔らかく高級なフリースで知られ、衣料品や毛布などに使用される。アルパカは草食動物で、草やその他の植物を食べる。社会的な動物で、最大20頭の群れで生活する。


出力2

Instruction: List all Canadian provinces in alphabetical order.
カナダの全州をアルファベット順に並べる。

Gemma Alpaca-LoRA: 1. Alberta
2. British Columbia
3. Manitoba
4. New Brunswick
5. Newfoundland and Labrador
6. Northwest Territories
7. Nova Scotia
8. Ontario
9. Prince Edward Island
10. Quebec
11. Saskatchewan
12. Yukon

Stanford Alpaca: Alberta, British Columbia, Manitoba, New Brunswick, Newfoundland and Labrador, Nova Scotia, Ontario, Prince Edward Island, Quebec, Saskatchewan, Northwest Territories, Nunavut, Yukon.

Alpaca-LoRA: Alberta, British Columbia, Manitoba, New Brunswick, Newfoundland and Labrador, Nova Scotia, Ontario, Prince Edward Island, Quebec, Saskatchewan


出力3

Instruction: Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.
1から100までの数字を表示するプログラムを書きなさい。ただし、3の倍数の場合は数字の代わりに'Fizz'と表示し、5の倍数の場合は'Buzz'と表示する。3と5の倍数の場合は'FizzBuzz'と表示せよ。

Gemma Alpaca-LoRA: 1
2
3
4
5
6
7
8
9
10
11
12
13

Stanford Alpaca:

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print('FizzBuzz')
    elif i % 3 == 0:
        print('Fizz')
    elif i % 5 == 0:
        print('Buzz')
    else:
        print(i)

Alpaca-LoRA:

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

出力3のプログラムを作成するタスク以外はオリジナルのAlpacaデータで学習させたモデルと比べても問題なく指示通りの出力がでていました。

プログラムを作成するタスクに関しては生成したデータを確認したところpythonのコードを作成したり、修正するタスクが100件ほどあったのですがデータ量が足りないのか上手く学習できていませんでした。

ただ、個人的にはGemmaでも十分良いデータを生成できたのかと感じています。

最後に

AlpacaのようなインストラクションデータをローカルLLMで作成することからこの1年間のLLMの進歩を感じることができて良かったです。

インストラクションデータに関しては生成に時間はかかるものの少しずつ量を増やしてAlpacaと同じ52K件を目指そうと思います。

また、日本語でも優秀なLLMが色々とでてきているため、近いうちに日本語でのインストラクションデータの生成も試してみたいと思っています。

20
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
14