System Prompts and Prompt Templates
What a System Prompt Is
Every LLM chat API distinguishes between three message roles:
- system — instructions to the model (not shown to the end user)
- user — the user's question or input
- assistant — the model's previous responses (for multi-turn conversations)
The system message is the highest-priority instruction. It shapes how the model responds to every subsequent user message.
defaultSystem() vs .system()
| Method | When it applies | Use case |
|---|---|---|
defaultSystem(text) |
Set once at bean creation via ChatClient.builder() |
Persistent persona/rules that apply to every call — SYSTEM_PREAMBLE in Power RAG |
.system(text) |
Set per-request in the fluent chain | Override the system prompt for a specific call without rebuilding the bean |
MultilingualPromptBuilder
The user message in Power RAG is not just the raw question. It is a structured prompt containing retrieved context, the question, and language/citation instructions — all assembled by MultilingualPromptBuilder.
public String buildUserMessage(String question, String context,
String language, boolean hasRelevantContext,
boolean imagePresent) {
String imageInstruction = imagePresent
? "An image has been attached. Carefully examine it...\n\n"
: "";
if (!hasRelevantContext || context.isBlank()) {
return imageInstruction +
"The knowledge base does not contain relevant documents. " +
"Answer using your general knowledge.\n\n" +
"Question: " + question + "\n\n" + langInstruction;
}
return imageInstruction + context +
"\n\nQuestion: " + question +
"\n\nPrimarily answer using the sources above, citing [SOURCE N]..." +
langInstruction;
}
Prompt Structure in Practice
The final user message sent to the LLM looks like this when context is available:
And this when no relevant context was found:
Language Handling
MultilingualPromptBuilder appends a language instruction at the end of every user message. The language is detected from the request (or defaulted to English) and passed through the entire pipeline so that:
- The LLM responds in the user's language
- The semantic cache lookup is language-scoped (an English hit is not served to a Spanish query)
- Audit logs record the detected language for analytics