为 Bob Shell 编写有效提示词
通过添加角色、上下文和示例为 Bob Shell 编写有效的提示词,然后将模糊的请求改进为带有错误处理并附带文档说明的 Python 代码。
当你的提示词明确指出任务、角色、上下文和示例时,IBM Bob Shell 可以生成更准确的代码。在本教程中,你将把一个模糊的请求重写为结构化提示词,然后对比两者的结果。
前提条件
要确认 Bob Shell 已安装,请打开终端并检查版本:
bob --version什么是提示词?
提示词是你向 Bob 提出的问题、请求或命令,用于获取你想要的输出。模糊的提示词会产生模糊的回答。好的提示词清晰、具体,并提供足够的上下文,以便 Bob 理解你想要什么。
提示词的组成部分
有效的提示词结合了以下组成部分:
| 组成部分 | 作用 | 示例 |
|---|---|---|
| 指令 | 告诉 Bob 要执行什么任务。强有力的指令可以减少歧义。 | "为这个 API 生成软件物料清单 (SBOM)。" |
| 角色 | 分配一个人格,以便 Bob 从该视角定制回答。 | "你是一位精通 Python 的高级软件工程师。" |
| 上下文 | 添加背景信息,例如场景、受众、语气或约束条件。 | "该函数在生产服务中运行,必须包含错误处理。" |
| 示例 | 展示你希望 Bob 遵循的模式、格式或风格。 | "遵循 @src/api/users.py 的结构。" |
| 提示 | 引导期望的回答形式,通常放在提示词末尾。 | "仅返回函数,以 def 开头。" |
你可以将提示词保存为自定义斜杠命令,以便重复使用并与团队共享。
什么是有效的提示词?
以下最佳实践可以帮助你为 Bob 编写有效的提示词。
- 具体说明: 你的提示词越具体,Bob 就越能理解你想要什么。与其问"如何编写 Python 函数?",不如问"如何编写一个接受数字列表并返回平均值的 Python 函数?"
- 提供上下文: 如果你的提示词涉及特定的项目或代码库,请提供一些相关背景。这有助于 Bob 生成与你的情况相关的回答。使用
@引用(如@src/main.py)将 Bob 指向特定文件。 - 拆解任务: 就像你在 backlog 中细化任务时一样,将复杂任务拆解为更小、更易管理的步骤。这可以帮助 Bob 生成更准确、更有针对性的回答。
- 包含示例: 如果你在添加新的 API,可以指导 Bob 查看代码库中写得好的 API 作为示例。你也可以在提示词中包含示例,向 Bob 展示你想要的格式或风格。
改进模糊的提示词
在本节中,你将为 Bob 编写一个模糊的提示词并查看输出结果。然后你将根据最佳实践改进提示词,以提高回答的质量。
目标是让 Bob 生成一个以 UTC 格式打印当前日期和时间的 Python 函数。该函数应有良好的文档说明并包含错误处理。
打开终端并以 Agent 模式启动交互式会话:
bob chat --mode agent输入以下提示词:
Write a Python function that prints the date and time.Bob 将生成一个回答,但它可能无法满足你的所有要求。
from datetime import datetime
def print_datetime():
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))该函数没有错误处理,并且打印的是本地时间,而不是 UTC。
输入 /clear 重置对话历史,使第一次的回答不会影响下一次回答:
/clear使用提示词组成部分改进你的提示词:
- 角色: "You are a senior software engineer with expertise in Python 3."
- 指令: "Write a Python 3 function that prints the current date and time in UTC format."
- 上下文: "The function should be well documented and include error handling."
- 示例: 指引 Bob 参考权威来源,例如 Real Python 异常处理最佳实践。
输入改进后的提示词:
You are a senior software engineer with expertise in Python 3.
Write a Python 3 function that prints the current date and time in UTC format.
The function should be well documented and include error handling,
modeled after https://realpython.com/ref/best-practices/exception-handling/."""
utc_datetime.py — Utility for printing the current UTC date and time.
"""
from datetime import datetime, timezone
def print_utc_datetime() -> None:
"""Print the current date and time in UTC (ISO 8601 format).
Retrieves the current moment from the system clock, attaches UTC timezone
info, formats it as an ISO 8601 string, and writes it to stdout.
Returns:
None
Raises:
OSError: If the system clock cannot be read (e.g., hardware failure or
OS-level permission restrictions).
ValueError: If datetime formatting unexpectedly fails (e.g., year out
of the supported range).
Example:
>>> print_utc_datetime()
Current UTC date and time: 2025-07-15T13:42:00.123456+00:00
"""
try:
now_utc: datetime = datetime.now(tz=timezone.utc)
except OSError as exc:
# System clock is unavailable or inaccessible.
raise OSError("Failed to read the system clock.") from exc
try:
formatted: str = now_utc.isoformat()
except ValueError as exc:
# Extremely unlikely, but guard against out-of-range datetime values.
raise ValueError(
f"Failed to format the datetime object: {now_utc!r}"
) from exc
print(f"Current UTC date and time: {formatted}")
if __name__ == "__main__":
print_utc_datetime()改进后的提示词生成了一个使用 UTC、包含文档字符串并处理了异常的函数。
IBM SkillsBuild
IBM SkillsBuild 是一个免费的在线学习平台,提供关于如何为 Bob 等 AI 助手编写有效提示词的课程。
要了解更多关于良好提示词编写的内容,请学习 Craft Precise Prompts for AI Models 课程。
后续步骤
在本教程中,你学习了有效提示词的组成部分,并将一个模糊的提示词改进为能够可靠生成所需输出的提示词。
继续学习创建和使用技能,将一个写得好的提示词转化为 Bob 可以自动应用的可重用、可重复的工作流程。