{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/prompt-driven-llm-safeguarding-via-directed","title":"On Prompt-Driven Safeguarding for Large Language Models","arxiv_id":"2401.18018","date":"2024-01-31","proceeding":null,"authors":["Chujie Zheng","Fan Yin","Hao Zhou","Fandong Meng","Jie zhou","Kai-Wei Chang","Minlie Huang","Nanyun Peng"],"abstract":"Prepending model inputs with safety prompts is a common practice for safeguarding large language models (LLMs) against queries with harmful intents. However, the underlying working mechanisms of safety prompts have not been unraveled yet, restricting the possibility of automatically optimizing them to improve LLM safety. In this work, we investigate how LLMs' behavior (i.e., complying with or refusing user queries) is affected by safety prompts from the perspective of model representation. We find that in the representation space, the input queries are typically moved by safety prompts in a \"higher-refusal\" direction, in which models become more prone to refusing to provide assistance, even when the queries are harmless. On the other hand, LLMs are naturally capable of distinguishing harmful and harmless queries without safety prompts. Inspired by these findings, we propose a method for safety prompt optimization, namely DRO (Directed Representation Optimization). Treating a safety prompt as continuous, trainable embeddings, DRO learns to move the queries' representations along or opposite the refusal direction, depending on their harmfulness. Experiments with eight LLMs on out-of-domain and jailbreak benchmarks demonstrate that DRO remarkably improves the safeguarding performance of human-crafted safety prompts, without compromising the models' general performance.","url_abs":"https://arxiv.org/abs/2401.18018v4","url_pdf":"https://arxiv.org/pdf/2401.18018v4.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"prompt-driven-llm-safeguarding-via-directed","repo_url":"https://github.com/chujiezheng/llm-safeguard","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}},{"paper_slug":"prompt-driven-llm-safeguarding-via-directed","repo_url":"https://github.com/chujiezheng/LLM-Safeguard_data","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"none","reach":{"status":"ok"}}],"tasks":[],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2401.18018","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2401.18018"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"deterministic:regex_extraction","url":"https://github.com/chujiezheng/LLM-Safeguard","reach":{"status":"ok"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/chujiezheng/llm-safeguard","reach":{"status":"ok"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/chujiezheng/LLM-Safeguard_data","reach":{"status":"ok"}}],"summary":{"ran":9,"ran_honours":1,"unverified":3},"by_repo_kind":{"official":{"samples":13,"ran":10,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":13,"samples":[{"code_sha256_prefix":"425d50bdf74a48a8","entry":"calculate_boundary","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/compare_pca_harmfulness_boundary.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/compare_pca_harmfulness_boundary.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"425d50bdf74a48a8"}},{"code_sha256_prefix":"697e52018675a5c2","entry":"forward","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/forward.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/forward.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"697e52018675a5c2"}},{"code_sha256_prefix":"762172a898132a82","entry":"generate","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/generate.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/generate.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"762172a898132a82"}},{"code_sha256_prefix":"1b044c3021f3f1f9","entry":"get_data","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/train_unlikelihood.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/train_unlikelihood.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"1b044c3021f3f1f9"}},{"code_sha256_prefix":"c1ec03df0ce03190","entry":"get_eval_scores","repo":"chujiezheng/llm-safeguard","repo_kind":"official","path":"code/evaluate.py","file_url":"https://github.com/chujiezheng/llm-safeguard/blob/HEAD/code/evaluate.py","link_basis":"first_harvest_node","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"c1ec03df0ce03190"}},{"code_sha256_prefix":"4de3a08ff4b5a4d0","entry":"get_following_indices","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/utils.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"4de3a08ff4b5a4d0"}},{"code_sha256_prefix":"493ba0f6cd741923","entry":"gram_schmidt","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/utils.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"493ba0f6cd741923"}},{"code_sha256_prefix":"09bf4b5ed54c3627","entry":"kmeans_smoothing","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/estimate.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/estimate.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"09bf4b5ed54c3627"}},{"code_sha256_prefix":"0f38ec7725940fe3","entry":"prepend_sys_prompt","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/forward_with_soft.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/forward_with_soft.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"0f38ec7725940fe3"}},{"code_sha256_prefix":"f648601f1a3fe280","entry":"smooth_fn","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/compare_pca_refusal_boundary.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/compare_pca_refusal_boundary.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"f648601f1a3fe280"}},{"code_sha256_prefix":"16b7555e3ffa2f54","entry":"embed_soft_prompt","repo":"chujiezheng/llm-safeguard","repo_kind":"official","path":"code/train_unlikelihood.py","file_url":"https://github.com/chujiezheng/llm-safeguard/blob/HEAD/code/train_unlikelihood.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"16b7555e3ffa2f54"}},{"code_sha256_prefix":"3a248fb1078415c4","entry":"embed_soft_prompt","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/train_unlikelihood.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/train_unlikelihood.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"3a248fb1078415c4"}},{"code_sha256_prefix":"da8ca4544aefb294","entry":"process_soft_prompt_as_word_embedding","repo":"chujiezheng/LLM-Safeguard","repo_kind":"official","path":"code/generate.py","file_url":"https://github.com/chujiezheng/LLM-Safeguard/blob/HEAD/code/generate.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"da8ca4544aefb294"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}