技術共有

プロンプトを構築する Self-Instruct の例

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

  1. プロンプトのバッチをシードとして人工的に構築します。 (人間が作成したタスクの小さなシード セットから開始します)
  2. 毎回、シードによって生成されたいくつかのプロンプトを入力に入力して数ショットのサンプルを作成し、LLM を使用してさらにプロンプ​​トを生成します (LLM を使用してシード タスクに基づいて新しい命令を生成します)。
  3. 品質の低いものを除外し、必要な可能性のあるものを修正します (生成された指示をフィルタリングして調整します)。
  4. 生成されたすべてのプロンプトを LLM に入力して出力結果を取得します (新しい命令の入出力インスタンスを作成します)。
  5. 入力出力、LLM のトレーニング サンプルの作成 (生成されたデータセットを使用して LLM を微調整する)

ステップ 2、LLM は以下を生成します。

import random
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load a pre-trained language model
model_name = "bigcode/starcoderbase-1b"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Seed tasks (simplified for demonstration)
seed_tasks = [
    "Write a function to calculate the factorial of a number.",
    "Create a class to represent a bank account.",
    "Implement a binary search algorithm."
]

def generate_instruction(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs, max_new_tokens=50)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

def self_instruct(num_iterations):
    generated_tasks = []
    
    for _ in range(num_iterations):
        # Sample existing tasks
        sampled_tasks = random.sample(seed_tasks   generated_tasks, min(3, len(seed_tasks)   len(generated_tasks)))
        
        # Create a prompt for generating new instructions
        prompt = "Generate a new programming task based on these examples:nn"
        prompt  = "n".join(sampled_tasks)
        prompt  = "nnNew task:"
        
        # Generate a new instruction
        new_task = generate_instruction(prompt)
        
        # In practice, you would filter and refine the generated task here
        
        generated_tasks.append(new_task)
    
    return generated_tasks

# Run Self-Instruct
new_tasks = self_instruct(5)
for i, task in enumerate(new_tasks, 1):
    print(f"Task {i}: {task}")

ステップ 3 フィルタ:

いくつかのルールを手動で定義し、あまりにも悪いルールを除外します (LLM を審判として使用することもできます)。

目的: 品質と多様性を確保するため。

  • 短すぎる、または長すぎる指示を除外する
  • 言語モデルに適さないキーワード(例:「画像」、「グラフ」、「ファイル」、「プロット」)を含む命令を除外します。
  • 句読点で始まる指示を除外する
  • 英語以外の文字で始まる指示を除外する
  • タスクプール内の既存の命令とROUGE-Lの類似度が高い(0.7以上)命令を除外する