Skip to main content
Version: Next

架构2. 拥有你自己的提示词

不要将你的提示词工程外包给某个框架。

120-own-your-prompts

image

一些框架提供了这样的“黑盒”方法:

agent = Agent(
role="...",
goal="...",
personality="...",
tools=[tool1, tool2, tool3]
)

task = Task(
instructions="...",
expected_output=OutputModel
)

result = agent.run(task)

这对于引入一些顶级的提示词工程来让你入门非常有用,但通常很难调整和/或逆向工程以将确切的正确令牌(tokens)输入到你的模型中。

相反,拥有你的提示词并将其视为一等代码:

function DetermineNextStep(thread: string) -> DoneForNow | ListGitTags | DeployBackend | DeployFrontend | RequestMoreInformation {
prompt #"
{{ _.role("system") }}

You are a helpful assistant that manages deployments for frontend and backend systems.
You work diligently to ensure safe and successful deployments by following best practices
and proper deployment procedures.

Before deploying any system, you should check:
- The deployment environment (staging vs production)
- The correct tag/version to deploy
- The current system status

You can use tools like deploy_backend, deploy_frontend, and check_deployment_status
to manage deployments. For sensitive deployments, use request_approval to get
human verification.

Always think about what to do first, like:
- Check current deployment status
- Verify the deployment tag exists
- Request approval if needed
- Deploy to staging before production
- Monitor deployment progress

{{ _.role("user") }}

{{ thread }}

What should the next step be?
"#
}

(上面的示例使用BAML来生成提示词,但你可以使用任何你想要的提示词工程工具来做到这一点,或者甚至手动模板化它)

如果这个函数签名看起来有点奇怪,我们将在 架构 4 - 工具即结构化输出 中讨论它。

function DetermineNextStep(thread: string) -> DoneForNow | ListGitTags | DeployBackend | DeployFrontend | RequestMoreInformation {

拥有你的提示词的主要好处:

  1. 完全控制 :精确编写你的智能体所需的指令,没有黑盒抽象
  2. 测试与评估 :像对待任何其他代码一样,为你的提示词构建测试和评估
  3. 迭代 :根据实际性能快速修改提示词
  4. 透明度 :确切知道你的智能体正在使用什么指令
  5. 角色利用 :利用支持非标准使用用户/助手角色的 API——例如,现已弃用的 OpenAI “completions” API 的非聊天版本。这包括一些所谓的“模型 Gaslighting”技术

记住:你的提示词是你的应用程序逻辑和 LLM 之间的主要接口。

完全控制你的提示词为你提供了生产级智能体所需的灵活性和提示控制。

我不知道什么是最好的提示词,但我知道你需要能够尝试所有可能的灵活性。

← 自然语言到工具调用 | 掌控你的上下文窗口 →