第 4 步:构建
我的理解
有了前三步的铺垫(目标清晰、技术路线验证、AI 能力测试兜底),实际构建阶段的核心不再是复杂代码,而是在 prompt 中合理利用 docstring 提供精确上下文,让 AI 专注于“待实现”的部分即可。本课还着重强调了可观测性的不可或缺:自动化工具一旦投入运行,“失败通知”配置是确保系统不会静默失败的最后防线——错误的结果比没有结果危害更大,因为它会悄无声息地向下游传播。遵循“尽早快速失败”原则,在链路的每个节点主动暴露问题,比等到最终用户发现错误时再修复代价低得多。
相关链接
- Ch04-L10 心得 4 风险管理 — 构建阶段引出的配套学习,系统讲解如何防御不确定性和控制成本
- Ch04-L08 学习 3 评估机制 — 已有测试使构建更有底气,出错时能立刻捕获并定位到最近的改动
- Ch04-L06 学习 2 面向 AI 的文档管理 — docstring 在构建 prompt 中的关键作用,本课是文档管理学习的直接应用
- Ch06-L07 重访周业务复盘 用Agent访问私有数据 — 另一个端到端构建自动化工具的案例,可横向对比构建思路与可观测性实践
原文
Lesson 34 of 68 第 4 步:构建 / Step 4: Build
有了前面所做的准备工作,以及我们目前学到的推荐实践,剩下的构建过程其实相当直接。
练习:
=======================================================
你会如何构造这个提示词?
=======================================================
示例提示词
=======================================================
我们已经有了如下的核心函数。请编写一个 Google Apps Script 函数,实现:
-
读取最近的 10 封 Gmail 邮件
-
调用该函数来判断这些邮件是否与 GenAI 相关
-
对于相关的邮件,将其标记为重要(important),以确保进入优先收件箱。我希望收到这些邮件的通知。
无需实现 isGenAIEmail 函数。
/**
-
Determines if a given email is related to Generative AI by using OpenAI’s GPT-3.5-turbo model.
-
@param {Object} email - The email object containing body as a string representing the email content.
-
@returns {boolean} - Returns true if the email is identified as related to Generative AI, otherwise false.
-
- The email object should have a body field that contains the text content of the email.
-
- The function sends up to the first 10,000 characters of the email body to the OpenAI API for classification.
-
- The classification is done by prompting the API to return “yes” or “no” based on relevance to Generative AI.
-
- Ensure the YOUR_OPENAI_API_KEY placeholder is replaced with an actual API key before using this function.
*/
function isGenAIEmail(email) {
// Function implementation
}
=======================================================
示例输出(仅代码)
=======================================================
/**
- Reads the latest 10 Gmail messages and marks the relevant ones as important if they are related to GenAI.
*/
function markGenAIMessagesAsImportant() {
// Get the 10 latest threads
var threads = GmailApp.getInboxThreads(0, 10);
// Loop through each thread
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
// Loop through each message in the thread
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var email = {
body: message.getBody()
};
// Check if the email is related to Generative AI
if (isGenAIEmail(email)) {
// Mark the message as important
message.markImportant();
console.log(`Marked as important: Subject - ${message.getSubject()}`);
}
}
}
}
=======================================================
如果我们真的运行这段代码,会发现它会抛出一些错误。只需把错误信息复制粘贴给 ChatGPT,就能得到正确的程序。
接下来我们可以进一步询问 ChatGPT 或 Copilot:如何让它每 5 分钟运行一次,并设置 Gmail 应用仅在收到重要邮件时才提醒。
练习:
=======================================================
在 ChatGPT 的帮助下,为这个 Google Apps Script 项目设置定时触发器,并配置 Gmail 通知。
=======================================================
在设置触发器时,你可能会看到“Failure notification settings”(失败通知设置):
这是让你能够发现任何错误的关键设置。如果没有正确配置,流水线可能会静默失败,而且很难察觉——因为也有可能是 GPT 没有正常工作。如果还有其他工具使用了该工具的输出,情况会更糟:错误会被悄无声息地向下游传递,造成更大的影响。因此,请始终保持可观测性(observability)的思维,就像我们对 GPT 所做的那样。如果注定要失败,那就尽早失败、快速失败,避免影响到其他环节。
最终项目的可运行代码可在此处查看:https://script.google.com/d/1nh-56tMzreWtzg-Rl9a3LJNhM4t2MjyovRKWp6AJNXHCMoFKecWVbm2_/edit?usp=sharing。
English Original
With all the preparations we did, and recommended practices we learned so far, it’s actually quite straightforward to do the remaining building process.
Exercise:
=======================================================
How would you construct the prompt?
=======================================================
Example prompt
=======================================================
We have our core function as below. Write a Google Apps Script function to:
-
Read 10 latest gmail messages
-
Call the function to check whether they are related with GenAI
-
For those related, mark it as important to make sure it enters the priority inbox. I want to receive notifications on those emails.
No need to implement the isGenAIEmail function.
/**
-
Determines if a given email is related to Generative AI by using OpenAI’s GPT-3.5-turbo model.
-
@param {Object} email - The email object containing body as a string representing the email content.
-
@returns {boolean} - Returns true if the email is identified as related to Generative AI, otherwise false.
-
- The email object should have a body field that contains the text content of the email.
-
- The function sends up to the first 10,000 characters of the email body to the OpenAI API for classification.
-
- The classification is done by prompting the API to return “yes” or “no” based on relevance to Generative AI.
-
- Ensure the YOUR_OPENAI_API_KEY placeholder is replaced with an actual API key before using this function.
*/
function isGenAIEmail(email) {
// Function implementation
}
=======================================================
Example output (code only)
=======================================================
/**
- Reads the latest 10 Gmail messages and marks the relevant ones as important if they are related to GenAI.
*/
function markGenAIMessagesAsImportant() {
// Get the 10 latest threads
var threads = GmailApp.getInboxThreads(0, 10);
// Loop through each thread
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
// Loop through each message in the thread
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var email = {
body: message.getBody()
};
// Check if the email is related to Generative AI
if (isGenAIEmail(email)) {
// Mark the message as important
message.markImportant();
console.log(`Marked as important: Subject - ${message.getSubject()}`);
}
}
}
}
=======================================================
If we actually run the code, we will find it will throw some error. Simply copy pasting the error message to ChatGPT will give us the correct program.
Then we could further ask ChatGPT or Copilot on how to make it run every 5 minutes, and set up the Gmail app to only notify when an important email arrives.
Exercise:
=======================================================
With ChatGPT’s help, set up the timer trigger for the Google Apps Script project. And set up the Gmail notification.
=======================================================
When set up the trigger, you may see “Failure notification settings”:
That’s the key setting that gives you visibility into any errors. Without this set properly, the pipeline may fail silently, and it’s hard to notice because it’s also possible that GPT was not doing its job. Things will become worse if there are any tools using the result of this tool. It will propagate the mistake silently and cause big impacts. So always have a mindset of observability, like what we did for GPT. If it needs to fail, fail early and fail fast before it impacts other things.
A working code of the final project can be found in https://script.google.com/d/1nh-56tMzreWtzg-Rl9a3LJNhM4t2MjyovRKWp6AJNXHCMoFKecWVbm2_/edit?usp=sharing.