~/tools / Embeddings and search (RAG) / RAG Prompt Builder
RAG Prompt Builder — Build an Ollama /api/chat request that answers only from numbered, cited sources
Turn your search results and a question into an Ollama /api/chat request that asks the model to answer only from numbered sources and to cite them. Optionally asks for JSON with the source number and a quote for each citation, sets the temperature to 0, and sends "truncate": false so that a prompt that does not fit fails instead of being cut. Nothing leaves your browser.
The example sources are written by hand in the shape that json_agg returns.
3 sources, read as a JSON array of objects.
curl command
Sends the request to Ollama on this machine (localhost:11434).
curl http://localhost:11434/api/chat -d '{
"model": "qwen2.5:7b",
"messages": [
{
"role": "system",
"content": "Answer the question using only the numbered sources in the user message. Cite the number of each source you use. If the sources do not contain the answer, say that you do not know and cite nothing. Answer in the same language as the question. For each citation, give the source number and copy a short quote from that source exactly as it is written. Return JSON that matches this schema: {\"type\":\"object\",\"properties\":{\"answer\":{\"type\":\"string\"},\"citations\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"source\":{\"type\":\"integer\"},\"quote\":{\"type\":\"string\"}},\"required\":[\"source\",\"quote\"]}}},\"required\":[\"answer\",\"citations\"]}"
},
{
"role": "user",
"content": "Sources:\n\n[1] (id: 12)\nWith HNSW, hnsw.ef_search is 40 by default, so a query returns at most 40 rows unless you raise it.\n\n[2] (id: 7)\nSET LOCAL inside a transaction changes hnsw.ef_search for a single query.\n\n[3] (id: 3)\npgvector supports PostgreSQL 13 and later.\n\nQuestion: Why does my HNSW search return only 40 rows with LIMIT 100?"
}
],
"stream": false,
"format": {
"type": "object",
"properties": {
"answer": {
"type": "string"
},
"citations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"source": {
"type": "integer"
},
"quote": {
"type": "string"
}
},
"required": [
"source",
"quote"
]
}
}
},
"required": [
"answer",
"citations"
]
},
"options": {
"temperature": 0
},
"truncate": false
}'Request body (JSON)
{
"model": "qwen2.5:7b",
"messages": [
{
"role": "system",
"content": "Answer the question using only the numbered sources in the user message. Cite the number of each source you use. If the sources do not contain the answer, say that you do not know and cite nothing. Answer in the same language as the question. For each citation, give the source number and copy a short quote from that source exactly as it is written. Return JSON that matches this schema: {\"type\":\"object\",\"properties\":{\"answer\":{\"type\":\"string\"},\"citations\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"source\":{\"type\":\"integer\"},\"quote\":{\"type\":\"string\"}},\"required\":[\"source\",\"quote\"]}}},\"required\":[\"answer\",\"citations\"]}"
},
{
"role": "user",
"content": "Sources:\n\n[1] (id: 12)\nWith HNSW, hnsw.ef_search is 40 by default, so a query returns at most 40 rows unless you raise it.\n\n[2] (id: 7)\nSET LOCAL inside a transaction changes hnsw.ef_search for a single query.\n\n[3] (id: 3)\npgvector supports PostgreSQL 13 and later.\n\nQuestion: Why does my HNSW search return only 40 rows with LIMIT 100?"
}
],
"stream": false,
"format": {
"type": "object",
"properties": {
"answer": {
"type": "string"
},
"citations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"source": {
"type": "integer"
},
"quote": {
"type": "string"
}
},
"required": [
"source",
"quote"
]
}
}
},
"required": [
"answer",
"citations"
]
},
"options": {
"temperature": 0
},
"truncate": false
}In the response, the answer is in message.content. If prompt_eval_count is much lower than you expect, the prompt may have been cut.
Request fields from Ollama's API reference and source code, checked on Ollama 0.40.11 on 2026-11-05. Ollama API: /api/chat
Everything is done in your browser. The tool writes the request and connects to nothing.
In retrieval-augmented generation (RAG), you search your documents first and then give the closest passages to the model together with the question. This tool writes that request for Ollama's /api/chat. Paste the search results as a JSON array of strings, as a JSON array of objects (for example the output of PostgreSQL's json_agg; the text is read from content, text, chunk, document or page_content, and id, title and url are added to the source's heading), or as plain text separated by lines of --- or by blank lines. The tool numbers the sources from [1], puts the rules into a system message, and puts the sources and then the question into one user message. With the JSON option, the format field gets a JSON Schema with the answer and a list of citations (source number and quote), and the same schema is written into the system message, as Ollama's structured outputs page recommends. Without it, the model is asked to write the numbers in square brackets after each sentence. The truncate option matters because Ollama cuts long prompts without an error: in Ollama's source code (0.40.11), it keeps the first few tokens and the end of the prompt and discards the part in between, and in our test with num_ctx 1024 this removed the system message and the first sources and changed the answer. The truncate field is in Ollama's source code but not on the /api/chat page; with "truncate": false, the same request returned HTTP 400 instead. We sent the requests from this tool to Ollama 0.40.11 with qwen2.5:7b, in both modes.
The core logic of this tool is open source under the MIT License. github.com/seigo5555/toolkitbox-lib/src/rag-prompt.ts
Use Cases
- Answer questions about your own manuals or notes with a local model, with the source of each statement
- Try the prompt and the citation format on a few real search results before you write the code for your application
- Find out whether your search results still fit into the model's context length
Usage
- Enter the question and paste the search results, for example the rows returned by the query from the pgvector SQL Builder.
- Choose the model, and optionally num_ctx and the options for JSON citations, temperature 0 and truncate.
- Copy the curl command or the JSON body and send it to Ollama. Paste the answer and the same sources into the RAG Citation Checker.
FAQ
Why are the sources numbered instead of using my database ids?
Short numbers are easier for the model to copy than long ids, and they can be checked against the list that was sent. The id, title and url of each object are still shown next to its number, so you can map the number back to the row.
Why does the question come after the sources?
When a prompt is longer than the context length, Ollama keeps the end of the prompt and discards text before it, so the question at the end is the part that is kept. It is safer still to send "truncate": false, so that you get an error and can send fewer rows or raise num_ctx.
How many sources can I send?
The tool accepts up to 50. How many fit depends on their length and on the context length: Ollama's documentation gives a default of 4k tokens with less than 24 GiB of VRAM. The prompt_eval_count field of the response shows how many tokens the prompt used.
Does the JSON option guarantee correct citations?
No. The format field makes the answer follow the JSON Schema, but the model can still cite the wrong source or quote something that is not in it. Check the answer with the RAG Citation Checker.
Can I use it with the OpenAI-compatible API?
The tool writes the native /api/chat request. Ollama's documentation says that structured outputs also work through the OpenAI-compatible API via response_format, but the request has a different shape.
Is my data sent anywhere?
No. The request is built in your browser. The tool never connects to Ollama.
Related articles
- Store and search Ollama embeddings in PostgreSQL with pgvectorKeep Ollama embeddings in PostgreSQL with pgvector, search them with HNSW or IVFFlat, and pass the results to /api/chat so that the answer cites its sources.
- Embeddings with Ollama: the basics for search and RAGMake embeddings with Ollama's /api/embed: the models and their sizes, truncate and dimensions, unit-length vectors and cosine similarity, from the official docs.