大模型入门实战指南:环境配置与提示词工程
前言
在深入大模型学习之前,了解基础概念至关重要。目前主流的大语言模型主要分为两类:Base LLM(基座模型)和 Instruction Tuned LLM(指令微调模型)。基座模型主要用于预测下一个 token,而指令微调模型则经过专门训练,能够更准确地理解并执行用户的指令。本文将基于 Python 环境,带你完成从环境搭建到基础提示词工程(Prompt Engineering)的实战演练。
资源、配置与环境
1. 学习平台推荐
建议通过 DeepLearning.AI 等权威平台进行基础学习,这些平台通常内置 Jupyter Notebook 环境,方便读者边看视频边编写代码,提升学习效率。
2. 本地环境搭建
如果你选择在本机运行代码,首先需要安装必要的依赖库。OpenAI 的官方 Python 客户端是核心工具。
pip install openai python-dotenv
openai:用于与 OpenAI API 进行交互。python-dotenv:用于管理环境变量,避免将敏感信息(如 API Key)硬编码在代码中。
3. API 密钥配置
为了安全起见,建议使用 .env 文件存储 API 密钥,并在代码中动态加载。
import openai
import os
from dotenv import load_dotenv, find_dotenv
# 加载环境变量
_ = load_dotenv(find_dotenv())
# 设置 OpenAI API 密钥
openai.api_key = os.getenv('OPENAI_API_KEY')
这段代码的作用是读取项目根目录下的 .env 文件中的 OPENAI_API_KEY 变量,并将其赋值给 OpenAI 库的配置项,确保后续调用服务时具备认证权限。
核心函数封装
为了方便后续调用,我们定义一个通用的辅助函数 get_completion。该函数封装了请求发送、参数配置及结果提取的逻辑。
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # 控制输出的随机性,0 表示最确定
)
return response.choices[0].message["content"]
参数说明:
prompt:用户输入的提示信息。model:使用的模型名称,默认为gpt-3.5-turbo。temperature:温度参数。值越低(如 0),输出越稳定、一致;值越高,输出越具有创造性但可能不稳定。
提示词工程原则一:清晰具体的指令
大模型对指令的理解能力依赖于输入的质量。编写清晰、具体的指令可以显著减少无关或错误的响应。
示例:文本总结
以下代码演示了如何通过明确指令让模型总结长文本。
text = f"""
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)
解析:
- 定义了一段关于如何编写有效提示的文本。
- 构建了一个新的
prompt,要求模型总结用三重反引号括起来的文本。 - 调用
get_completion生成单句总结。
此示例证明了提供足够的上下文(Context)比简短的指令更能引导模型输出高质量内容。
提示词工程原则二:结构化输出
为了让程序更好地处理模型返回的数据,我们可以要求模型以特定格式(如 JSON、HTML)输出。
示例:JSON 格式生成
prompt = f"""
Generate a list of three made-up book titles along \
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
解析:
- 明确要求生成三个虚构书籍信息。
- 指定 JSON 格式及键名(
book_id,title,author,genre)。 - 这种结构化输出便于后续代码直接解析为对象使用。
指令格式化与逻辑判断
除了总结,我们还可以利用大模型进行文本重组和逻辑判断。
示例:步骤化指令提取
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \
water boiling. While that's happening, \
grab a cup and put a tea bag in it. Once the water is \
hot enough, just pour it over the tea bag. \
Let it sit for a bit so the tea can steep. After a \
few minutes, take out the tea bag. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write "No steps provided."
"""{text_1}"""
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
对比测试: 如果输入一段描述性文本(如风景描写),模型应识别出其中不包含指令。
text_2 = f"""
The sun is shining brightly today, and the birds are \
singing. It's a beautiful day to go for a \
walk in the park...
"""
# 使用相同的 prompt 结构
# 预期输出:No steps provided.
进阶:给予机器思考时间
对于复杂任务,分步指令(Chain of Thought)能显著提升准确性。这被称为'给机器思考时间'。
示例:多步骤任务处理
text = f"""
In a charming village, siblings Jack and Jill set out on \
a quest to fetch water from a hilltop \
well. As they climbed, singing joyfully, misfortune \
struck—Jack tripped on a stone and tumbled \
down the hill, with Jill following suit. \
Though slightly battered, the pair returned home to \
comforting embraces. Despite the mishap, \
their adventurous spirits remained undimmed, and they \
continued exploring with delight.
"""
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple \
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \
keys: french_summary, num_names.
Separate your answers with line breaks.
Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
解析: 通过明确列出 1、2、3、4 步操作,模型会按顺序执行,避免了遗漏关键步骤,最终输出包含法语摘要和名字数量的 JSON 对象。
示例:数学逻辑验证
prompt = f"""
Determine if the student's solution is correct or not.
Question:
I'm building a solar power installation and I need \
help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations
as a function of the number of square feet.
Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 10x
Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000
"""
response = get_completion(prompt)
print(response)
解析: 此示例展示了模型作为'导师'的角色。通过提供问题和学生答案,模型需要运用逻辑推理来验证计算过程是否正确。这证明了在提示词中引入推理步骤(Thinking Process)能有效提升复杂问题的解答质量。
总结与注意事项
1. 幻觉现象(Hallucinations)
大模型在回答问题时可能会出现事实性错误,这被称为'幻觉'。这是当前技术的正常现象。应对策略包括:
- 优化提示词,要求模型引用来源。
- 结合检索增强生成(RAG)技术。
- 人工审核关键输出。
2. 最佳实践
- 明确角色:告诉模型它是谁(如'你是一个资深程序员')。
- 提供示例:Few-Shot Prompting 能显著提高效果。
- 迭代优化:根据输出不断调整提示词。
3. 下一步计划
本文仅涵盖了基础配置与简单提示词。后续章节将深入探讨:
- 对话历史管理与上下文窗口限制。
- 高级微调(Fine-tuning)流程。
- 企业级应用架构设计。
希望本指南能为你的大模型学习之路打下坚实基础。


