Improving Large Language Model Safety with Contrastive Representation Learning

Fuente: arXiv
Saved in:
Bibliographic Details
Main Authors: Simko, Samuel, Sachan, Mrinmaya, Schölkopf, Bernhard, Jin, Zhijing
Format: Preprint
Published: 2025
Subjects:
Online Access:
Tags: Add Tag
No Tags, Be the first to tag this record!
_version_ 1866908735969951744
author Simko, Samuel
Sachan, Mrinmaya
Schölkopf, Bernhard
Jin, Zhijing
author_facet Simko, Samuel
Sachan, Mrinmaya
Schölkopf, Bernhard
Jin, Zhijing
contents Large Language Models (LLMs) are powerful tools with profound societal impacts, yet their ability to generate responses to diverse and uncontrolled inputs leaves them vulnerable to adversarial attacks. While existing defenses often struggle to generalize across varying attack types, recent advancements in representation engineering offer promising alternatives. In this work, we propose a defense framework that formulates model defense as a contrastive representation learning (CRL) problem. Our method finetunes a model using a triplet-based loss combined with adversarial hard negative mining to encourage separation between benign and harmful representations. Our experimental results across multiple models demonstrate that our approach outperforms prior representation engineering-based defenses, improving robustness against both input-level and embedding-space attacks without compromising standard performance. Our code is available at https://github.com/samuelsimko/crl-llm-defense
format Preprint
id arxiv_https___arxiv_org_abs_2506_11938
institution arXiv
publishDate 2025
record_format arxiv
spellingShingle Improving Large Language Model Safety with Contrastive Representation Learning
Simko, Samuel
Sachan, Mrinmaya
Schölkopf, Bernhard
Jin, Zhijing
Computation and Language
Artificial Intelligence
Machine Learning
Large Language Models (LLMs) are powerful tools with profound societal impacts, yet their ability to generate responses to diverse and uncontrolled inputs leaves them vulnerable to adversarial attacks. While existing defenses often struggle to generalize across varying attack types, recent advancements in representation engineering offer promising alternatives. In this work, we propose a defense framework that formulates model defense as a contrastive representation learning (CRL) problem. Our method finetunes a model using a triplet-based loss combined with adversarial hard negative mining to encourage separation between benign and harmful representations. Our experimental results across multiple models demonstrate that our approach outperforms prior representation engineering-based defenses, improving robustness against both input-level and embedding-space attacks without compromising standard performance. Our code is available at https://github.com/samuelsimko/crl-llm-defense
title Improving Large Language Model Safety with Contrastive Representation Learning
topic Computation and Language
Artificial Intelligence
Machine Learning
url https://arxiv.org/abs/2506.11938