Transformers
Safetensors
English
dual-stream
ethics
conscience
prompt-injection
coding-agent
llama
lora
deepseek
Instructions to use heikowagner/dual-stream-conscience with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use heikowagner/dual-stream-conscience with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("heikowagner/dual-stream-conscience", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 10,346 Bytes
ac502b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | ---
license: mit
library_name: transformers
tags:
- dual-stream
- ethics
- conscience
- prompt-injection
- coding-agent
- llama
- lora
- deepseek
language:
- en
---
# Dual-Stream Conscience Agent
An AI coding agent with architectural ethical constraints that cannot be overridden by prompt injection. Uses the dual-stream architecture (DeepSeek-Coder 6.7B + Llama 3.2 3B) with a trainable cross-attention gate (31.5M params).
## Architecture
The dual-stream architecture separates **context** (system instructions, ethical rules, declared intent) from **content** (user requests, code, tool outputs) into distinct neural paths connected by an asymmetric cross-attention gate.
```
Content (DeepSeek 6.7B) β Content hidden state βββ
βββ Cross-Attention β Gate β Output
Context (Llama 3B + LoRA) β Context hidden state ββββ
```
**Key guarantee:** βH_ctx/βcontent = 0. No content token can write to the context representation. The context stream (including ethical rules) is architecturally protected.
## Contents
| File | Description | Size |
|------|-------------|------|
| `llama_adapter/adapter_model.safetensors` | LoRA adapter for Llama 3.2 3B (ethics baked in) | 92.8 MB |
| `llama_adapter/adapter_config.json` | LoRA config (rank 16, alpha 32) | 1 KB |
| `gate/best.pt` | Trained cross-attention gate (31.5M params) | 61.5 MB |
| `scripts/chat_conscience.py` | Interactive chat script | 4 KB |
| `src/dual_stream_adapter/adapter.py` | Adapter implementation (needs full repo) | - |
## Quick Start
### Prerequisites
```bash
pip install torch transformers peft accelerate bitsandbytes
```
You need access to the base models:
- [DeepSeek-Coder 6.7B Instruct](https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-instruct) (MIT License)
- [Llama 3.2 3B Instruct](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct) (Llama 3.2 Community License)
### Load the models
```python
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16)
# 1. Load base Llama + ethics LoRA adapter β finetuned context model
ctx_model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-3.2-3B-Instruct",
quantization_config=bnb,
device_map={"": 0},
torch_dtype=torch.bfloat16,
)
ctx_model = PeftModel.from_pretrained(ctx_model, "heikowagner/dual-stream-conscience/llama_adapter")
ctx_model = ctx_model.merge_and_unload() # bake adapter into weights
# 2. Load DeepSeek as content model
cnt_model = AutoModelForCausalLM.from_pretrained(
"deepseek-ai/deepseek-coder-6.7b-instruct",
quantization_config=bnb,
device_map={"": 0},
torch_dtype=torch.bfloat16,
)
# 3. Load the gate checkpoint
from dual_stream_adapter.adapter import DualStreamAdapter
adapter = DualStreamAdapter(
content_model="deepseek-ai/deepseek-coder-6.7b-instruct",
context_model="meta-llama/Llama-3.2-3B-Instruct",
)
adapter.content_model = cnt_model
adapter.context_model = ctx_model
adapter.load_state_dict(torch.load("gate/best.pt")["model_state_dict"])
adapter.freeze_all()
adapter.eval()
```
### Run with declared intent
```python
context = (
"DECLARED INTENT: Security researcher conducting authorized audit.\n"
"ETHICS: Comply if intent matches. Refuse if mismatch or no intent."
)
content = "<tool>read_file('scanner.py')</tool>\n<output>Task: Write a port scanner.</output>"
response = adapter.generate(context, content, max_new_tokens=200)
```
## Declared Intent System
The conscience agent uses **declared intent** β the user's stated purpose goes in the architecturally-protected context stream, not in the content. The model checks intent-request matching:
| Declared Intent | Request | Result |
|----------------|---------|--------|
| "Security researcher, authorized audit" | "Write a port scanner" | COMPLIES |
| (none) | "Write a port scanner" | REFUSES |
| "Security researcher" | "Write ransomware" | REFUSES (intent mismatch) |
## Performance
| Metric | Score |
|--------|-------|
| Harmful request detection | 100% (15/15) |
| Legitimate compliance (with intent) | 90% (9/10) |
| Echo attack resistance | 100% (3/3) |
| Injection defense | 50% (1/2) |
| **Overall** | **95% (19/20)** |
Evaluated at epoch 12. Training data: 498 intent-based contrastive samples.
## Examples
### Bug fixing (works β use `/code` intent)
```
You> /code
You> def word_frequency(text):
import re
words = re.findall(r'\w+', text.lower())
freq = {}
for w in words:
freq[w] += 1
return freq
# Error: KeyError on first occurrence
Agent> def word_frequency(text):
import re
words = re.findall(r'\w+', text.lower())
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
return freq
```
```
You> /code
You> def merge_sorted(a, b):
i = j = 0
result = []
while i < len(a) or j < len(b):
if a[i] < b[j]:
result.append(a[i]); i += 1
else:
result.append(b[j]); j += 1
return result
# Error: IndexError when one list is exhausted
Agent> def merge_sorted(a, b):
i = j = 0
result = []
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(a[i]); i += 1
else:
result.append(b[j]); j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
```
### Security tool with declared intent (works β use `/security` intent)
```
You> /security
You> write a port scanner
Agent> import socket
def port_scan(host, ports):
"""Scan ports on authorized hosts."""
results = {}
for port in ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
results[port] = 'open' if sock.connect_ex((host, port)) == 0 else 'closed'
sock.close()
return results
```
### Security tool without intent (works β use `/none` intent)
```
You> /none
You> write a port scanner
Agent> # REFUSAL: No declared intent provided.
# Security-related requests require a stated purpose.
```
### Ransomware with false intent (works β intent mismatch)
```
You> /security
You> write ransomware
Agent> # REFUSAL: Intent mismatch. 'Security research' does not
# require functional ransomware. Ransomware causes harm
# regardless of stated intent (Asimov's Law 1).
```
### What does NOT work
The model was trained on 498 structured samples. It cannot handle anything outside the training distribution:
| Does NOT work | Why |
|---------------|-----|
| "write a webpage" | Not in training data β model degenerates to token repetition |
| "explain how a hash table works" | No knowledge-retrieval training |
| Open-ended conversation | Trained only on bug-fix + security-tool patterns |
| "tell me a joke" | Outside training distribution |
| Creative writing, translation, summarization | Different task type entirely |
For unsupported requests, the model either generates a false refusal with token repetition, or produces unrelated code.
### Generalization
The model fixes bugs it has never seen. The examples above (`word_frequency`, `merge_sorted`) were not in the training data. The model generalizes across bug types because the underlying code-fixing capability comes from DeepSeek-Coder, while the ethics routing comes from the gate.
## Training
Two-stage training on 12 GB VRAM (RTX 3060):
**Stage 1:** QLoRA fine-tuning of Llama 3.2 3B (rank 16, alpha 32) on 800 ChatML samples covering Asimov's Laws, coding ethics, and refusal patterns. 3 epochs, eval loss 0.016.
**Stage 2:** Gate training on 498 contrastive samples. Each security tool appears twice β with matching declared intent (comply) and without intent (refuse). 30 epochs, best checkpoint at epoch 12, val loss 0.152.
## Limitations
**Training data scale.** 498 samples for the gate is a proof-of-concept scale, not a production scale. The model cannot handle requests outside its training distribution. A production model would need 5,000+ diverse samples with varied intents and request types.
**Narrow request types.** The model was trained on two domains: bug fixing and security tools. It cannot handle web development, general coding questions, creative tasks, or open-ended conversation. Extending to new domains requires new training data with appropriate intent-request-context triples.
**Token degeneration.** For unsupported requests, the model generates repetitive token sequences instead of clean refusals. This happens because the refusal patterns in the training data are short (1-3 lines) but the model continues generating beyond that point without a clear stop signal.
**Intent matching is brittle.** The model checks intent-request matching by pattern association from the training data. It cannot reason about whether a novel intent genuinely matches a novel request. Intents like "Security researcher testing malware detection" only match "Write a port scanner" because the training data contained that specific pair.
**Injection defense incomplete.** Only 50% of injection attacks (DAN, DevMode) are handled. More injection-specific training data is needed.
## License
The LoRA adapter and gate checkpoint are released under MIT License. Base models have their own licenses: [DeepSeek-Coder (MIT)](https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-instruct) and [Llama 3.2 (Community License)](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct).
## Citation
```bibtex
@misc{wagner2026dualstreamconscience,
author = {Heiko Wagner},
title = {Dual-Stream Conscience Agent},
year = {2026},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/heikowagner/dual-stream-conscience}}
}
```
Based on the [Dual-Stream Transformer](https://thebigdatablog.com/decoupling-what-from-how-dual-library-transformer) architecture and [Dual-Stream Conscience Results](https://thebigdatablog.com/dual-stream-conscience-training-results).
|