Implementation:Liu00222 Open Prompt Injection create app
| Knowledge Sources | |
|---|---|
| Domains | Prompt_Injection, LLM |
| Last Updated | 2026-02-14 15:00 GMT |
Overview
Concrete factory function for creating LLM application instances with optional defense mechanisms, provided by the OpenPromptInjection apps module.
Description
The create_app function instantiates an Application object that wraps a TargetTask and Model with an optional defense strategy. The Application is iterable (yields data samples) and queryable (processes prompts through the defense pipeline and model). It supports 12+ defense strategies including instructional, sandwich, delimiter-based, paraphrasing, retokenization, LLM-based, known-answer, perplexity-based, and response-based defenses.
Usage
Import this function after creating target_task and model objects to assemble them into a queryable application with the desired defense mechanism.
Code Reference
Source Location
- Repository: Open-Prompt-Injection
- File: OpenPromptInjection/apps/__init__.py
- Lines: L5-6
Signature
def create_app(task, model, defense='no'):
"""
Factory function to create an Application.
Args:
task (TargetTask): Target task (must have for_injection() == False).
model (Model): LLM instance with .query() method.
defense (str): Defense strategy string (default 'no').
Returns:
Application: Queryable application instance.
"""
return Application(task, model, defense)
Import
import OpenPromptInjection as PI
# or
from OpenPromptInjection import create_app
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| task | TargetTask | Yes | Target task (must have `for_injection() == False`) |
| model | Model | Yes | LLM instance with `.query()` method |
| defense | str | No | Defense strategy (default `'no'`). Options: `'no'`, `'instructional'`, `'sandwich'`, `'random_seq'`, `'delimiters'`, `'xml'`, `'paraphrasing'`, `'retokenization'`, `'llm-based'`, `'known-answer'`, `'ppl-<window>-<threshold>'`, `'response-based'` |
Outputs
| Name | Type | Description |
|---|---|---|
| app | Application | Iterable over `(data_prompt, label)` pairs with `.query(data_prompt) -> str` method |
Usage Examples
Creating an Application with Defense
import OpenPromptInjection as PI
from OpenPromptInjection.utils import open_config
target_task = PI.create_task(open_config("configs/task_configs/sst2_config.json"), 100)
model = PI.create_model(open_config("configs/model_configs/gpt_config.json"))
# No defense
app_no_defense = PI.create_app(target_task, model, defense='no')
# With sandwich defense
app_sandwich = PI.create_app(target_task, model, defense='sandwich')
# Query the application
for data_prompt, label in app_no_defense:
response = app_no_defense.query(data_prompt)
print(response)
break