Sub topic 12
Grounding a prompt template with a retriever
This chapter shows how to connect a retriever to a prompt template in Prompt Builder, so the model answers from retrieved Knowledge excerpts. It assumes you have already built a search index and a retriever over your Knowledge content. You should also know how to open Prompt Builder and edit a template.
What a grounded prompt template does
A prompt template is a reusable set of instructions with placeholders. When the template runs, Salesforce fills each placeholder with data, then sends the finished prompt to the model. A retriever is one of the resources you can insert. At run time it searches the index with the customer's question and returns the top matching chunks. Those chunks replace the placeholder.
The model never searches by itself. It sees only what the template gives it.
Build the template
- In Setup, open Prompt Builder and create a new template. A Flex template works well because you choose your own input resources.
- Add an input resource for the question, for example a free text input named
Question. - Add the retriever as a resource. Select your Knowledge retriever and map its search text to the
Questioninput. - Write the instructions and insert both resources where they belong.
- Preview the template with a sample question and check the resolved prompt.
A starting prompt looks like this:
Answer the customer's question using only the knowledge article excerpts provided below.
If the excerpts do not contain the answer, say you could not find it and offer to connect the customer with a human agent.
Do not use outside knowledge. Mention the article title when you rely on an excerpt.
Excerpts:
<retriever results inserted here by the prompt template>
Question:
<customer question inserted here>The bracketed lines stand for the resources you insert with the resource picker. They are not text you type.
Why each instruction matters
| Instruction | Purpose |
|---|---|
| "using only the excerpts" | Limits the model to retrieved content |
| Fallback sentence | Defines behavior when retrieval returns nothing |
| "Do not use outside knowledge" | Reduces answers from general training data |
| "Mention the article title" | Lets a reader verify the source |
The title instruction only works if the retriever returns the title field. Check the returned fields you chose earlier.
Common errors
- Placeholder not mapped. The retriever runs with an empty query and returns weak or random chunks. Confirm the preview shows the question text in the search input.
- Empty excerpts section. The index may have no matching content, or a filter may be too strict. Test the retriever on its own first.
- Model answers from general knowledge anyway. The fallback instruction is missing or vague. State the fallback explicitly.
- Too many chunks. A large result count adds noise and uses more of the prompt size limit. Keep the count modest.
Exercise
A preview of your grounded template returns a fluent answer, but the resolved prompt shows an empty excerpts section. Which part do you fix first, and why?
Show answer
Fix retrieval first. The model had no excerpts, so its answer came from general knowledge. Test the retriever alone, check the input mapping and any filters, and confirm the article is published and indexed. Then rewrite the instructions so the model returns the fallback message when excerpts are empty.