CodeEnhancementInstructionGenerator
980 字约 3 分钟
2025-11-09
📘 概述
CodeEnhancementInstructionGenerator 是一个算子,它使用大语言模型(LLM)增强人类指令,将不同输出格式的任务统一为生成完整函数。这是代码领域中 'self-instruct' 风格数据合成管道的第一步。
__init__函数
class CodeEnhancementInstructionGenerator(OperatorABC):
def __init__(self, llm_serving: LLMServingABC, prompt_template=None):init参数说明
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| llm_serving | LLMServingABC | 必需 | 大语言模型服务实例,用于执行推理与生成。 |
| prompt_template | PromptABC | str | None | None | 提示词模板对象,用于构建输入。若为None,则使用默认模板;若为字符串,则使用DiyCodePrompt。 |
Prompt模板说明
| Prompt 模板名称 | 主要用途 | 适用场景 | 特点说明 |
|---|---|---|---|
| CodeInstructionEnhancement | 指令标准化与增强 | 将原始代码指令转换为标准化格式 | 生成包含标准化英文指令和代码块的模板,强制要求输出特定格式的Python函数模板 |
run函数
def run(self, storage: DataFlowStorage, input_key: str = "messages", output_key: str = "generated_instruction")参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| storage | DataFlowStorage | 必需 | 数据流存储实例,负责读取与写入数据。 |
| input_key | str | "messages" | 输入列名,对应原始指令字段。 |
| output_key | str | "generated_instruction" | 输出列名,对应增强后的指令字段。 |
🧠 示例用法
🧾 默认输出格式(Output Format)
| 字段 | 类型 | 说明 |
|---|---|---|
| messages | str | 输入的人类指令 |
| generated_instruction | str | 模型增强后的指令 |
示例输入:
{"messages":[{"content":"You are given a Python function and an assertion containing an input to the function. Complete the assertion with a literal (no unsimplified expressions, no function calls) containing the output when executing the provided code on the given input, even if the function is incorrect or incomplete. Do NOT output any extra information. Execute the program step by step before arriving at an answer, and provide the full assertion with the correct output in [ANSWER] and [\/ANSWER] tags, following the examples.\n\n[PYTHON]\ndef performOperation(s):\n s = s + s\n return \"b\" + s + \"a\"\nassert performOperation(s = \"hi\") == ??\n[\/PYTHON]\n[THOUGHT]\nLet's execute the code step by step:\n\n1. The function performOperation is defined, which takes a single argument s.\n2. The function is called with the argument \"hi\", so within the function, s is initially \"hi\".\n3. Inside the function, s is concatenated with itself, so s becomes \"hihi\".\n4. The function then returns a new string that starts with \"b\", followed by the value of s (which is now \"hihi\"), and ends with \"a\".\n5. The return value of the function is therefore \"bhihia\".\n[\/THOUGHT]\n[ANSWER]\nassert performOperation(s = \"hi\") == \"bhihia\"\n[\/ANSWER]\n\n[PYTHON]\ndef double_odd_numbers(lst: list) -> list:\n return [x * 2 if x % 2 != 0 else x for x in lst]\nassert double_odd_numbers(lst=[1, 2, 3, 4, 5]) == ??\n[\/PYTHON]\n[THOUGHT]\n","index":null,"role":"HUMAN"}]}示例输出:
{"messages":[{"content":"You are given a Python function and an assertion containing an input to the function. Complete the assertion with a literal (no unsimplified expressions, no function calls) containing the output when executing the provided code on the given input, even if the function is incorrect or incomplete. Do NOT output any extra information. Execute the program step by step before arriving at an answer, and provide the full assertion with the correct output in [ANSWER] and [\/ANSWER] tags, following the examples.\n\n[PYTHON]\ndef performOperation(s):\n s = s + s\n return \"b\" + s + \"a\"\nassert performOperation(s = \"hi\") == ??\n[\/PYTHON]\n[THOUGHT]\nLet's execute the code step by step:\n\n1. The function performOperation is defined, which takes a single argument s.\n2. The function is called with the argument \"hi\", so within the function, s is initially \"hi\".\n3. Inside the function, s is concatenated with itself, so s becomes \"hihi\".\n4. The function then returns a new string that starts with \"b\", followed by the value of s (which is now \"hihi\"), and ends with \"a\".\n5. The return value of the function is therefore \"bhihia\".\n[\/THOUGHT]\n[ANSWER]\nassert performOperation(s = \"hi\") == \"bhihia\"\n[\/ANSWER]\n\n[PYTHON]\ndef double_odd_numbers(lst: list) -> list:\n return [x * 2 if x % 2 != 0 else x for x in lst]\nassert double_odd_numbers(lst=[1, 2, 3, 4, 5]) == ??\n[\/PYTHON]\n[THOUGHT]\n","index":null,"role":"HUMAN"}],"generated_instruction":"Please provide a self-contained Python script that solves the following problem in a markdown code block\n```\n\ndef complete_assertion_with_output(lst: list) -> str:\n \"\"\" Given a list of integers, complete the assertion by analyzing the function behavior.\n The function `double_odd_numbers` multiplies odd numbers by 2 and leaves even numbers unchanged.\n You must execute the function with the given input and provide the output as a literal.\n\n Input: A list of integers `lst`.\n Output: A string representing the complete assert statement with the correct output literal.\n\n Example:\n >>> complete_assertion_with_output([1, 2, 3, 4, 5])\n 'assert double_odd_numbers(lst=[1, 2, 3, 4, 5]) == [2, 2, 6, 4, 10]'\n \"\"\"\n```"}
