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

fix(digest): digest title prediction #73

Merged
merged 2 commits into from
Nov 15, 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
73 changes: 47 additions & 26 deletions src/pages/api/teams/[teamId]/digests/[digestId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ router
.use(checkDigest)
.patch(async (req, res) => {
const digestId = req.query.digestId as string;

try {
let digest = await client.digest.findUnique({
select: { publishedAt: true, teamId: true },
Expand All @@ -34,36 +33,17 @@ router
const lastDigests = await client.digest.findMany({
select: { title: true },
where: { teamId: digest?.teamId },
take: 5,
orderBy: { publishedAt: 'desc' },
});
if (!digest?.teamId) throw new Error('Missing teamId');

const lastDigestTitles = [
...lastDigests?.map((digest) => digest?.title),
req.body.title,
...lastDigests?.map((digest) => digest?.title),
].filter((title) => !!title);

if (Boolean(lastDigestTitles?.length)) {
const prompt = `
Here is a list of document titles sorted from most recent to oldest, separared by ; signs : ${lastDigestTitles.join(
';'
)}
Just guess the next document title. Don't add any other sentence in your response.
`;

try {
const response = await openAiCompletion({ prompt });
const guessedTitle = response[0]?.message?.content;

await client.team.update({
where: { id: digest?.teamId },
data: {
nextSuggestedDigestTitle: guessedTitle,
},
});
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
}
}
updateSuggestedDigestTitle(lastDigestTitles.reverse(), digest?.teamId!);
}

digest = await client.digest.update({
Expand Down Expand Up @@ -97,10 +77,51 @@ router
id: digestId?.toString(),
},
});

const wasAPublishedDigest = Boolean(digest?.publishedAt);
if (wasAPublishedDigest) {
await client.team.update({
quentingrchr marked this conversation as resolved.
Show resolved Hide resolved
where: { id: digest?.teamId },
data: {
nextSuggestedDigestTitle: null,
},
});
}
return res.status(201).json(digest);
});

export default router.handler({
onError: errorHandler,
});

async function updateSuggestedDigestTitle(
lastDigestTitles: {
title: string;
}[],
teamId: string
) {
if (Boolean(lastDigestTitles?.length)) {
const prompt = `
Here is a list of document titles sorted from most recent to oldest, separared by ; signs : ${lastDigestTitles.join(
';'
)}
Just guess the next document title. Don't add any other sentence in your response. If you can't guess a logical title, just write idk.
`;

try {
const response = await openAiCompletion({ prompt });
const guessedTitle = response[0]?.message?.content;
const canPredict = guessedTitle !== 'idk';
if (canPredict) {
await client.team.update({
where: { id: teamId },
data: {
nextSuggestedDigestTitle: guessedTitle,
},
});
}
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
}
}
}
1 change: 1 addition & 0 deletions src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const openAiCompletion = async ({
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: prompt }],
model,
temperature: 0.5,
});

return chatCompletion.choices;
Expand Down
35 changes: 24 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3921,10 +3921,11 @@ brotli@^1.3.3:
dependencies:
base64-js "^1.1.2"

browser-request@*:
version "0.3.3"
resolved "https://registry.yarnpkg.com/browser-request/-/browser-request-0.3.3.tgz#9ece5b5aca89a29932242e18bf933def9876cc17"
integrity sha512-YyNI4qJJ+piQG6MMEuo7J3Bzaqssufx04zpEKYfSrl/1Op59HWali9zMtBpXnkmqMcOuWJPZvudrm9wISmnCbg==
"browser-request@github:postlight/browser-request#feat-add-headers-to-response":
version "0.3.2"
resolved "https://codeload.github.com/postlight/browser-request/tar.gz/38faa5b85741aabfca61aa37d1ef044d68969ddf"
dependencies:
http-headers "^3.0.1"

browserslist@^4.21.10, browserslist@^4.21.9:
version "4.22.1"
Expand Down Expand Up @@ -6777,6 +6778,13 @@ [email protected]:
statuses "2.0.1"
toidentifier "1.0.1"

http-headers@^3.0.1:
version "3.0.2"
resolved "https://registry.yarnpkg.com/http-headers/-/http-headers-3.0.2.tgz#5147771292f0b39d6778d930a3a59a76fc7ef44d"
integrity sha512-87E1I+2Wg4dxxz4rcxElo3dxO/w1ZtgL1yA0Sb6vH3qU16vRKq1NjWQv9SCY3ly2OQROcoxHZOUpmelS+k6wOw==
dependencies:
next-line "^1.1.0"

[email protected]:
version "7.0.0"
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-7.0.0.tgz#e9096c5afd071a3fce56e6252bb321583c124673"
Expand Down Expand Up @@ -7832,7 +7840,7 @@ jose@^4.11.4, jose@^4.15.1:
resolved "https://registry.yarnpkg.com/jose/-/jose-4.15.4.tgz#02a9a763803e3872cf55f29ecef0dfdcc218cc03"
integrity sha512-W+oqK4H+r5sITxfxpSU+MMdr/YSWGvgZMQDIsNoBDGGy4i7GBPTtvFKibQzW06n3U3TqHjhvBJsirShsEJ6eeQ==

jquery@*:
jquery@^3.5.0:
version "3.7.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.7.1.tgz#083ef98927c9a6a74d05a6af02806566d16274de"
integrity sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg==
Expand Down Expand Up @@ -9571,14 +9579,14 @@ [email protected]:
resolved "https://registry.yarnpkg.com/moment-parseformat/-/moment-parseformat-3.0.0.tgz#3a1dc438b4bc073b7e93cc298cfb6c5daac26dba"
integrity sha512-dVgXe6b6DLnv4CHG7a1zUe5mSXaIZ3c6lSHm/EKeVeQI2/4pwe0VRde8OyoCE1Ro2lKT5P6uT9JElF7KDLV+jw==

moment-timezone@*:
version "0.5.43"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.43.tgz#3dd7f3d0c67f78c23cd1906b9b2137a09b3c4790"
integrity sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==
moment-timezone@0.5.37:
version "0.5.37"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.37.tgz#adf97f719c4e458fdb12e2b4e87b8bec9f4eef1e"
integrity sha512-uEDzDNFhfaywRl+vwXxffjjq1q0Vzr+fcQpQ1bU0kbzorfS7zVtZnCnGc8mhWmF39d4g4YriF6kwA75mJKE/Zg==
dependencies:
moment "^2.29.4"
moment ">= 2.9.0"

moment@^2.23.0, moment@^2.29.4:
"moment@>= 2.9.0", moment@^2.23.0:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
Expand Down Expand Up @@ -9685,6 +9693,11 @@ next-contentlayer@^0.3.2:
"@contentlayer/core" "0.3.4"
"@contentlayer/utils" "0.3.4"

next-line@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/next-line/-/next-line-1.1.0.tgz#fcae57853052b6a9bae8208e40dd7d3c2d304603"
integrity sha512-+I10J3wKNoKddNxn0CNpoZ3eTZuqxjNM3b1GImVx22+ePI+Y15P8g/j3WsbP0fhzzrFzrtjOAoq5NCCucswXOQ==

next-superjson-plugin@^0.5.8:
version "0.5.9"
resolved "https://registry.yarnpkg.com/next-superjson-plugin/-/next-superjson-plugin-0.5.9.tgz#2b4dc19799d205d33e5c648a7e48a2cef79b3052"
Expand Down
Loading