what is the best way to defend against prompt injection when using LLMs
- User
what is the best way to defend against prompt injection when using LLMs
- Assistant
There are a few key strategies for defending against prompt injection attacks when using large language models (LLMs):
-
Input validation and sanitization: Carefully validate and sanitize any user input before incorporating it into prompts. Remove or escape any potentially malicious characters or sequences.
-
Avoid using raw user input in prompts: Instead of directly inserting user input into prompts, use placeholders or indirect references, and keep the user-supplied content separate from the prompt itself.
-
Use prompt templates: Define safe, pre-approved prompt templates with clear boundaries and placeholders for user input. Stick to using these templates rather than dynamically generating prompts.
-
Limit prompt context: Provide only the necessary context in prompts, and avoid including sensitive information or broad instructions that could be exploited.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.