大模型入门实战指南:环境配置与提示词工程
前言
在深入大模型学习之前,了解基础概念至关重要。目前主流的大语言模型主要分为两类:Base LLM(基座模型)和 Instruction Tuned LLM(指令微调模型)。基座模型主要用于预测下一个 token,而指令微调模型则经过专门训练,能够更准确地理解并执行用户的指令。本文将基于 Python 环境,带你完成从环境搭建到基础提示词工程(Prompt Engineering)的实战演练。
大模型的基础配置与提示词工程核心原则。内容涵盖环境搭建、OpenAI API 调用方法、以及通过清晰指令和结构化输出优化模型效果的技术实践。重点讲解了如何通过具体示例实现文本总结、JSON 格式生成及多步骤任务处理,并探讨了机器思考时间对复杂问题解答的重要性,最后提及了模型幻觉现象及其应对策略。

在深入大模型学习之前,了解基础概念至关重要。目前主流的大语言模型主要分为两类:Base LLM(基座模型)和 Instruction Tuned LLM(指令微调模型)。基座模型主要用于预测下一个 token,而指令微调模型则经过专门训练,能够更准确地理解并执行用户的指令。本文将基于 Python 环境,带你完成从环境搭建到基础提示词工程(Prompt Engineering)的实战演练。
建议通过 DeepLearning.AI 等权威平台进行基础学习,这些平台通常内置 Jupyter Notebook 环境,方便读者边看视频边编写代码,提升学习效率。
如果你选择在本机运行代码,首先需要安装必要的依赖库。OpenAI 的官方 Python 客户端是核心工具。
pip install openai python-dotenv
openai:用于与 OpenAI API 进行交互。python-dotenv:用于管理环境变量,避免将敏感信息(如 API Key)硬编码在代码中。为了安全起见,建议使用 .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)输出。
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)
解析:
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)能有效提升复杂问题的解答质量。
大模型在回答问题时可能会出现事实性错误,这被称为'幻觉'。这是当前技术的正常现象。应对策略包括:
本文仅涵盖了基础配置与简单提示词。后续章节将深入探讨:
希望本指南能为你的大模型学习之路打下坚实基础。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online