ChatGPT,Do_Not_Reply收件箱强制管理者
LLMs可以通过多种方式引入到Web应用程序中,在将其作为任何大问题的核心解决方案之前,我首先决定以完全无用的方式将其整合为一个Do_Not_Reply收件箱的调解员,这样做将会很有趣。
我正在开发一个以砾石骑行路线为重点的网络应用程序,https://sherpa-map.com,并且发现需要一个登录,这是我以前从未编程过的。在添加所有典型的、有用的注册条目,并在开发过程中大量利用ChatGPT的过程中,这是如此有用,以至于我甚至没费心安装Postman,我被一个非常搞笑的想法打动了,为什么不将ChatGPT API集成为一个服务,检查do_not_reply@sherpa-map.com的收件箱,并以讽刺、嘲笑的方式回复?
为了在与 Microsoft Graph 进行邮件发送/回复/接收的 Node JS 后端中实现此功能,需要几个关键部件:
- 一个监听并发送回复的功能。
// Function to listen for replies to the "do_not_reply" mailbox
async function listenForReplies() {
const client = graph.Client.init({
defaultVersion: 'v1.0',
authProvider: (done) => {
credential.getToken('https://graph.microsoft.com/.default')
.then((tokenResponse) => {
const accessToken = tokenResponse.token;
done(null, accessToken);
})
.catch((error) => {
done(error, null);
});
},
});
try {
// Fetch unread messages in the "do_not_reply" mailbox
const messages = await client.api(`/users/${userID}/messages`).filter("isRead eq false").get();
if (messages.value && messages.value.length > 0) {
for (const message of messages.value) {
// Send a snarky reply to the sender
await sendSnarkyReply(client, message, message.from.emailAddress.address);
// Mark the message as read
await client.api(`/users/${userID}/messages/${message.id}`).patch({ isRead: true });
}
}
} catch (error) {
console.error('Error listening for replies:', error);
}
2. 一个主要功能带有不断循环的结构,每隔一分钟通过激活“listenForReplies()”函数来检查收件箱。
async function main() {
while (true) {
await listenForReplies();
// Check for new emails every 60 seconds
await new Promise(resolve => setTimeout(resolve, 60 * 1000));
}
}
main();
3. “sendSnarkyReply()”函数格式化并发送响应给用户。
// Helper function to send a snarky reply
async function sendSnarkyReply(client, originalMessage, recipientEmail) {
const snarkyResponse = await generateSnarkyResponse(originalMessage);
const message = {
subject: 'RE: ' + originalMessage.subject,
body: {
contentType: 'HTML',
content: snarkyResponse,
},
toRecipients: [
{
emailAddress: {
address: recipientEmail,
},
},
],
};
try {
await client.api(`/users/${userID}/sendMail`).post({ message });
console.log('Snarky email sent successfully!');
} catch (error) {
console.error('Error sending snarky email:', error);
}
}
4. 最后是好东西,即“snarkyResponse()”函数,它解释了循环过程中未读电子邮件,将请求发送给OpenAI并返回响应。
async function generateSnarkyResponse(message) {
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const emailBody = message.body.content; // or message.body.content, depending on what you want to use
try {
const response = await axios.post(apiUrl, {
model: 'gpt-3.5-turbo', // Specify the model to use
messages: [
{
"role": "system",
"content": "You are a witty, sarcastic, and snarky AI trained to humorously reply to emails sent to a do_not_reply inbox. Use your humor to create a playful response while reminding the sender that they shouldn't have emailed this inbox in the first place."
},
{
"role": "user",
"content": emailBody
}
],
max_tokens: 250, // Adjust the desired length of the generated response
temperature: 0.7, // Adjust the level of creativity in the generated response
// n: 3, // Number of responses to generate for each prompt
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
});
const { choices } = response.data;
const reply = response.data.choices[0].message.content;
// Add the disclaimer text with <br> tags for line breaks and <strong> tags for bold text
const disclaimer = '<br><br><strong>Disclaimer: This is an OpenAI bot, instructed to act a bit snarky as people don\'t normally email a do-not-reply inbox. This reply was generated by an AI, not a human being.</strong>';
// Add the bot's signature
const signature = '<br><br>--<br><strong>Snarky Bot</strong><br>Do-Not-Reply Representative<br>Sherpa LLC<br><a href="https://www.sherpa-map.com">www.sherpa-map.com</a>';
const replyWithDisclaimerAndSignature = reply + disclaimer + signature;
return replyWithDisclaimerAndSignature;
} catch (error) {
console.error('Error generating snarky response:', error);
console.log('Error response data:', error.response.data); // Add this line
return 'Oops, something went wrong!';
}
}
虽然这里可能会有很多事情,但基本上它归结为将电子邮件内容转发给OpenAI,利用角色提示“您是一位机智、讽刺和尖刻的AI,训练有素,可以幽默回复发送到do_not_reply收件箱的电子邮件。利用您的幽默感创造出一种玩味的回应,提醒发件人他们不应该在第一时间发送邮件到此收件箱。”附加一些额外的细节,如免责声明和自己的签名。
这是我回复的一个测试验证电子邮件的示例:
以下是一个响应示例:
这个的娱乐价值之所以更高是因为它并不仅限于一次回复,每次回复电子邮件都会发送整个电子邮件串,因此完全有可能进行对话!