提示工程基础
了解提示工程的基础知识,以便有效地与大型语言模型交互并引导其生成结果。
提示工程基础 是 CoddyKit 上的免费 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Powered SaaS: Stripe + Auth + Billing + Deploy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Prompt Engineering?
Welcome! In this lesson, we'll dive into Prompt Engineering, a crucial skill for working with Artificial Intelligence, especially Large Language Models (LLMs).
It's essentially the art of crafting effective inputs (prompts) to get the best possible outputs from an AI model.
Guiding the AI
Think of an LLM as a brilliant but sometimes vague assistant. Without clear instructions, it might give you generic or unhelpful answers.
Prompt engineering helps us:
- Get precise, relevant responses.
- Control the AI's behavior and style.
- Unlock the full potential of AI tools.
Basic Prompt Structure
A good prompt usually has two main parts: a clear instruction and relevant context.
The instruction tells the AI what to do, and the context gives it the information to work with. Try running this basic example:
def send_prompt(prompt_text):
# In a real app, this would call an AI API
print(f"Sending to AI:\n---\n{prompt_text}\n---")
if __name__ == "__main__":
instruction = "Summarize the following text."
context = "The quick brown fox jumps over the lazy dog."
full_prompt = f"{instruction}\n\nText: {context}"
send_prompt(full_prompt)Clarity is Key
Vague prompts lead to vague answers. Be as precise as possible about what you want, including length, tone, and specific details.
Compare these two prompts:
def send_prompt(prompt_text):
print(f"Sending to AI:\n---\n{prompt_text}\n---")
if __name__ == "__main__":
# Vague prompt
vague_prompt = "Write about dogs."
send_prompt(f"Vague prompt:\n{vague_prompt}")
# Specific prompt
specific_prompt = (
"Write a short, factual paragraph (50-70 words) "
"about the average lifespan and common breeds of domestic dogs. "
"Use a friendly, informative tone."
)
send_prompt(f"\nSpecific prompt:\n{specific_prompt}")Role-Playing with Prompts
You can guide the AI to adopt a specific persona or role. This helps tailor the response style and content to your needs.
The AI will try to "think" like the persona you assign. See this example:
def send_prompt(prompt_text):
print(f"Sending to AI:\n---\n{prompt_text}\n---")
if __name__ == "__main__":
persona_prompt = (
"Act as a seasoned travel blogger. "
"Write a catchy Instagram caption for a photo "
"of a beautiful sunset over Santorini, Greece. "
"Include relevant emojis and hashtags."
)
send_prompt(persona_prompt)Learning from Examples
Sometimes, showing the AI a few input-output examples helps it understand the desired pattern better than just instructions. This technique is called few-shot learning.
It's great for tasks like classification or rephrasing:
def send_prompt(prompt_text):
print(f"Sending to AI:\n---\n{prompt_text}\n---")
if __name__ == "__main__":
few_shot_prompt = (
"Classify the following food items as Fruit or Vegetable:\n\n"
"Input: Apple -> Output: Fruit\n"
"Input: Carrot -> Output: Vegetable\n"
"Input: Banana -> Output: " # AI would complete this
)
send_prompt(few_shot_prompt)Control the Output Format
You can instruct the AI to return information in a specific structure, such as bullet points, numbered lists, tables, or even JSON.
This is very useful for integrating AI output into applications:
def send_prompt(prompt_text):
print(f"Sending to AI:\n---\n{prompt_text}\n---")
if __name__ == "__main__":
format_prompt = (
"List 3 benefits of cloud computing in a numbered list format.\n\n"
"1." # AI would complete this
)
send_prompt(format_prompt)
json_format_prompt = (
"Provide details for a person named 'Jane Doe' "
"who is 28 years old and works as a 'Software Engineer'. "
"Output this as a JSON object with keys 'name', 'age', and 'occupation'."
)
send_prompt(f"\n{json_format_prompt}")Refine & Iterate
Prompt engineering is rarely a one-shot deal. You'll often need to refine your prompts based on the AI's initial responses.
Think of it as a conversation: start simple, analyze the response, then add or change instructions to get closer to your desired outcome.
What to Avoid
Keep these in mind to avoid common prompt engineering mistakes:
- Vagueness: "Tell me about cars" is too broad.
- Overloading: Too many complex instructions at once.
- Ambiguity: Words with multiple meanings without context.
- Lack of Context: Assuming the AI knows your specific internal information.
Test Your Prompt Skills
Which of the following prompts is the most effective for asking an AI to generate a short, positive review for a new coffee shop called "Bean There, Done That"?
Recap: Guiding Your AI
You've learned the fundamentals of prompt engineering!
- Be Clear: Use precise instructions.
- Provide Context: Give the AI necessary information.
- Set Persona: Guide the AI's role and tone.
- Use Examples: Show desired patterns with few-shot learning.
- Specify Format: Control the output structure.
- Iterate: Refine your prompts for better results.
Mastering these techniques will significantly improve your AI interactions!
常见问题解答
「提示工程基础」课时是免费的吗?
是的 — 「提示工程基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程的其余内容,请升级到 CoddyKit PRO。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
「提示工程基础」这节课中我会学到什么?
了解提示工程的基础知识,以便有效地与大型语言模型交互并引导其生成结果。 你通过在浏览器中直接运行的动手代码来练习 AI Powered SaaS: Stripe + Auth + Billing + Deploy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Powered SaaS: Stripe + Auth + Billing + Deploy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「提示工程基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课中编写并运行代码吗?
能。每节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。