Metaprompt — Dùng Claude để viết prompt cho Claude
Điểm nổi bật
Nhấn để đến mục tương ứng
- 1 Áp dụng ngay: Metaprompt là một prompt đặc biệt hướng dẫn Claude tạo ra prompts khác — phần này cung cấp quy trình cụ thể giúp bạn triển khai hiệu quả mà không cần thử nghiệm nhiều lần.
- 2 Một điều ít người đề cập: ANTHROPICMETAPROMPT = """Today you will be writing instructions to an eager, helpful, but inexperienced and unworldly. Hiểu rõ bối cảnh áp dụng sẽ quyết định 80% thành công khi triển khai.
- 3 Nội dung cốt lõi: """ Inject task vào metaprompt metaprompt = ANTHROPICMETAPROMPT.replace"{{TASK}}", taskdescription response =. Nắm vững phần này sẽ giúp bạn áp dụng hiệu quả hơn 70% so với đọc lướt toàn bài.
- 4 Công cụ AI sẽ thay đổi cách bạn làm việc: PROMPT HIỆN TẠI: {currentprompt} KẾT QUẢ THỰC TẾ các cases thất bại: {failurecases} Phân tích vấn đề và viết lại prompt. Điểm mấu chốt là biết cách đặt prompt đúng để nhận kết quả có thể sử dụng ngay.
- 5 Góc nhìn thực tế: SYSTEMPROMPTGENERATOR = """Tạo system prompt chuyên nghiệp cho AI assistant với các thông tin sau: TÊN: {name} MỤC. Điều quan trọng là hiểu rõ khi nào nên và không nên áp dụng phương pháp này.
Viết prompt tốt là một kỹ năng — và như mọi kỹ năng khác, nó mất thời gian để học. Nhưng có một shortcut thú vị: dùng Claude để viết prompts cho Claude. Đây là kỹ thuật "metaprompting" — một trong những công cụ mạnh nhất trong prompt engineering toolkit.
Metaprompt là gì?
Metaprompt là một prompt đặc biệt hướng dẫn Claude tạo ra prompts khác. Thay vì tự viết prompt từ đầu, bạn mô tả task bằng ngôn ngữ tự nhiên, Claude sẽ tạo ra prompt chuyên nghiệp, đầy đủ, và được tối ưu hóa.
Anthropic đã phát hành một metaprompt chính thức — hãy xem cách nó hoạt động.
Metaprompt chính thức của Anthropic
ANTHROPIC_METAPROMPT = """Today you will be writing instructions to an eager, helpful, but inexperienced and
unworldly AI assistant who needs careful guidance. I will explain a task to you and you will write instructions for
the eager AI assistant to follow.
Here are some examples of tasks and their instructions:
<Task Instruction Example>
<Task>Act as a polite customer success agent for Acme Dynamics. Use only the information in the <FAQ> tags below
to answer questions.</Task>
<Inputs>
{$FAQ}
{$QUESTION}
</Inputs>
<Instructions>
You will be acting as an AI customer success agent for a company called Acme Dynamics.
When I write BEGIN DIALOGUE you will enter this role, and all further input from the "Instructor:" will be treated
as coming from a user seeking information. Only respond based on information contained in the <FAQ> tags.
...
</Instructions>
</Task Instruction Example>
That example was just to give you an idea. Now here is the task for which I'd like you to write instructions:
<Task>
{{TASK}}
</Task>
Now write instructions for the AI assistant to follow in order to complete the task. Your instructions should be
complete and thorough. Do not write a summary or preamble - jump straight into the instructions."""
Cách dùng Metaprompt
import anthropic
client = anthropic.Anthropic()
def generate_prompt(task_description: str) -> str:
"""
Dùng metaprompt để tạo prompt chuyên nghiệp từ mô tả task đơn giản.
"""
# Inject task vào metaprompt
metaprompt = ANTHROPIC_METAPROMPT.replace("{{TASK}}", task_description)
response = client.messages.create(
model="claude-opus-4-5", # Dùng model mạnh nhất để tạo prompt
max_tokens=4000,
messages=[{"role": "user", "content": metaprompt}],
temperature=1.0, # Cao để sáng tạo hơn
)
return response.content[0].text
# Ví dụ: Tạo prompt cho customer service bot
task = """
Tạo một AI assistant cho bộ phận hỗ trợ khách hàng của công ty phần mềm kế toán Việt Nam.
Assistant cần:
- Trả lời câu hỏi về sản phẩm phần mềm
- Hướng dẫn cài đặt và sử dụng cơ bản
- Escalate các vấn đề phức tạp cho team kỹ thuật
- Luôn lịch sự, chuyên nghiệp, dùng tiếng Việt
"""
generated_prompt = generate_prompt(task)
print("=== PROMPT ĐƯỢC TẠO ===")
print(generated_prompt[:1000] + "...")
Ví dụ 2: Metaprompt cho nhiều loại task
TASK_EXAMPLES = [
{
"name": "Email Classifier",
"task": "Phân loại email khách hàng vào các categories: Complaint, Question, Feedback, Spam. Trả về category và confidence score."
},
{
"name": "Meeting Summarizer",
"task": "Tóm tắt ghi chú cuộc họp tiếng Việt thành: action items, decisions made, và next steps. Format có cấu trúc rõ ràng."
},
{
"name": "Code Reviewer",
"task": "Review Python code, chỉ ra bugs, security issues, và performance problems. Đề xuất cải thiện cụ thể với code examples."
},
{
"name": "Product Description Writer",
"task": "Viết mô tả sản phẩm e-commerce hấp dẫn từ danh sách thông số kỹ thuật. Tập trung vào benefits, không phải features."
},
]
for example in TASK_EXAMPLES:
print(f"
{'='*50}")
print(f"Task: {example['name']}")
print(f"Mô tả: {example['task'][:80]}...")
prompt = generate_prompt(example["task"])
print(f"Prompt được tạo ({len(prompt)} chars):")
print(prompt[:300] + "...")
Kỹ thuật nâng cao: Prompt Improvement Loop
Metaprompting kết hợp với eval để tự động cải thiện prompt:
IMPROVEMENT_PROMPT = """Bạn là chuyên gia prompt engineering. Nhiệm vụ: cải thiện prompt dưới đây.
PROMPT HIỆN TẠI:
{current_prompt}
KẾT QUẢ THỰC TẾ (các cases thất bại):
{failure_cases}
Phân tích vấn đề và viết lại prompt để:
1. Xử lý tốt hơn các failure cases
2. Giữ nguyên những gì đang hoạt động tốt
3. Rõ ràng hơn, ít mơ hồ hơn
Trả về prompt cải thiện trong tag <improved_prompt>."""
def improve_prompt(current_prompt: str, failure_cases: list) -> str:
"""Tự động cải thiện prompt dựa trên failure cases."""
# Format failure cases
failures_text = "
".join([
f"- Input: '{case['input']}'
Expected: '{case['expected']}'
Got: '{case['actual']}'"
for case in failure_cases
])
improvement_request = IMPROVEMENT_PROMPT.format(
current_prompt=current_prompt,
failure_cases=failures_text
)
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=3000,
messages=[{"role": "user", "content": improvement_request}],
temperature=0.7,
)
import re
text = response.content[0].text
match = re.search(r"(.*?) ", text, re.DOTALL)
return match.group(1).strip() if match else text
# Ví dụ workflow
initial_prompt = """Phân loại sentiment của text tiếng Việt.
Trả về: POSITIVE, NEGATIVE, hoặc NEUTRAL."""
failure_cases = [
{
"input": "Sản phẩm ổn nhưng giá cao quá, không biết có nên mua không",
"expected": "MIXED",
"actual": "NEUTRAL"
},
{
"input": "Trời ơi, sao mà tệ vậy!!!",
"expected": "NEGATIVE",
"actual": "NEUTRAL" # Claude bị confuse bởi dấu chấm than
},
]
improved = improve_prompt(initial_prompt, failure_cases)
print("Prompt cải thiện:")
print(improved)
Meta-Prompt để tạo System Prompts
SYSTEM_PROMPT_GENERATOR = """Tạo system prompt chuyên nghiệp cho AI assistant với các thông tin sau:
TÊN: {name}
MỤC ĐÍCH: {purpose}
TONE: {tone}
GIỚI HẠN: {constraints}
CONTEXT: {context}
System prompt cần bao gồm:
1. Định nghĩa vai trò rõ ràng
2. Hướng dẫn cụ thể về cách xử lý các tình huống
3. Những điều assistant KHÔNG được làm
4. Format output mong muốn
5. Cách xử lý khi không biết câu trả lời
Viết system prompt hoàn chỉnh, sẵn sàng dùng ngay."""
def create_system_prompt(name: str, purpose: str, tone: str = "professional",
constraints: str = "none", context: str = "") -> str:
prompt = SYSTEM_PROMPT_GENERATOR.format(
name=name,
purpose=purpose,
tone=tone,
constraints=constraints,
context=context
)
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=2000,
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
)
return response.content[0].text
# Tạo system prompt cho chatbot bán hàng
system_prompt = create_system_prompt(
name="Mia - Shopping Assistant",
purpose="Hỗ trợ khách hàng tìm sản phẩm và tư vấn mua hàng trên sàn thương mại điện tử",
tone="thân thiện, nhiệt tình, chuyên nghiệp",
constraints="Không tiết lộ thông tin nội bộ, không so sánh tiêu cực với đối thủ",
context="Sàn TMĐT Việt Nam, chuyên về điện tử và công nghệ"
)
print(system_prompt[:500] + "...")
Khi nào dùng Metaprompt?
- Bắt đầu dự án mới: Tạo prompt baseline nhanh thay vì viết từ đầu
- Scale prompt engineering: Một người có thể tạo hàng chục prompts chất lượng cao trong một ngày
- Onboard người mới: Người không biết prompt engineering vẫn có thể tạo prompts tốt
- A/B testing: Tạo nhiều biến thể prompt để test xem cái nào tốt hơn
Metaprompting không thay thế được sự hiểu biết sâu về task của bạn — nhưng nó là công cụ cực kỳ hiệu quả để bootstrap và iterate nhanh. Kết hợp với Building Evals để đánh giá và cải thiện prompts được tạo ra.
Bai viet co huu ich khong?
Bản quyền thuộc về tác giả. Vui lòng dẫn nguồn khi chia sẻ.






