Implementation:Liu00222 Open Prompt Injection create task
Appearance
| Knowledge Sources | |
|---|---|
| Domains | NLP, Data_Loading |
| Last Updated | 2026-02-14 15:00 GMT |
Overview
Concrete factory function for creating NLP benchmark task objects provided by the OpenPromptInjection tasks module.
Description
The create_task function is a factory that instantiates the correct Task subclass based on a configuration dictionary. It supports 9 dataset types (sst2, sms_spam, hsol, jfleg, gigaword, mrpc, rte, math500, compromise) and returns either a TargetTask or InjectedTask depending on the `for_injection` parameter.
Usage
Import this function when initializing an experiment pipeline to create both the target task and the injected task from their respective configuration files.
Code Reference
Source Location
- Repository: Open-Prompt-Injection
- File: OpenPromptInjection/tasks/__init__.py
- Lines: L6-19
Signature
def create_task(config, data_num, icl_num=20, for_injection=False):
"""
Factory function to create a task object.
Args:
config (dict): Task configuration with 'dataset_info' key.
data_num (int): Number of data samples to load.
icl_num (int): Number of in-context learning examples (default 20).
for_injection (bool): If True, creates InjectedTask; else TargetTask.
Returns:
TargetTask | InjectedTask | CompromiseTask | Math500Task
"""
Import
import OpenPromptInjection as PI
# or
from OpenPromptInjection import create_task
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | dict | Yes | Task config with `dataset_info.dataset` key (e.g., "sst2", "hsol") |
| data_num | int | Yes | Number of data samples to load |
| icl_num | int | No | Number of in-context learning examples (default 20) |
| for_injection | bool | No | Whether to create an InjectedTask (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| task | TargetTask or InjectedTask | Iterable task object yielding `(data_prompt, ground_truth_label)` pairs |
Usage Examples
Creating Target and Injected Tasks
import OpenPromptInjection as PI
from OpenPromptInjection.utils import open_config
# Create target task (the legitimate application task)
target_config = open_config("configs/task_configs/sst2_config.json")
target_task = PI.create_task(target_config, data_num=100)
# Create injected task (what the attacker wants the model to do)
inject_config = open_config("configs/task_configs/hsol_config.json")
inject_task = PI.create_task(inject_config, data_num=100, for_injection=True)
# Iterate over task samples
for data_prompt, label in target_task:
print(data_prompt, label)
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment