Part04:提示词示例

提示词实战:从基础到进阶的七大应用场景

上一节我们介绍了提示词的基本概念,本节将通过七个具体的应用场景,展示如何通过精心设计的提示词来执行不同类型的任务。学习概念的最佳方式往往是通过实例,以下示例将带你深入了解提示工程的实际应用。

文本概括

文本摘要是自然语言生成的标准任务之一。语言模型最有前景的应用之一,就是将复杂的文章和概念概括成简洁易读的摘要。

基础摘要任务

我们可以尝试使用提示词进行一个基本的摘要任务。注意这里使用了"A:"作为显式提示格式,告诉模型接下来期望有一个答案。

提示词:

Explain antibiotics

A:

输出:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

进阶控制:一句话总结

如果你觉得模型提供的信息过多,可以指导模型用一句话总结相关内容,从而进一步提炼信息。

提示词:

Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

Explain the above in one sentence:

输出:

Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

信息提取

虽然语言模型主要用于自然语言生成,但它们在分类和信息提取等自然语言处理任务上同样表现出色。

示例:提取特定产品名

这是一个从给定段落中提取特定信息的提示词示例。

提示词:

Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.

Mention the large language model based product mentioned in the paragraph above:

输出:

The large language model based product mentioned in the paragraph above is ChatGPT.

你可以通过多种方法改进上述结果,但这已经展示了通过简单指示让模型执行不同任务的强大能力。

问答

让模型给出具体回答的最佳方法之一是改善提示词的格式。结合指令、上下文、输入和输出指示,通常能获得更好的结果。

结构化提示词示例

以下示例展示了更结构化的提示词格式,包含明确的上下文和约束条件。

提示词:

Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

Question: What was OKT3 originally sourced from?

Answer:

输出:

Mice.

文本分类

作为提示词工程师,你需要擅长提供清晰的指令。但在更困难的用例中,仅仅提供指令是不够的,这时你需要考虑提供示例(Few-Shot Prompting)来指定正确的行为。

基础分类

提示词:

Classify the text into neutral, negative or positive.

Text: I think the food was okay.

Sentiment:

输出:

Neutral

进阶分类:指定格式

如果你希望模型返回特定的标签格式(例如小写的"neutral"而不是首字母大写的"Neutral"),可以通过提供示例来引导模型。

提示词:

Classify the text into neutral, negative or positive.

Text: I think the vacation is okay.

Sentiment: neutral

Text: I think the food was okay.

Sentiment:

输出:

neutral

注意:具体性的重要性

如果在提示词中拼写错误(如将"neutral"写成"nutral"),模型可能会忽略你的虚构标签而输出它认为正确的标签。这强调了在提示词中提供准确信息和示例的重要性。

对话

你可以通过提示工程指导大语言模型扮演特定角色,指定其行为意图和身份。这在构建客户服务聊天机器人等对话系统时非常有用,也被称为"角色提示"。

技术性回答风格

提示词:

The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

Human: Hello, who are you?

AI: Greeting! I am an AI research assistant. How can I help you today?

Human: Can you tell me about the creation of blackholes?

AI:

输出:

Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

通俗易懂风格

如果你希望回答更易于理解,可以调整指令。

提示词:

The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.

Human: Hello, who are you?

AI: Greeting! I am an AI research assistant. How can I help you today?

Human: Can you tell me about the creation of black holes?

AI:

输出:

Sure! Black holes are created when a very massive star runs out of fuel and collapses in on itself. The gravity of the star is so strong that nothing, not even light, can escape its pull. This creates a point in space-time with an intense gravitational force called a black hole.

代码生成

大语言模型在代码生成方面非常有效,通过巧妙的提示,你可以执行大量的代码生成任务,甚至不需要指定编程语言。

简单程序

提示词:

/*

Ask the user for their name and say "Hello"

*/

输出:

let name = prompt("What is your name?");

console.log(Hello, ${name}!);

数据库查询生成

通过提供更多上下文(如数据库模式),模型可以生成复杂的查询。

提示词:

"""

Table departments, columns = [DepartmentId, DepartmentName]

Table students, columns = [DepartmentId, StudentId, StudentName]

Create a MySQL query for all students in the Computer Science Department

"""

输出:

SELECT StudentId, StudentName

FROM students

WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

推理

推理是大型语言模型面临的最困难任务之一,但对于复杂应用的涌现至关重要。对于涉及数学能力的推理任务,有时需要更高级的提示词工程技术。

基础算术

提示词:

What is 9,000 * 9,000?

输出:

81,000,000

复杂推理:分步思考

当模型直接回答出错时(例如判断奇偶数之和),可以通过指示模型"将问题分解为步骤"来提高准确率。

提示词:

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.

输出:

Odd numbers: 15, 5, 13, 7, 1

Sum: 41

41 is an odd number.

通过提供更好的指令并结合示例,可以显著提高模型在推理任务上的准确性。在后续章节中,我们将介绍更高级的提示工程概念和技术。

相关推荐
zzh940774 小时前
ChatGPT官网镜像站实战:高并发系统限流与熔断策略设计与演进
人工智能·gpt·chatgpt
点我头像干啥4 小时前
“慧眼识脊”AI助手:从零搭建MRI脊椎智能分割系统UNET
人工智能·深度学习·神经网络·机器学习
帐篷Li4 小时前
AI物联网自进化平台:让智能家居真正“智能“起来
人工智能·物联网·智能家居
nap-joker4 小时前
图神经网络与多模态DTI特征用于精神分裂症分类:来自脑网络分析与基因表达的洞见
人工智能·神经网络·分类·图卷积神经网络·图注意力机制·单分支、双分支图卷积网络
大刘讲IT4 小时前
数字化转型的认知博弈与管理重构
人工智能·经验分享·制造
another heaven4 小时前
【深度学习 超参调优】warmup
人工智能·深度学习
QYR_Jodie4 小时前
从环保政策驱动与贵金属回收需求驱动到稳健增长:全球硫代硫酸铵2026-2032年CAGR4.8%,2032年达6.93亿美元
大数据·人工智能·市场报告
万兴丶4 小时前
Unity 用AI自动开发游戏近一年----最新Cursor使用心得
人工智能·游戏·unity·cursor
dddaidai1234 小时前
Spring AI Alibaba(二)Hooks 和Interceptors
java·人工智能·spring