File size: 11,717 Bytes
e706de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# Concept: System Prompts & Agent Specialization

## Overview

This example demonstrates how to transform a general-purpose LLM into a **specialized agent** using **system prompts**. The key insight: you don't need different models for different tasksβ€”you need different instructions.

## What is a System Prompt?

A **system prompt** is a persistent instruction that shapes the AI's behavior for an entire conversation session.

### Analogy
Think of hiring someone for a job:

```

Without System Prompt          With System Prompt

─────────────────────         ──────────────────────

"Hi, I'm an AI."              "I'm a professional translator

                               with expertise in scientific

What do you want?"            German. I follow strict quality

                              guidelines and output format."

```

## How System Prompts Work

### The Context Structure

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚           CONTEXT WINDOW                    β”‚

β”‚                                             β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  SYSTEM PROMPT (Always present)       β”‚ β”‚

β”‚  β”‚  "You are a professional translator..." β”‚

β”‚  β”‚  "Follow these rules..."              β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β”‚                    ↓                        β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  USER MESSAGES                        β”‚ β”‚

β”‚  β”‚  "Translate this text..."             β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β”‚                    ↓                        β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  AI RESPONSES                         β”‚ β”‚

β”‚  β”‚  (Shaped by system prompt)            β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

The system prompt sits at the top of the context and influences **every** response.

## Agent Specialization Pattern

### Transformation Flow

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  General Model   β”‚ +  β”‚ System Prompt   β”‚ =  β”‚ Specialized Agentβ”‚

β”‚                  β”‚    β”‚                 β”‚    β”‚                  β”‚

β”‚ β€’ Knows many     β”‚    β”‚ β€’ Define role   β”‚    β”‚ β€’ Translation    β”‚

β”‚   things         β”‚    β”‚ β€’ Set rules     β”‚    β”‚   Agent          β”‚

β”‚ β€’ No specific    β”‚    β”‚ β€’ Constrain     β”‚    β”‚ β€’ Coding Agent   β”‚

β”‚   role           β”‚    β”‚   output        β”‚    β”‚ β€’ Analysis Agent β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

### Example Specializations

**Translation Agent (this example):**
```

System Prompt = Role + Rules + Output Format

```

**Code Assistant:**
```javascript

systemPrompt: "You are an expert programmer. 

Always provide working code with comments.

Explain complex logic."

```

**Data Analyst:**
```javascript

systemPrompt: "You are a data analyst.

Always show your calculations step-by-step.

Cite data sources when available."

```

## Anatomy of an Effective System Prompt

### The 5 Components

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  1. ROLE DEFINITION                     β”‚

β”‚  "You are a [specific role]..."         β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  2. TASK DESCRIPTION                    β”‚

β”‚  "Your goal is to..."                   β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  3. BEHAVIORAL RULES                    β”‚

β”‚  "Always do X, Never do Y..."           β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  4. OUTPUT FORMAT                       β”‚

β”‚  "Format your response as..."           β”‚

β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€

β”‚  5. CONSTRAINTS                         β”‚

β”‚  "Do NOT include..."                    β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

### This Example's Structure

```

Role:        "Professional scientific translator"

Task:        "Translate English to German with precision"

Rules:       8 specific translation guidelines

Format:      Idiomatic German, scientific style

Constraints: "ONLY translated text, no explanation"

```

## Why Detailed System Prompts Matter

### Comparison Study

**Minimal System Prompt:**
```javascript

systemPrompt: "Translate to German"

```

**Result:**
- May add unnecessary explanations
- Inconsistent terminology
- Mixed formality levels
- Extra conversational text

**Detailed System Prompt (this example):**
```javascript

systemPrompt: `You are a professional translator...

- Rule 1: Preserve technical accuracy

- Rule 2: Use idiomatic German

- Rule 3: Follow scientific conventions

...

DO NOT add any explanations`

```

**Result:**
- βœ… Consistent quality
- βœ… Correct terminology
- βœ… Proper formatting
- βœ… Only translation output

### Quality Impact

```

Detail Level          Output Quality

───────────         ─────────────────

Very minimal  β†’     Unpredictable

Basic role    β†’     Somewhat consistent

Detailed      β†’     Highly consistent ⭐

Over-detailed β†’     May confuse model

```

## System Prompt Design Patterns

### Pattern 1: Role-Playing
```

"You are a [profession] with expertise in [domain]..."

```
Makes the model adopt that perspective.

### Pattern 2: Rule-Based
```

"Follow these rules:

1. Always...

2. Never...

3. When X, do Y..."

```
Explicit constraints lead to predictable behavior.

### Pattern 3: Output Formatting
```

"Format your response as:

- JSON

- Markdown

- Plain text only

- Step-by-step list"

```
Controls the structure of responses.

### Pattern 4: Contextual Awareness
```

"You remember: [previous facts]

You know that: [domain knowledge]

Current situation: [context]"

```
Primes the model with relevant information.

## How This Relates to AI Agents

### Agent = Model + System Prompt + Tools

```

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚             AI Agent                       β”‚

β”‚                                            β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  System Prompt (Agent's "Identity")  β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β”‚                  ↓                         β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  LLM (Agent's "Brain")               β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β”‚                  ↓                         β”‚

β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚

β”‚  β”‚  Tools (Agent's "Hands") [Optional]  β”‚ β”‚

β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

**In this example:**
- System Prompt: "You are a translator..."
- LLM: Apertus-8B model
- Tools: None (translation is done by the model itself)

**In more complex agents:**
- System Prompt: "You are a research assistant..."
- LLM: Any model
- Tools: Web search, calculator, file access, etc.

## Practical Applications

### 1. Domain Specialization
```

Medical β†’ "You are a medical professional..."

Legal β†’ "You are a legal expert..."

Technical β†’ "You are an engineer..."

```

### 2. Output Control
```

JSON API β†’ "Always respond in valid JSON"

Markdown β†’ "Format all responses as markdown"

Code β†’ "Only output executable code"

```

### 3. Behavioral Constraints
```

Concise β†’ "Use maximum 2 sentences"

Detailed β†’ "Explain thoroughly with examples"

Neutral β†’ "Avoid opinions, state only facts"

```

### 4. Multi-Language Support
```

systemPrompt: `You are a multilingual assistant.

Respond in the same language as the input.`

```

## Chat Wrappers Explained

Different models need different conversation formats:

```

Model Type        Format Needed         Wrapper

──────────────   ───────────────────   ─────────────────

Llama 2/3        Llama format          LlamaChatWrapper

GPT-style        ChatML format         ChatMLWrapper

Harmony models   Harmony format        HarmonyChatWrapper

```

**What they do:**
```

Your Message β†’ [Chat Wrapper] β†’ Formatted Prompt β†’ Model

                    ↓

          Adds special tokens:

          <|system|>, <|user|>, <|assistant|>

```

The wrapper ensures the model understands which part is the system prompt, which is the user message, etc.

## Key Takeaways

1. **System prompts are powerful**: They fundamentally change how the model behaves
2. **Detailed is better**: More specific instructions = more consistent results
3. **Structure matters**: Role + Rules + Format + Constraints
4. **No retraining needed**: Same model, different behaviors
5. **Foundation for agents**: System prompts are the first step in building specialized agents

## Evolution Path

```

1. Basic Prompting           (intro.js)

       ↓

2. System Prompts            (translation.js) ← You are here

       ↓

3. System Prompts + Tools    (simple-agent.js)

       ↓

4. Multi-turn reasoning      (react-agent.js)

       ↓

5. Full Agent Systems

```

This example bridges the gap between basic LLM usage and true agent behavior by showing how to specialize through instructions.