Tolerate haiku failures (#9738)

Co-authored-by: Eric <eashaw@sailsjs.com>
This commit is contained in:
Mike McNeil
2023-02-07 16:07:54 -06:00
committed by GitHub
co-authored by Eric
parent f4dd05d3ee
commit d80101924f
+40 -33
View File
@@ -191,45 +191,52 @@ module.exports = {
// ██║███████║███████║╚██████╔╝███████╗ ╚██████╗███████╗╚██████╔╝███████║███████╗██████╔╝
// ╚═╝╚══════╝╚══════╝ ╚═════╝ ╚══════╝ ╚═════╝╚══════╝ ╚═════╝ ╚══════╝╚══════╝╚═════╝
//
// Disabled on 02-06-2023 until we find out the cause of the 429 responses from OpenAI.
//
// Handle closed issue by commenting on it.
// let owner = repository.owner.login;
// let repo = repository.name;
// let issueNumber = issueOrPr.number;
// let newBotComment;
// let baseHeadersForGithubApiRequests = {
// 'User-Agent': 'Fleetie pie',
// 'Authorization': `token ${sails.config.custom.githubAccessToken}`
// };
let owner = repository.owner.login;
let repo = repository.name;
let issueNumber = issueOrPr.number;
let newBotComment;
let baseHeadersForGithubApiRequests = {
'User-Agent': 'Fleetie pie',
'Authorization': `token ${sails.config.custom.githubAccessToken}`
};
// if (!sails.config.custom.openAiSecret) {
// throw new Error('sails.config.custom.openAiSecret not set. Cannot respond with haiku.');
// }//•
if (!sails.config.custom.openAiSecret) {
throw new Error('sails.config.custom.openAiSecret not set. Cannot respond with haiku.');
}//•
// // Grab issue title and body, then truncate the length of the body so that it fits
// // within the maximum length tolerated by OpenAI. Then combine those into a prompt
// // generate a haiku based on this issue.
// let issueSummary = '# ' + issueOrPr.title + '\n' + _.trunc(issueOrPr.body, 2000);
// Grab issue title and body, then truncate the length of the body so that it fits
// within the maximum length tolerated by OpenAI. Then combine those into a prompt
// generate a haiku based on this issue.
let issueSummary = '# ' + issueOrPr.title + '\n' + _.trunc(issueOrPr.body, 2000);
// // Generate haiku
// // [?] https://beta.openai.com/docs/api-reference/completions/create
// let openAiReport = await sails.helpers.http.post('https://api.openai.com/v1/completions', {
// model: 'text-davinci-003',
// prompt: `You are an empathetic product designer. I will give you a Github issue with information about a particular improvement to Fleet, an open-source device management and security platform. You will write a haiku about how this improvement could benefit users or contributors. Be detailed and specific in the haiku. Do not use hyperbole. Be matter-of-fact. Be positive. Do not make Fleet (or anyone) sound bad. But be honest. If appropriate, mention imagery from nature, or from a glass city in the clouds. Do not give orders.\n\nThe first GitHub issue is:\n${issueSummary}`,
// temperature: 0.7,
// max_tokens: 256//eslint-disable-line camelcase
// }, {
// Authorization: `Bearer ${sails.config.custom.openAiSecret}`
// });
// newBotComment = openAiReport.choices[0].text;
// newBotComment = newBotComment.replace(/^\s*\n*[^\n:]*Haiku[^\n:]*:\s*/i,'');// « eliminate "*Haiku:" prefix line, if one is generated
// Generate haiku
// [?] https://beta.openai.com/docs/api-reference/completions/create
let openAiReport = await sails.helpers.http.post('https://api.openai.com/v1/completions', {
model: 'text-davinci-003',
prompt: `You are an empathetic product designer. I will give you a Github issue with information about a particular improvement to Fleet, an open-source device management and security platform. You will write a haiku about how this improvement could benefit users or contributors. Be detailed and specific in the haiku. Do not use hyperbole. Be matter-of-fact. Be positive. Do not make Fleet (or anyone) sound bad. But be honest. If appropriate, mention imagery from nature, or from a glass city in the clouds. Do not give orders.\n\nThe first GitHub issue is:\n${issueSummary}`,
temperature: 0.7,
max_tokens: 256//eslint-disable-line camelcase
}, {
Authorization: `Bearer ${sails.config.custom.openAiSecret}`
})
.tolerate((err)=>{
sails.log('Failed to generate haiku using OpenAI. Error details from OpenAI:',err);
});
// // Now that we know what to say, add our comment.
// await sails.helpers.http.post('https://api.github.com/repos/'+encodeURIComponent(owner)+'/'+encodeURIComponent(repo)+'/issues/'+encodeURIComponent(issueNumber)+'/comments',
// {'body': newBotComment},
// baseHeadersForGithubApiRequests
// );
if (!openAiReport) {// If OpenAI could not be reached…
newBotComment = 'I couldn\'t think of a haiku this time. (See fleetdm.com logs for more information.)';
} else {// Otherwise, haiku was successfully generated…
newBotComment = openAiReport.choices[0].text;
newBotComment = newBotComment.replace(/^\s*\n*[^\n:]*Haiku[^\n:]*:\s*/i,'');// « eliminate "*Haiku:" prefix line, if one is generated
}
// Now that we know what to say, add our comment.
await sails.helpers.http.post('https://api.github.com/repos/'+encodeURIComponent(owner)+'/'+encodeURIComponent(repo)+'/issues/'+encodeURIComponent(issueNumber)+'/comments',
{'body': newBotComment},
baseHeadersForGithubApiRequests
);
} else if (
(ghNoun === 'pull_request' && ['opened','reopened','edited'].includes(action))