Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve github autoresponder #3568

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading