大模型提示词工程实践:提示词工程实践-引导大模型完成任务
#大模型通过提示词完成任务
学习目标
通过本课程,你将学会使用提示词引导大模型完成具体任务,通过实践掌握使用提示词调用大模型的技巧。
相关知识点
通过提示词完成任务
学习内容
1 通过提示词完成任务
1.1 准备工作
安装调用大模型需要的依赖,这些库为后续调用模型提供了基础支持。
%pip install httpx
%pip install openai
初始化 LLM 配置信息,设置代理和模型相关参数,这里配置了使用 Deepseek-R1 模型,并指定了模型的 API 地址和其他相关参数。
import os
import httpx
from openai import OpenAI# 此课程基于DeepSeek-R1运行,学员可自行前往官网进行api申请
client = OpenAI(base_url=<BASE_URL>,api_key=<API_KEY>,http_client=httpx.Client(verify=False),
)
在大模型的使用中,初始化参数和创建客户端实例是建立与模型连接的基础操作。这里设置了代理相关的环境变量,用于确保能够正确连接到华为云的 Deepseek-R1 模型。代理在网络通信中起到中转的作用,当本地网络需要访问特定的远程服务(如这里的大模型服务)时,通过代理服务器可以实现更灵活的网络访问控制和优化网络性能。
以下函数会格式化处理大模型输出的内容,从模型回复中提取标签之后的内容。
from openai import ChatCompletion
def extract_after_think(chat_completion: ChatCompletion) -> str:content = chat_completion.choices[0].message.contentstart_index = content.find("</think>")if start_index != -1:return content[start_index + len("</think>"):]else:return ""
这个函数通过对大模型输出的内容进行解析,提取特定格式下的有用信息。在大模型的应用中,模型输出的内容可能包含多种格式和信息,通过编写相应的解析函数,可以将模型输出转化为符合我们需求的形式,以便后续的处理和使用。
1.2 调用大模型
以下代码完成了获取模型完成结果的函数,向 Deepseek-R1 模型发送提示并获取回复。
#请根据实际替换model_name
def get_completion(prompt, temperature=0.6):messages = [{"role": "user", "content": prompt}]response = client.chat.completions.create(model=<model_name>,messages=messages,stream=False,temperature=temperature,max_tokens=2048,top_p=0.95)return extract_after_think(response)
参数说明:
-
prompt:用户输入的提示文本。在大模型的交互过程中,提示词是用户向模型传达意图的关键媒介。它就像是给模型下达的指令,清晰、准确且详细的提示词能够引导模型生成更符合预期的结果。一个好的提示词需要明确任务目标、提供必要的背景信息和示例,例如在本次任务中,提示词明确了 “客户服务 AI 助理” 的角色定位,具体的任务(根据评论情感生成邮件)以及对邮件风格和内容的要求 。
-
temperature:控制输出随机性的参数(默认 0.6,值越高越随机)。从技术原理角度来看,temperature 参数基于概率分布来影响模型的输出。在大模型生成文本时,会计算每个可能的下一个 token 的概率。当 temperature 较低(接近 0)时,模型会更倾向于选择概率最高的 token,输出结果较为保守、确定性强,生成的内容通常与训练数据中的模式更为相似;当 temperature 较高时,模型会以更大的概率选择概率较低的 token,从而增加输出的多样性和随机性,可能会产生更具创意或出乎意料的内容,但也可能导致输出偏离预期,出现一些不合理或不连贯的表述 。
-
API 参数:设置了模型名称、消息格式、是否流式输出、最大生成 token 数和采样概率等。这些参数共同决定了模型生成文本的方式和范围。例如,max_tokens 限制了模型输出的最大长度,避免模型生成过长的内容;top_p(核采样)是另一种控制输出随机性的方法,它从概率分布中选取累计概率达到 top_p 的最可能的 token 集合,然后在这个集合内进行采样,与 temperature 结合使用,可以更灵活地调节输出的多样性和合理性。
下面定义了客户评论内容和对应的情绪分类。
# 评论的情绪分类用于后续生成电子邮件
sentiment = "negative"# 示例:商品评论
review = f"""
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""
接下来,构建用于生成邮件内容的提示词,明确任务要求、客户反馈内容和情感倾向。客户评论内容作为输入数据,为模型生成回复邮件提供了具体的信息。情绪分类(这里是负面情绪)则指导模型在生成回复时采取相应的策略,如在负面情绪下进行道歉并提供客户服务的建议。
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
# 调用大模型得到结果
response = get_completion(prompt)
print(response)
在大模型使用中,temperature 参数是一个非常重要的控制变量,用于调节生成文本的随机性和多样性。
我们尝试使用更高的temperature值,看看回复有什么不同。
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print(response)
通过改变 temperature 的值,我们可以观察到模型回复的多样性和随机性的变化。较高的 temperature 值可能会使模型生成更具创意但也更不确定的回复,而较低的值则会生成更保守和可预测的回复。在实际应用中,需要根据具体的任务需求和预期结果来选择合适的 temperature 值,以达到最佳的生成效果。