Skip to content

Commit 713db03

Browse files
terlan98Tarlan Ismayilsoy
andauthored
Create note "Explore prompt design & safety for on-device foundation models" (#99)
Co-authored-by: Tarlan Ismayilsoy <[email protected]>
1 parent c7227f3 commit 713db03

8 files changed

Lines changed: 152 additions & 4 deletions

File tree

Sources/WWDCNotes/WWDCNotes.docc/WWDC25/WWDC25-248-Explore-prompt-design-and-safety-for-ondevice-foundation-models.md

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,159 @@ Design generative AI experiences that leverage the strengths of the Foundation M
88
@CallToAction(url: "https://developer.apple.com/videos/play/wwdc2025/248", purpose: link, label: "Watch Video (22 min)")
99

1010
@Contributors {
11-
@GitHubUser(<replace this with your GitHub handle>)
11+
@GitHubUser(terlan98)
1212
}
1313
}
1414

15-
😱 "No Overview Available!"
15+
## Key Takeaways
1616

17-
Be the hero to change that by watching the video and providing notes! It's super easy:
18-
[Learn More…](https://wwdcnotes.com/documentation/wwdcnotes/contributing)
17+
- 🤖 Best for simple tasks; no facts, math, and code generation
18+
- ✍️ Use clear prompts with commands, examples and length control
19+
- 🛡️ Multiple safety layers: built-in guardrails, instructions, and input controls
20+
- 🧪 Evaluation and testing required for better quality and safety
21+
22+
## Design for on-device LLM
23+
The on-device LLM has some limitations:
24+
- **Complex tasks**
25+
- Break down tasks into simpler steps to get good results
26+
- **Math calculations**
27+
- Use traditional (non-AI) code for calculations instead
28+
- **Code generation**
29+
- Avoid code generation prompts since the model is not optimized for code
30+
- **Factual or world knowledge**
31+
- Don't rely on the model for facts and recent events
32+
- Can still be used in games and scenarios where the accuracy of the output is not too important
33+
- **Hallucinations**
34+
- Use guided generation (see <doc:WWDC25-286-Meet-the-Foundation-Models-framework>) to improve response reliability
35+
36+
Hallucination example (model thinks plain bagels have toppings):
37+
@Image(source: "WWDC25-248-Bagel-Mistake")
38+
39+
## Prompting best practices
40+
- Use length qualifiers, such as paragraph or word count
41+
- Example phrases: *"in a few words"*, *"in three sentences"*, *"in a single paragraph"*, "in detail"
42+
```swift
43+
let prompt = "In a single paragraph, generate a bedtime story about a fox."
44+
```
45+
46+
- Specify a role and style
47+
48+
@Image(source: "WWDC25-248-Role-and-Style")
49+
50+
- Use clear commands
51+
- Give a single specific task in detail
52+
- Provide up to 5 examples
53+
- Use all-caps strong commands like MUST and DO NOT to control the behavior
54+
55+
> Tip: The new Playgrounds feature (see <doc:WWDC25-247-Whats-new-in-Xcode>) is a great place to test prompts directly in Xcode
56+
57+
@Image(source: "WWDC25-248-Playground")
58+
59+
### Instructions vs Prompts
60+
The aforementioned best practices are applicable to both instructions and prompts.
61+
62+
An **instruction** is a special type of prompt that defines how the model should behave across all subsequent prompts in a session.
63+
The model receives the instruction before any other prompt.
64+
65+
@Image(source: "WWDC25-248-Instructions")
66+
67+
### Interactive experiences
68+
You can offer some interactivity in your app by allowing your users to provide prompts to the model.
69+
70+
@Row {
71+
@Column {}
72+
@Column(size: 2) {
73+
@Image(source: "WWDC25-248-Interactivity")
74+
}
75+
@Column {}
76+
}
77+
78+
## Design for safety
79+
While Apple's Foundation Models framework comes with integrated safety features, it's important to evaluate potential risks specific to your app.
80+
81+
### Built-in guardrails
82+
The framework automatically applies guardrails to:
83+
- **Input:** Instructions, prompts, and tool calls are screened for harmful content
84+
- **Output:** Model responses are filtered even if inputs bypass the initial screening
85+
86+
You can catch and handle guardrail violation errors:
87+
```swift
88+
do {
89+
try await session.respond(to: prompt)
90+
} catch LanguageModelSession.GenerationError.guardrailViolation {
91+
print("Safety guardrail violation occurred.")
92+
}
93+
```
94+
95+
> Note: Errors occurring in **proactive features**, not driven by user actions, are safe to ignore. However, any errors originating from **user-initiated features** should show suitable UI feedback to the user.
96+
97+
### Build trust and safety
98+
- Disallow inappropriate content
99+
- Handle user input with care
100+
- Evaluate potential consequences of users acting on your app's output
101+
102+
### Add safety instructions
103+
@Row {
104+
@Column {}
105+
@Column(size: 2) {
106+
@Image(source: "WWDC25-248-Safety-Instructions")
107+
}
108+
@Column {}
109+
}
110+
111+
> Important: Instructions should only come from you as the developer. Never include untrusted user content in your instructions.
112+
113+
### User input handling patterns
114+
- Direct user input (high flexibility, high risk)
115+
```swift
116+
let prompt = userInput
117+
```
118+
- Combined prompts (balanced approach)
119+
```swift
120+
let prompt = "Generate a story about \(userInput)"
121+
```
122+
- Curated prompts (low flexibility, low risk)
123+
```swift
124+
enum Topic: String {
125+
case adventure = "an adventure in an ancient forest"
126+
case fantasy = "a fantasy on an uninhibited island"
127+
}
128+
129+
let topic: Topic = .adventure
130+
let prompt = "Generate a story about \(topic.rawValue)"
131+
```
132+
133+
### Use case-specific mitigations
134+
Consider the real-world impact of the generated content in your app. Here are some examples:
135+
- Bagel flavor generation app
136+
- Show an allergen warning
137+
- Add the ability to disable some ingredients in app settings
138+
- Trivia generation app
139+
- Use denying keywords in your instructions to avoid controversial political topics or inappropriate content
140+
- Train a classifier for more reliable outputs
141+
142+
### Layering-based approach
143+
The aforementioned safety recommendations act as multiple layers. Each layer has its weaknesses but when stacked together, the chances of a safety violation passing through all of them is very low.
144+
145+
@Row {
146+
@Column {}
147+
@Column {
148+
@Image(source: "WWDC25-248-Layers")
149+
}
150+
@Column {}
151+
}
152+
153+
## Evaluate and test
154+
- Curate a dataset with prompts for all use cases and safety issues
155+
- Create automated tests with manual checks
156+
- Use another LLM to grade responses
157+
- Test failure scenarios with safety violations to see how your app behaves
158+
159+
The model will be updated continuously. Make sure to report any safety issues you encounter.
160+
161+
## Safety checklist
162+
- [ ] Handle guardrail violation errors
163+
- [ ] Add safety instructions
164+
- [ ] Control user input in prompts
165+
- [ ] Apply use case-specific mitigations
166+
- [ ] Evaluate and test
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)