Veni AI
人工智能

提示工程技术:LLM 优化指南

提示工程技术、思维链、少样本学习,以及企业级提示策略,帮助从 AI 模型中获取最大效率。

Veni AI Technical Team13 Ocak 20254 dk okuma
提示工程技术:LLM 优化指南

Prompt Engineering Techniques: LLM 优化指南

Prompt engineering 是通过系统化设计输入来获得 AI 模型期望输出的艺术与科学。正确的提示策略可将模型性能提升高达 50%。

Prompt Engineering 基础

Prompt 结构

一个有效的 Prompt 通常包含以下组件:

  1. System Instruction:模型角色与行为规则
  2. Context:与任务相关的背景信息
  3. Example(s):期望输出格式示例
  4. Task:具体请求或问题
  5. Output Format:期望的响应结构

基础 Prompt 模板

1[Role Definition] 2You are a {field of expertise} expert. 3 4[Context] 5{Relevant information} 6 7[Task] 8{Specific request} 9 10[Format] 11{Expected output format}

基础 Prompt 技巧

1. Zero-Shot Prompting(零样本提示)

在不提供示例的情况下直接定义任务:

Summarize the following text: {text}

适用场景:

  • 简单任务
  • 常识性问题
  • 分类任务

2. Few-Shot Prompting(少样本提示)

通过示例来定义任务:

1Text: "This product is great, I am very satisfied" 2Sentiment: Positive 3 4Text: "It was a terrible experience, I do not recommend it" 5Sentiment: Negative 6 7Text: "It's okay for the price" 8Sentiment: ?

最佳实践:

  • 通常 3-5 个示例即可
  • 覆盖各种边界情况
  • 示例顺序随机化

3. Chain-of-Thought (CoT)

逐步思考:

1Question: A store has 15 apples. 8 apples were sold, 2then 6 more apples arrived. How many apples are there? 3 4Let's think step by step: 51. Initially there are 15 apples 62. 8 apples sold: 15 - 8 = 7 apples left 73. 6 apples arrived: 7 + 6 = 13 apples 8 9Answer: 13 apples

4. Self-Consistency

为同一问题生成不同推理路径:

1Solve this problem in 3 different ways and choose the most consistent answer: 2 3[Problem]

5. Tree of Thoughts (ToT)

分支式思维树:

1Problem: {complex problem} 2 3Thought 1: {approach A} 4 → Sub-thought 1.1: {detail} 5 → Sub-thought 1.2: {detail} 6 7Thought 2: {approach B} 8 → Sub-thought 2.1: {detail} 9 10Evaluate and select the most suitable path.

高级技巧

ReAct(Reasoning + Acting)

思考与行动循环:

1Question: How many times larger is Istanbul's population than Paris? 2 3Thought: I need to find the population of both cities 4Action: [search] Istanbul population 5Observation: Istanbul population ~16 million 6 7Thought: Now I need to find Paris population 8Action: [search] Paris population 9Observation: Paris population ~2.2 million 10 11Thought: I can calculate the ratio 12Action: [calculate] 16 / 2.2 13Observation: 7.27 14 15Answer: Istanbul's population is approximately 7.3 times larger than Paris.

Constitutional AI Prompting

定义伦理与安全规则:

1System: You are a helpful assistant. 2 3Rules: 41. Do not generate harmful content 52. Do not share personal information 63. Do not help with illegal activities 74. Always be honest 8 9User question: {question}

Role Prompting

定义专业领域角色:

1You are a cybersecurity expert with 20 years of experience. 2You have worked as a CISO in Fortune 500 companies. 3You can explain technical details clearly and understandably. 4 5User's question: {question} 6## 提示优化策略 7 8### 1. 提高具体性 9 10❌ 不佳示例:

Write a blog post

✅ 良好示例:

Target audience: Software developers Topic: Docker container security Length: 1500-2000 words Tone: Technical but accessible Format: Introduction, 5 main sections, conclusion

1 2### 2. 确定输出格式 3

Provide your response in this JSON format: { "summary": "string", "key_points": ["string"], "next_steps": ["string"], "confidence_score": number }

1 2### 3. 负向提示 3 4指定不希望出现的行为: 5

Do NOT do the following:

  • Give speculative information
  • Make claims without citing sources
  • Lead the user
  • Give excessively long answers
1 2### 4. 使用分隔符 3 4明确不同部分: 5

###CONTEXT### {context information}

###TASK### {work to be done}

###FORMAT### {output format}

1 2## 模型相关优化 3 4### 针对 GPT 5
  • Use System message effectively
  • Activate JSON mode: response_format={"type": "json_object"}
  • Temperature: 0.7-1.0 for creative tasks, 0.1-0.3 for analytical
1 2### 针对 Claude 3
  • Use XML tags: <context>, <task>, <format>
  • Put important information at the end in long context
  • Evaluate Thinking tags
1 2### 针对 Gemini 3
  • Optimize for multimodal prompts
  • Up-to-date information with Grounding
  • Adjust Safety settings
1 2## 提示测试与迭代 3 4### A/B 测试框架 5

Prompt A: {version 1} Prompt B: {version 2}

Metrics:

  • Accuracy: %
  • Consistency: 1-5
  • Latency: ms
  • Token usage: #
1 2### 提示版本管理 3

prompt_v1.0: First version prompt_v1.1: Typo corrections prompt_v2.0: CoT added prompt_v2.1: Output format changed

1 2## 企业级提示管理 3 4### 创建提示库 5

/prompts /classification - sentiment_analysis.json - intent_detection.json /generation - blog_writer.json - code_reviewer.json /extraction - entity_extraction.json - data_parsing.json

1 2### 提示模板系统 3 4```python 5class PromptTemplate: 6 def __init__(self, template, variables): 7 self.template = template 8 self.variables = variables 9 10 def render(self, **kwargs): 11 return self.template.format(**kwargs) 12 13# Usage 14sentiment_prompt = PromptTemplate( 15 template="Analyze sentiment: {text}", 16 variables=["text"] 17)

常见错误与解决方案

错误 1:提示过于模糊

问题: 模型无法理解你的需求
解决方案: 添加明确的指令和示例

错误 2:提示过长

问题: 超出 Token 限制,成本升高
解决方案: 删除不必要信息,使用摘要

错误 3:指令冲突

问题: 模型行为不一致
解决方案: 优先级排序并澄清规则

错误 4:幻觉

问题: 模型生成不存在的信息
解决方案: 使用 Grounding、要求引用、降低 temperature

总结

提示工程是 AI 项目成功的关键因素之一。通过正确的技术和系统化的方法,你可以显著提升模型性能。

在 Veni AI,我们为企业客户构建定制化提示策略。欢迎联系我们获取专业支持。

İlgili Makaleler