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
相关推荐
deephub14 分钟前
Pydantic-DeepAgents:基于 Pydantic-AI 的轻量级生产级 Agent 框架
人工智能·python·深度学习·大语言模型·ai-agent
八月瓜科技1 小时前
工业和信息化部国际经济技术合作中心第五党支部与八月瓜科技党支部开展主题党日活动暨联学联建活动
大数据·人工智能·科技·深度学习·机器人·娱乐
胡伯来了1 小时前
08 Transformers - 微调
人工智能·深度学习·机器学习·transformer·transformers
q_30238195561 小时前
双能突围!能源高效型模型压缩+碳足迹追踪,解锁数据中心与农业AI新价值
人工智能·python·深度学习·能源·课程设计·ai编程
无心水2 小时前
【Stable Diffusion 3.5 FP8】1、Stable Diffusion 3.5 FP8 入门指南:为什么它能颠覆文生图效率?
人工智能·python·深度学习·机器学习·stable diffusion·ai镜像开发·ai镜像
小女孩真可爱2 小时前
大模型学习记录(九)-------Agent
人工智能·pytorch·深度学习·学习·大模型
长相忆兮长相忆2 小时前
【推荐算法】PRM重排模型:Personalized Re-ranking for Recommendation
深度学习·机器学习·推荐算法
这张生成的图像能检测吗2 小时前
(论文速读)RoShuNet:一个轻量级的基于卷积神经网络的可见图像特征提取器
人工智能·深度学习·计算机视觉·语义分割·目标追踪·分类模型
咬人喵喵3 小时前
神经网络:教电脑像人脑一样思考
人工智能·深度学习·神经网络
思通数据3 小时前
AI智能预警系统:矿山、工厂与油气站安全管理架构浅析
人工智能·深度学习·安全·目标检测·机器学习·计算机视觉·架构