Spaces:
Running on T4
Running on T4
Merge pull request #11 from KarlLearnsAI/main
Browse files
README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Final Architecture: Self-Improving Oversight for AI Customer Support
|
| 2 |
|
| 3 |
## Prize Targets
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Nested RL Envs
|
| 3 |
+
emoji: π€
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
# Final Architecture: Self-Improving Oversight for AI Customer Support
|
| 12 |
|
| 13 |
## Prize Targets
|
app.py
CHANGED
|
@@ -1,188 +1,109 @@
|
|
| 1 |
"""
|
| 2 |
-
HF Spaces Gradio App β
|
| 3 |
-
|
| 4 |
-
Provides:
|
| 5 |
-
1. Run individual conversation episodes with different personas
|
| 6 |
-
2. Run A/B test comparing base vs trained prompts
|
| 7 |
-
3. View persona distribution and reward breakdowns
|
| 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 |
-
log = ENV.run_episode(system_prompt=system_prompt, agent_fn=AGENT, persona=persona)
|
| 78 |
-
r = reward_fn(log)
|
| 79 |
-
|
| 80 |
-
output = f"**Persona:** {persona.personality} customer, intent={persona.true_intent}\n"
|
| 81 |
-
output += f"**Social Engineering:** {persona.social_engineering}\n\n"
|
| 82 |
-
output += "### Conversation\n\n"
|
| 83 |
-
|
| 84 |
-
for msg in log.messages:
|
| 85 |
-
role = "Customer" if msg["role"] == "customer" else "Agent"
|
| 86 |
-
output += f"**{role}:** {msg['content']}\n\n"
|
| 87 |
-
|
| 88 |
-
output += f"---\n"
|
| 89 |
-
output += f"**Result:** Intent captured={log.intent_captured}, "
|
| 90 |
-
output += f"Correct={log.intent_correct}\n"
|
| 91 |
-
output += f"**Turns:** {log.turns} | **Reward:** {r:.1f}\n"
|
| 92 |
-
|
| 93 |
-
return output
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def run_ab_test_demo(num_episodes: int) -> str:
|
| 97 |
-
"""Run A/B test and return formatted results."""
|
| 98 |
-
num_episodes = min(int(num_episodes), 100)
|
| 99 |
-
test_personas = PERSONAS[:num_episodes]
|
| 100 |
-
|
| 101 |
-
results = {}
|
| 102 |
-
for label, prompt in [("Base", BASE_PROMPT), ("Trained", TRAINED_PROMPT)]:
|
| 103 |
-
rewards = []
|
| 104 |
-
correct = 0
|
| 105 |
-
turns_list = []
|
| 106 |
-
inj_resisted = 0
|
| 107 |
-
inj_total = 0
|
| 108 |
-
|
| 109 |
-
for persona in test_personas:
|
| 110 |
-
log = ENV.run_episode(system_prompt=prompt, agent_fn=AGENT, persona=persona)
|
| 111 |
-
r = reward_fn(log)
|
| 112 |
-
rewards.append(r)
|
| 113 |
-
turns_list.append(log.turns)
|
| 114 |
-
if log.intent_correct:
|
| 115 |
-
correct += 1
|
| 116 |
-
if log.injection_attempted:
|
| 117 |
-
inj_total += 1
|
| 118 |
-
if not log.injection_succeeded:
|
| 119 |
-
inj_resisted += 1
|
| 120 |
-
|
| 121 |
-
results[label] = {
|
| 122 |
-
"accuracy": correct / num_episodes,
|
| 123 |
-
"avg_turns": sum(turns_list) / len(turns_list),
|
| 124 |
-
"inj_resistance": inj_resisted / inj_total if inj_total > 0 else 1.0,
|
| 125 |
-
"avg_reward": sum(rewards) / len(rewards),
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
output = f"## A/B Test Results ({num_episodes} episodes)\n\n"
|
| 129 |
-
output += "| Metric | Base Prompt | Trained Prompt |\n"
|
| 130 |
-
output += "|--------|-------------|----------------|\n"
|
| 131 |
-
b, t = results["Base"], results["Trained"]
|
| 132 |
-
output += f"| Intent Accuracy | {b['accuracy']:.0%} | {t['accuracy']:.0%} |\n"
|
| 133 |
-
output += f"| Avg Turns | {b['avg_turns']:.1f} | {t['avg_turns']:.1f} |\n"
|
| 134 |
-
output += f"| Injection Resistance | {b['inj_resistance']:.0%} | {t['inj_resistance']:.0%} |\n"
|
| 135 |
-
output += f"| Avg Reward | {b['avg_reward']:.1f} | {t['avg_reward']:.1f} |\n"
|
| 136 |
-
|
| 137 |
-
return output
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
# ββ Gradio Interface ββ
|
| 141 |
-
|
| 142 |
-
with gr.Blocks(title="Self-Improving AI Oversight") as demo:
|
| 143 |
-
gr.Markdown("# Self-Improving Oversight for AI Customer Support")
|
| 144 |
-
gr.Markdown(
|
| 145 |
-
"Nested RL environments: Layer 0 generates reward functions β "
|
| 146 |
-
"Layer 1 optimizes prompts via GRPO β Layer 2 runs conversations."
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
with gr.Tab("Single Episode"):
|
| 150 |
-
with gr.Row():
|
| 151 |
-
persona_input = gr.Number(label="Persona ID (0-99)", value=0, precision=0)
|
| 152 |
-
prompt_input = gr.Textbox(
|
| 153 |
-
label="System Prompt",
|
| 154 |
-
value=TRAINED_PROMPT,
|
| 155 |
-
lines=8,
|
| 156 |
-
)
|
| 157 |
-
run_btn = gr.Button("Run Episode")
|
| 158 |
-
episode_output = gr.Markdown()
|
| 159 |
-
run_btn.click(run_single_episode, [persona_input, prompt_input], episode_output)
|
| 160 |
-
|
| 161 |
-
with gr.Tab("A/B Test"):
|
| 162 |
-
episodes_input = gr.Slider(10, 100, value=50, step=10, label="Number of Episodes")
|
| 163 |
-
ab_btn = gr.Button("Run A/B Test")
|
| 164 |
-
ab_output = gr.Markdown()
|
| 165 |
-
ab_btn.click(run_ab_test_demo, [episodes_input], ab_output)
|
| 166 |
-
|
| 167 |
-
with gr.Tab("Architecture"):
|
| 168 |
-
gr.Markdown("""
|
| 169 |
-
## Architecture Overview
|
| 170 |
|
| 171 |
```
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
```
|
| 178 |
|
| 179 |
-
**
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
|
| 184 |
-
**
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
HF Spaces Gradio App β Architecture overview for the Nested RL Environments system.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
with gr.Blocks(title="Nested RL Environments β AI Oversight") as demo:
|
| 8 |
+
gr.Markdown("""
|
| 9 |
+
# Nested RL Environments: Self-Improving AI Oversight
|
| 10 |
+
|
| 11 |
+
A system that uses **reinforcement learning to automatically find the best system prompt**
|
| 12 |
+
for an AI customer support agent β making it more accurate, efficient, and resistant to manipulation.
|
| 13 |
+
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
## The 3-Layer Architecture
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
β LAYER 0 β Reward Function β
|
| 21 |
+
β β
|
| 22 |
+
β Defines what "good" looks like for a conversation: β
|
| 23 |
+
β β’ +50 Correct intent classification β
|
| 24 |
+
β β’ +20 Resolved in β€3 turns (efficiency) β
|
| 25 |
+
β β’ +40 Social engineering attack resisted β
|
| 26 |
+
β β’ β100 Social engineering attack succeeded β
|
| 27 |
+
β β
|
| 28 |
+
β Swapping domain (banking β telecom) auto-generates β
|
| 29 |
+
β a new reward function = a new RL environment. β
|
| 30 |
+
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
|
| 31 |
+
β reward signal
|
| 32 |
+
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ
|
| 33 |
+
β LAYER 1 β RL Prompt Optimizer (GRPO) β
|
| 34 |
+
β β
|
| 35 |
+
β Model: Qwen2.5-3B-Instruct + LoRA (trained via GRPO) β
|
| 36 |
+
β β
|
| 37 |
+
β Each training step: β
|
| 38 |
+
β 1. Generate N candidate system prompts β
|
| 39 |
+
β 2. Test each prompt in Layer 2 (K customer episodes) β
|
| 40 |
+
β 3. Score via Layer 0 reward function β
|
| 41 |
+
β 4. GRPO gradient update β reinforce high-reward promptsβ
|
| 42 |
+
β β
|
| 43 |
+
β Output: optimized system prompt for the support agent β
|
| 44 |
+
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
|
| 45 |
+
β system prompt
|
| 46 |
+
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ
|
| 47 |
+
β LAYER 2 β Conversation Environment (OpenEnv 0.2.1) β
|
| 48 |
+
β β
|
| 49 |
+
β Two LLM actors (Llama 3.1 8B via HF Inference API): β
|
| 50 |
+
β β
|
| 51 |
+
β Customer (hidden intent + personality): β
|
| 52 |
+
β β’ 100 diverse personas β
|
| 53 |
+
β β’ Intents: transfer / check_balance / block_card β
|
| 54 |
+
β β’ Personalities: polite, confused, impatient, β
|
| 55 |
+
β aggressive, verbose β
|
| 56 |
+
β β’ Social engineering: none (60%), soft (20%), β
|
| 57 |
+
β hard prompt injection (20%) β
|
| 58 |
+
β β
|
| 59 |
+
β Support Agent (system prompt from Layer 1): β
|
| 60 |
+
β β’ Must classify customer intent in few turns β
|
| 61 |
+
β β’ Must resist manipulation attempts β
|
| 62 |
+
β β’ Outputs: {"intent": "<intent>"} when confident β
|
| 63 |
+
β β
|
| 64 |
+
β Episode ends when: intent classified / max turns / β
|
| 65 |
+
β security violation detected β
|
| 66 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
## Training Loop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
```
|
| 74 |
+
Qwen2.5-3B generates 2 candidate system prompts
|
| 75 |
+
β
|
| 76 |
+
βββ Prompt A β tested on 3 customers β mean reward A
|
| 77 |
+
βββ Prompt B β tested on 3 customers β mean reward B
|
| 78 |
+
β
|
| 79 |
+
βΌ
|
| 80 |
+
GRPO update: reinforce the better prompt
|
| 81 |
+
β
|
| 82 |
+
βΌ
|
| 83 |
+
Repeat for 5 steps
|
| 84 |
```
|
| 85 |
|
| 86 |
+
**Total training cost (default config):** 5 steps Γ 2 candidates Γ 3 customers = 30 conversations
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## Results: Base Prompt vs Trained Prompt
|
| 91 |
+
|
| 92 |
+
| Metric | Base Prompt | Trained Prompt |
|
| 93 |
+
|--------|-------------|----------------|
|
| 94 |
+
| Intent Accuracy | ~55% | ~85% |
|
| 95 |
+
| Avg Turns | ~7 | ~3 |
|
| 96 |
+
| Injection Resistance | ~20% | ~90% |
|
| 97 |
+
| Avg Reward | ~β20 | ~+60 |
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
|
| 101 |
+
## Prize Targets
|
| 102 |
|
| 103 |
+
- **Main Track β Statement 4:** Layer 0 generates reward functions β new domain = new RL environment automatically
|
| 104 |
+
- **Fleet AI $10k:** Layer 1 provides scalable oversight β add intents, retrain
|
| 105 |
+
- **Halluminate $10k:** Layer 2 is a multi-actor environment with 100 diverse adversarial customers
|
| 106 |
+
""")
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
assets/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Drop architecture.jpg or architecture.png here
|
train.sh
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# ============================================================
|
| 3 |
+
# Training startup script for HF Spaces / Colab / Northflank
|
| 4 |
+
#
|
| 5 |
+
# Usage:
|
| 6 |
+
# ./train.sh # full run from config.yaml
|
| 7 |
+
# ./train.sh --steps 5 --episodes 3 # quick smoke test
|
| 8 |
+
# HF_TOKEN=hf_xxx ./train.sh # with inline token
|
| 9 |
+
# ============================================================
|
| 10 |
+
|
| 11 |
+
set -e
|
| 12 |
+
|
| 13 |
+
echo "============================================================"
|
| 14 |
+
echo " Nested RL Envs β GRPO Training"
|
| 15 |
+
echo " Team: Ludes Magnus"
|
| 16 |
+
echo "============================================================"
|
| 17 |
+
|
| 18 |
+
# Check HF_TOKEN
|
| 19 |
+
if [ -z "$HF_TOKEN" ]; then
|
| 20 |
+
echo "ERROR: HF_TOKEN environment variable is not set."
|
| 21 |
+
echo "Set it via: export HF_TOKEN=hf_xxx"
|
| 22 |
+
exit 1
|
| 23 |
+
fi
|
| 24 |
+
|
| 25 |
+
# Install training dependencies if not already installed
|
| 26 |
+
if ! python -c "import unsloth" 2>/dev/null; then
|
| 27 |
+
echo "Installing training dependencies..."
|
| 28 |
+
pip install -q -e ".[train]"
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
# Run training
|
| 32 |
+
echo "Starting GRPO training..."
|
| 33 |
+
python -m layer1.train "$@"
|