Principle:1Panel dev MaxKB Prompt Construction
| Knowledge Sources | |
|---|---|
| Domains | Prompt_Engineering, RAG |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
A prompt assembly technique that combines retrieved knowledge context, conversation history, and user questions into structured LangChain message lists for LLM invocation.
Description
Prompt construction is the bridge between retrieval and generation in RAG. MaxKB's BaseGenerateHumanMessageStep assembles the final message list by:
- Including an optional SystemMessage (system prompt with long-term memory substitution)
- Including recent chat history (limited by `dialogue_number`, typically 3 turns)
- Constructing a HumanMessage with the prompt template, where `{data}` is replaced with formatted paragraph content (title: content pairs wrapped in `` tags, truncated to `max_paragraph_char_number`) and `{question}` is replaced with the (optimized) user question
If no paragraphs were found, the `no_references_setting` fallback applies: either `ai_questioning` (ask the AI a standalone question) or `designated_answer` (return a predefined response).
Usage
Use this principle when designing:
- RAG prompt templates with knowledge context injection
- Multi-turn conversation prompt assembly
- Fallback behavior for empty retrieval results
Theoretical Basis
The RAG prompt structure follows a consistent template pattern:
# Abstract algorithm description (NOT real implementation)
def build_prompt(question, paragraphs, prompt_template, max_chars):
if not paragraphs:
return fallback_prompt.replace("{question}", question)
# Format knowledge data
data_parts = []
for p in paragraphs:
entry = f"{p.title}:{p.content}"
if len(data_parts) + len(entry) > max_chars:
entry = entry[:max_chars - len(data_parts)]
data_parts.append(f"<data>{entry}</data>")
break
data_parts.append(f"<data>{entry}</data>")
data = "\n".join(data_parts)
return prompt_template.replace("{data}", data).replace("{question}", question)
The `` tag wrapping helps the LLM distinguish between knowledge context and the user's question, improving citation accuracy and reducing hallucination.