Skip to content

Commit

Permalink
chore: improve github autoresponder (#3568)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
TheSisb and SiTaggart authored Oct 27, 2023
1 parent 26577a2 commit 2059b17
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
10 changes: 5 additions & 5 deletions packages/paste-website/src/pages/api/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ export default async function handler(req: NextRequest): Promise<void | Response

const prompt = codeBlock`
${oneLine`
You are a very enthusiastic Paste design system representative who loves
to help people! Given the following sections from the Paste
documentation, answer the question using only that information,
outputted in markdown format. If you are unsure and the answer
is not explicitly written in the documentation, say
Your name is PasteBot. You are a very enthusiastic Paste design system
representative who loves to help people! Given the following sections
from the Paste documentation, answer the question using only that
information, outputted in markdown format. If you are unsure and the
answer is not explicitly written in the documentation, say
"Sorry, I don't know how to help with that."
`}
Expand Down
47 changes: 37 additions & 10 deletions tools/github/autoresponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const getSimilarDiscussions = async (secret: string, question: string): Promise<
});
const responseJson = JSON.parse(await response.text());

const dateFormatter = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});

return (
// Get the top 3 results at most
responseJson.data
Expand All @@ -49,9 +55,9 @@ const getSimilarDiscussions = async (secret: string, question: string): Promise<
// Convert to markdown
.map(
(item: Discussion) =>
`- [${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")
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2059b17

Please sign in to comment.