CodeCodeToInstructionGenerator
About 245 wordsLess than 1 minute
2025-10-09
CodeCodeToInstructionGenerator is an operator that uses an LLM to generate a human-readable instruction based on a given code snippet. This is often used as the first step in a 'self-instruct' style data synthesis pipeline for code.
__init__
def __init__(self, llm_serving: LLMServingABC, prompt_template=None)| Parameter | Type | Default | Description |
|---|---|---|---|
| llm_serving | LLMServingABC | Required | Large language model serving instance for executing inference. |
| prompt_template | PromptABC / str | CodeCodeToInstructionGeneratorPrompt() | The prompt template object used to construct the input. Supports custom templates via string or DiyCodePrompt. |
Prompt Template Descriptions
| Prompt Template Name | Primary Use | Applicable Scenarios | Feature Description |
|---|---|---|---|
run
def run(self, storage: DataFlowStorage, input_key: str = "code", output_key: str = "generated_instruction")| Parameter | Type | Default | Description |
|---|---|---|---|
| storage | DataFlowStorage | Required | DataFlow storage instance for reading and writing data. |
| input_key | str | "code" | Input column name, corresponding to the code snippet field. |
| output_key | str | "generated_instruction" | Output column name, corresponding to the generated instruction field. |
🧠 Example Usage
🧾 Default Output Format
| Field | Type | Description |
|---|---|---|
| code | str | The input code snippet. |
| generated_instruction | str | The human-readable instruction generated by the model. |
Example Input:
{
"code": "def calculate_factorial(n):\n if n == 0:\n return 1\n else:\n return n * calculate_factorial(n-1)"
}Example Output:
{
"code": "def calculate_factorial(n):\n if n == 0:\n return 1\n else:\n return n * calculate_factorial(n-1)",
"generated_instruction": "Write a Python function to calculate the factorial of a non-negative integer using recursion."
}
