From 2059b1775d0d11501449a2e054cad2e3ef41de89 Mon Sep 17 00:00:00 2001 From: TheSisb Date: Fri, 27 Oct 2023 14:51:21 -0500 Subject: [PATCH] chore: improve github autoresponder (#3568) * chore: prompt the bot with its name * chore: dont autorespond if answer unknown, improve date and score value * chore: pr review fixes * fix: make the bot a little more conversational * chore: minor extra sentence to prompt self serve --------- Co-authored-by: Si Taggart --- packages/paste-website/src/pages/api/ai.ts | 10 ++--- tools/github/autoresponder.ts | 47 +++++++++++++++++----- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/packages/paste-website/src/pages/api/ai.ts b/packages/paste-website/src/pages/api/ai.ts index 988c32cff9..d9099a1996 100644 --- a/packages/paste-website/src/pages/api/ai.ts +++ b/packages/paste-website/src/pages/api/ai.ts @@ -146,11 +146,11 @@ export default async function handler(req: NextRequest): Promise - `- [${item.heading}](${item.path}) (updated: ${ - item.meta.updatedAt - }, similarity score: ${item.similarity.toFixed(2)})`, + `- [${item.heading}](${item.path}) (updated: ${dateFormatter.format( + new Date(item.meta.updatedA as string), + )}, similarity score: ${Math.round(item.similarity * 100)}%)`, ) // Convert to string .join("\n") @@ -106,12 +112,8 @@ const writeAnswerToDiscussion = async (discussionId: string, body: string): Prom } }; -const commentHeader = ` - **Disclaimer:** This is a very experimental bot using OpenAI's GPT-4. The answers may not be correct, a human will review the answer and update it if necessary. - - --- - -`; +const commentSeparator = "\n\n---\n\n"; +const commentHeader = `**Disclaimer:** This is a very experimental bot using OpenAI's GPT-4. The answers may not be correct, a human will review the answer and update it if necessary.`; // @ts-expect-error deno const similarDiscussions = await getSimilarDiscussions(API_SECRET, DISCUSSION_BODY); console.log("similar discussions:", similarDiscussions); @@ -120,7 +122,32 @@ console.log("similar discussions:", similarDiscussions); const answerFromAi = await getAnswerFromAi(API_SECRET, DISCUSSION_BODY); console.log("response from AI:", answerFromAi.trim().slice(0, 300)); -const fullBody = `${commentHeader}${answerFromAi}\n\n---\n\nHere are some similar discussions:\n\n${similarDiscussions}`; +const hasAnswerFromAi = !answerFromAi.includes("Sorry, I don't know how to help with that."); +const hasSimilarDiscussions = similarDiscussions.length > 0 && similarDiscussions !== "No similar discussions found."; + +// if you don't have an answer from AI AND you don't have similar discussions, don't bother commenting +if (!hasSimilarDiscussions && !hasAnswerFromAi) { + // @ts-expect-error deno + Deno.exit(0); +} + +let fullBody = `${commentHeader}`; + +// answer and similar discussions +if (hasAnswerFromAi && hasSimilarDiscussions) { + const similarDiscussionPrefix = + "\n\nI also did a search, and I managed to find these other Discussions that might be similar or related to your question. Give them a read to see if they answer your question. If they do, head back here and update this discussion and mark it as answered, pointing others to the related discussion:\n\n"; + + fullBody = `${fullBody}${commentSeparator}${answerFromAi}${similarDiscussionPrefix}${similarDiscussions}`; +} + +// No answer, but similar discussions. +if (!hasAnswerFromAi && hasSimilarDiscussions) { + const similarDiscussionPrefix = + "\n\nI did do a search though, and I managed to find these other Discussions that might be similar or related to your question. Give them a read to see if they answer your question. If they do, head back here and update this discussion and mark it as answered, pointing others to the related discussion:\n\n"; + + fullBody = `${fullBody}${commentSeparator}${answerFromAi}${similarDiscussionPrefix}${similarDiscussions}`; +} // @ts-expect-error deno const commentId = await writeAnswerToDiscussion(DISCUSSION_NODE_ID, fullBody);