Artificial Intelligence 26 min read

A Comprehensive Prompt Engineering Framework: Universal Templates, RAG, Few‑Shot, Memory, and Automated Optimization

The article presents a universal four‑part prompt template—role, problem description, goal, and requirements—augmented with role definitions, RAG‑based knowledge retrieval, few‑shot examples, memory handling, temperature/top‑p tuning, and automated optimization techniques such as APE, APO, and OPRO, enabling developers to reliably craft high‑quality prompts for LLMs.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
A Comprehensive Prompt Engineering Framework: Universal Templates, RAG, Few‑Shot, Memory, and Automated Optimization

In the era of large language models (LLMs), the core skill for developers is Prompt Engineering – the systematic method of crafting prompts that dramatically affect model performance.

1. Universal Prompt Framework – The article proposes a four‑part template: role + problem description + goal + requirements . This structure yields a “passable” first‑draft prompt for most tasks.

2. Role Definition – A role acts as an “ability package”. The recommended role template is:

现在你是一位优秀的{{你想要的身份}},拥有{{你想要的教育水平}},并且具备{{你想要的工作年份及工作经历}},你的工作内容是{{与问题相关的工作内容}},同时你具备以下能力{{你需要的能力}}

Example (financial analysis):

现在你是一位优秀的{{财务分析顾问}},拥有{{财务学、经济学等专业的硕士或博士学位}},并且具备{{八年以上的财务分析工作经验}},你的工作内容是{{对投融资数据进行分析}},同时你具备以下能力:{{专业知识、分析能力、学习能力等}}

3. Problem & Goal Specification – Clearly state what the model should do, the desired output, and any constraints (e.g., format, length). The article provides concrete examples such as generating code comments or test‑case design.

4. Requirement Placement & Formatting – Place constraints at the end of the prompt to leverage the model’s “text‑completion” bias. Use clear separators (e.g., #, <>, ```), empty lines, or Markdown to improve readability.

5. Enhancing Prompts with RAG – Retrieval‑Augmented Generation (RAG) injects dynamic external knowledge via vector‑based similarity search. The article explains the classic RAG pipeline (embedding + vector DB) and mentions frameworks like LangChain, Milvus, LlamaIndex, and Pinecone.

6. Few‑Shot Learning – Providing multiple high‑quality examples improves stability but may reduce model creativity. The recommendation is to use “few‑but‑effective” examples, possibly retrieved by RAG.

7. Memory Management – Short‑term memory (current conversation) and long‑term memory (historical dialogues) can be incorporated to preserve context across interactions.

8. Controlling Model Determinism – Adjust temperature and top‑p to balance randomness and stability. Lower temperature yields deterministic outputs; higher temperature increases diversity. Top‑p truncates the probability mass to a threshold.

9. Automated Prompt Optimization – Recent research enables LLMs to self‑optimize prompts. Representative algorithms include:

APE – generate candidate prompts, score on a training set, and perform Monte‑Carlo‑style sampling around top candidates.

APO – apply gradient‑descent‑like updates to prompts based on loss gradients.

OPRO – treat the LLM itself as an optimizer, iteratively refining prompts via natural‑language instructions.

10. Conclusion – By adopting the universal framework, enriching prompts with RAG, few‑shot, memory, and proper parameter tuning, developers can reliably build high‑quality prompts. The article, authored by Liu Congwei of Tencent Cloud, aims to make Prompt Engineering accessible to all developers, turning anyone into a Prompt Engineer.

Prompt Engineeringlarge language modelsRAGfew-shot learningAI Prompt Optimization
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.