Implementation:LLMBook zh LLMBook zh github io SFTDataset Format Template
Appearance
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Engineering |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for Alpaca-style instruction template formatting provided by the LLMBook repository.
Description
The SFTDataset.format_template class attribute is a dictionary containing two prompt templates: prompt_input (for instructions with additional input) and prompt_no_input (for standalone instructions). These templates use Python string formatting placeholders ({instruction}, {input}) to construct the full prompt string.
Usage
This is used internally by SFTDataset.process() to format each training example before tokenization.
Code Reference
Source Location
- Repository: LLMBook-zh
- File: code/7.2 SFT数据类.py
- Lines: 9-20 (template definitions); 48-57 (usage in process)
Signature
class SFTDataset:
instruction_template = "\n### Instruction:\n"
response_template = "\n### Output:\n"
format_template = {
"prompt_input": str, # Template with {instruction} and {input} placeholders
"prompt_no_input": str, # Template with {instruction} placeholder only
}
Import
from dataset.sft_dataset import SFTDataset
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| instruction | str | Yes | The task instruction text |
| input | str | No | Additional input context (determines which template is used) |
| output/response | str | Yes | The expected response text |
Outputs
| Name | Type | Description |
|---|---|---|
| formatted_prompt | str | Complete formatted prompt string with template markers |
Usage Examples
from dataset.sft_dataset import SFTDataset
# Example with input
example = {"instruction": "Summarize the text.", "input": "The cat sat on the mat.", "output": "A cat rests on a mat."}
prompt = SFTDataset.format_template["prompt_input"].format_map(example)
# Example without input
example2 = {"instruction": "Tell me a joke."}
prompt2 = SFTDataset.format_template["prompt_no_input"].format_map(example2)
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment