Self-Instruct构造Prompt的例子

  1. 人工构造一批Prompt做种子。(Starting with a small seed set of human-written tasks)
  2. 每次把一些种子+后来生成的Prompt,放到Input里做few-shot examples,用LLM生成更多的Prompt;(Using the LLM to generate new instructions based on the seed tasks)
  3. 过滤掉质量太差的,修正能要的;(Filtering and refining the generated instructions)
  4. 把生成的所有Prompt,输入LLM得到输出结果;(Creating input-output instances for the new instructions)
  5. Input+Output,做LLM的训练样本(Using the generated dataset to fine-tune the 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:\n\n"
        prompt += "\n".join(sampled_tasks)
        prompt += "\n\nNew 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来做裁判)

目的:确保质量和多样性;

  • Filter out instructions that are too short or too long
  • Filter out instructions containing keywords unsuitable for language models (e.g. "image", "graph", "file", "plot")
  • Filter out instructions starting with punctuation
  • Filter out instructions starting with non-English characters
  • Filter out instructions that have high ROUGE-L similarity (above 0.7) with any existing instruction in the task pool

相关推荐

  1. Self-Instruct构造Prompt例子

    2024-07-09 21:18:03       22 阅读
  2. ChatGPT 基本用法!ChatGPT4prompt使用例子

    2024-07-09 21:18:03       59 阅读
  3. js构造模式解释和例子和优缺点

    2024-07-09 21:18:03       26 阅读
  4. Pytorch中self.parameters()

    2024-07-09 21:18:03       30 阅读
  5. Pytest 例子

    2024-07-09 21:18:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:18:03       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:18:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:18:03       43 阅读
  4. Python语言-面向对象

    2024-07-09 21:18:03       54 阅读

热门阅读

  1. Oracle-查询表空间使用率很慢

    2024-07-09 21:18:03       22 阅读
  2. git reset HEAD^1

    2024-07-09 21:18:03       17 阅读
  3. 数据的统计探针:SKlearn中的统计分析方法

    2024-07-09 21:18:03       20 阅读
  4. 数据的完美贴合:SKlearn中的数据拟合方法全解

    2024-07-09 21:18:03       21 阅读
  5. Python基础学习笔记(十二)——字典

    2024-07-09 21:18:03       24 阅读
  6. LeetCode 205. 同构字符串

    2024-07-09 21:18:03       19 阅读
  7. GNU/Linux - 什么是loopback设备

    2024-07-09 21:18:03       23 阅读
  8. LeetCode 290. 单词规律

    2024-07-09 21:18:03       19 阅读
  9. Linux应用开发-第四章Linux的多进程开发(1)

    2024-07-09 21:18:03       23 阅读
  10. C#中的类

    2024-07-09 21:18:03       27 阅读
  11. Linux安全加固:防火墙规则与SELinux策略

    2024-07-09 21:18:03       18 阅读