Designing Effective Prompts for Large Language Models: Structure, Code Examples, and Regex Extraction
The article presents a systematic prompt template—comprising Instruction, Input Data, Context, and Output Indicator—demonstrates code examples for single‑ and multi‑task formatting, shows how clear markers enable regex extraction, and introduces Baidu’s PaddlePaddle Star River Community to simplify building reliable LLM‑driven applications.
Large language model (LLM) outputs often contain redundant information. This article proposes a prompt template—based on Wenxin Yiyan—that includes Instruction, Input Data, Context, and Output Indicator, enabling LLMs to follow developer commands precisely.
The author, Zhang Hongshen, is a Ph.D. candidate at Zhejiang University, with research in data science and power systems, and has been featured by Nature.
Two main problems are identified: (1) the lack of a theoretical system for prompt design, and (2) mismatches between model output and developer expectations, requiring manual post‑processing.
The article addresses these issues from two perspectives: the basic composition of prompts and advanced applications such as formatted output.
The basic components are:
Instruction – the core task description.
Input Data – the specific information the model must process.
Context – additional background or constraints.
Output Indicator – the desired format or type of the result.
Example: judging whether a student's statement is correct. The four components are combined into a single prompt.
user_input = "请判断1+1=3是否正确,正确请使用'正确'表示,错误请使用'错误'表示,请仅输出'正确'和'错误',请勿输出其他任何信息"
response = erniebot.ChatCompletion.create(
model='ernie-3.5',
messages=[{'role':'user','content': user_input}]
)
print(response.get_result())A simple prompt may still yield extra explanations. By explicitly including all four components, the model produces a concise, structured answer.
user_input = "请判断学生说的话是否正确
学生说的话:1+1=3
信息:正确请使用'正确'表示,错误请使用'错误'表示
输出格式:
###是否正确
{是否正确}"
response = erniebot.ChatCompletion.create(
model='ernie-3.5',
messages=[{'role':'user','content': user_input}]
)
print(response.get_result())For multi‑task scenarios, additional sections can be added, e.g., requesting the model to also explain the error.
user_input = "请判断学生说的话是否正确
学生说的话:1+1=3
信息:正确请使用'正确'表示,错误请使用'错误'表示
输出格式:
###是否正确
{是否正确}
###学生的错误
{学生的错误}"
response = erniebot.ChatCompletion.create(
model='ernie-3.5',
messages=[{'role':'user','content': user_input}]
)
print(response.get_result())After obtaining a formatted response, regular expressions can extract the required fields.
import re
text = response.get_result()
pattern_correctness = re.compile(r'###是否正确
(.*?)
###学生的错误', re.DOTALL)
pattern_error = re.compile(r'###学生的错误
(.*)', re.DOTALL)
correctness_result = pattern_correctness.search(text)
error_result = pattern_error.search(text)
is_correct = correctness_result.group(1).strip() if correctness_result else None
student_error = error_result.group(1).strip() if error_result else None
print(f"{is_correct}")
print(f"{student_error}")Using a clear marker like “###” simplifies regex matching and enables downstream processing of LLM outputs.
The article also introduces the PaddlePaddle Star River Community, a Baidu‑backed platform that provides compute resources, model libraries, datasets, and an online coding environment, lowering the barrier for AI application development.
import erniebot
erniebot.api_type = 'aistudio'
erniebot.access_token = '<your token>'
user_input = "请判断1+1=3是否正确,正确请使用'正确'表示,错误请使用'错误'表示,请仅输出'正确'和'错误'"
response = erniebot.ChatCompletion.create(
model='ernie-3.5',
messages=[{'role':'user','content': user_input}]
)
print(response.get_result())By combining the basic prompt components with single‑task or multi‑task formatting, developers can build reliable AI applications and extract structured information efficiently.
Overall, the rapid development of general LLMs and AI agents heralds a new era for AI application development, accessible to both technical and non‑technical users.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
