## 3 Methodology
### Iterative Optimization of Prompt Engineering  
In RAG systems, prompt engineering serves as a critical component for optimizing generation quality. Its object is to design efficient prompt templates that guide the model to synthesize retrieved contextual information and produce accurate, coherent responses. The design principles for prompts emphasize task specificity, descriptive granularity, and unambiguous instruction. A recommended methodology involves adopting a stepwise-prompting framework with iterative refinement. This approach begins with analyzing fundamental corpus units and initial testing using minimalistic prompts. Through continuous evaluation of LLM outputs, the prompts are dynamically adjusted to enhance expressiveness and operational efficacy [48; 49; 50]. In this study, the optimization of prompts follows a three-step progressive approach.  
Step 1: Construct prompt templates that clearly define task requirements and effectively utilize retrieval context. Typical RAG prompts include the following elements:  
* Task Instruction: Clearly specify the type of generation task (e.g., question answering, summarization).
* Retrieval Context: Insert the top-k relevant text chunks as references.
* Query Statement: The user's original input question or command.
* Format Constraints: Specify the output format (e.g., JSON, bullet points).  
A typical basic prompt template structure is shown in Figure 3. In the figure, the left part is the template, and the right part is an application case.  
Step 2: Optimize the integration of retrieval context and query to enhance generation relevance. In the prompt, employing interpretable or suggestive markers indicated in the [Context] to differentiate various chunks enhances the model's focus on the primary relevant chunk, as illustrated in Figure 4. Additionally, weights can be assigned to each context chunk based on retrieval context and similarity, guiding the model to pay more attention to highly relevant segments [51]. In the example shown in Figure 4, the [Context] includes the chapter name where the chunk text is located and the weight related to the relevance of the problem, thereby guiding the LLM to focus on the relevant content.  
Step 3: Adapt the generative model to task requirements through instruction tuning (Instruction Tuning). This involves making instructions clear and specific, avoiding ambiguous commands (such as \"Please answer\"), and instead using concrete actions (like \"List three options\"). Control the length of the output by specifying constraints in the instructions (e.g., \"Summarize in one sentence\"). Achieve domain adaptation by incorporating terminology constraints relevant to specialized fields (e.g., \"Explain using construction engineering terms\"). When necessary, include example input-output pairs within prompts to guide the model in mimicking format and logic, as shown in Figure 5.  
Figure 4: Prompt with context integration.  
Figure 3: Basic prompt.  
Due to the variations in researchers' habits and modes of expression, prompts in practical applications are not uniform, and the final prompt typically necessitates iterative refinement throughout the testing phase. This flexibility offers extensive scope for creative and exploratory tasks. However, it also mandates that researchers possess a deep comprehension of the specific tasks at hand and exhibit proficiency in meticulously calibrating prompts.  
Through systematic prompt engineering strategies, the RAG systems can precisely leverage the retrieved contexts to generate high-quality and highly relevant textual outputs, effectively bridging the gap between knowledge retrieval and generative coherence.