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

Story summary generation using AI #285

Merged
merged 2 commits into from
Dec 4, 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: 10 additions & 0 deletions api/functions/graphql/types/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,16 @@ const createStory = extendType({
author_name: createdStory.user.name,
tags,
})
.catch((err) => {
console.log("Error happened while posting to queue service:");
console.log(err);
}),
queueService.aiService
.generateStoryOgSummary({
id: createdStory.id,
title: createdStory.title,
body: createdStory.body,
})
.catch((err) => {
console.log("Error happened while posting to queue service:");
console.log(err);
Expand Down
84 changes: 84 additions & 0 deletions api/functions/on-queue-callback/on-queue-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const serverless = require("serverless-http");
const { createExpressApp } = require("../../modules");
const express = require("express");
const CONSTS = require("../../utils/consts");
const { prisma } = require("../../prisma");
const cacheService = require("../../services/cache.service");

const onQueueCallback = async (req, res) => {
const base64Token = Buffer.from(
`${CONSTS.BF_QUEUES_SERVICE_USERNAME}:${CONSTS.BF_QUEUES_SERVICE_PASS}`
).toString("base64");
const authToken = req.headers.authorization?.split(" ")[1];

if (authToken !== base64Token)
return res.status(401).send("Unauthorized Access");

const type = req.body.type;

const handler = jobHandlers[type];

if (!handler) return res.status(400).send("Unknown job type: ", type);

try {
await handler(req.body);
} catch (error) {
console.log(error);
return res
.status(400)
.send(error.message ?? "An unexpected error happened");
}

// Exec job type specific logic
return res.status(200).send("OK");
};

let app;

if (process.env.LOCAL) {
app = createExpressApp();
app.post("/on-queue-callback", onQueueCallback);
} else {
const router = express.Router();
router.post("/on-queue-callback", onQueueCallback);
app = createExpressApp(router);
}

const handler = serverless(app);
exports.handler = async (event, context) => {
return await handler(event, context);
};

const jobHandlers = {
"create-story-root-event": async (data) => {
const { story_id, root_event_id } = data;
if (!story_id || !root_event_id)
throw new Error("story_id or root_event_id are not provided");

await Promise.all([
prisma.story.update({
where: { id: Number(story_id) },
data: {
nostr_event_id: root_event_id,
},
}),
cacheService.invalidateStoryById(story_id),
]);
},

"generate-story-og-summary": async (data) => {
const { story_id, summary } = data;
if (!story_id || !summary)
throw new Error("story_id or summary are not provided");

await Promise.all([
prisma.story.update({
where: { id: Number(story_id) },
data: {
excerpt: summary,
},
}),
cacheService.invalidateStoryById(story_id),
]);
},
};
14 changes: 13 additions & 1 deletion api/functions/test-something/test-something.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const serverless = require("serverless-http");
const { createExpressApp } = require("../../modules");
const express = require("express");
const { queueService } = require("../../services/queue-service");

const testSomething = async (req, res) => {
// first, do some validation to make sure the function has been invoked internally
Expand All @@ -10,8 +11,19 @@ const testSomething = async (req, res) => {
// return res.status(401).json({ status: "ERROR", message: "Unauthorized" });
// }

const {} = req.body;
const story = req.body;
try {
queueService.aiService
.generateStoryOgSummary({
id: story.id,
title: story.title,
body: story.body,
})
.catch((err) => {
console.log("Error happened while posting to queue service:");
console.log(err);
});

return res.status(200).json({ status: "OK", message: "Done" });
} catch (error) {
console.log(error);
Expand Down
26 changes: 26 additions & 0 deletions api/services/queue-service/ai-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { marked } = require("marked");
const env = require("../../utils/consts");
const { callQueueApi } = require("./helpers");

const aiService = {
generateStoryOgSummary: ({ id, title, body }) => {
const htmlBody = marked.parse(body);

const bodyAsText = htmlBody
.replace(/<[^>]+>/g, "")
.replace(/&amp;/g, "&")
.replace(/&#39;/g, "'")
.replace(/&quot;/g, '"');

return callQueueApi("/add-job/ai/generate-story-og-summary", {
story: {
id,
title,
body: bodyAsText,
},
callback_url: env.FUNCTIONS_URL + "/on-queue-callback",
});
},
};

module.exports = aiService;
2 changes: 2 additions & 0 deletions api/services/queue-service/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const emailService = require("./emails-service");
const nostrService = require("./nostr-service");
const searchIndexService = require("./search-index-service");
const aiService = require("./ai-service");

const queueService = {
nostrService,
emailService,
searchIndexService,
aiService,
};

module.exports = { queueService };
2 changes: 2 additions & 0 deletions api/utils/consts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const env = envsafe(
devDefault: "http://localhost:3001",
}),
HYGRAPH_WEBHOOKS_SECRET: str({
allowEmpty: true,
default: "",
devDefault: "SUPER_SECRET",
}),
},
Expand Down
43 changes: 8 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ functions:
path: on-job-success
method: post

on-queue-callback:
handler: api/functions/on-queue-callback/on-queue-callback.handler
events:
- http:
path: on-queue-callback
method: post

upload-image-url:
handler: api/functions/upload-image-url/upload-image-url.handler
events:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ function StoryPageContent({ story }: Props) {

return (
<>
<OgTags
title={story.title}
description={story.body.slice(0, 50)}
image={story.cover_image}
/>
<OgTags image={story.cover_image} />
<Card id="content" onlyMd className="relative max">
<div className="flex justify-between items-center flex-wrap mb-16">
<PostPageHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function PostDetailsPage(props: Props) {

return (
<>
<OgTags title={post.title} description={post.body.slice(0, 50)} />
<OgTags title={post.title} description={post.excerpt} />
<ScrollToTop />
<div className={`page-container max-md:bg-white`}>
{isLargeScreen ? (
Expand Down
3 changes: 3 additions & 0 deletions src/features/Posts/pages/PostDetailsPage/postDetails.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ query PostDetails($id: Int!, $type: POST_TYPE!) {
... on Story {
id
title
excerpt
createdAt
author {
id
Expand Down Expand Up @@ -42,6 +43,7 @@ query PostDetails($id: Int!, $type: POST_TYPE!) {
... on Bounty {
id
title
excerpt
createdAt
author {
id
Expand Down Expand Up @@ -86,6 +88,7 @@ query PostDetails($id: Int!, $type: POST_TYPE!) {
... on Question {
id
title
excerpt
createdAt
author {
id
Expand Down
5 changes: 4 additions & 1 deletion src/graphql/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ export type PostDetailsQueryVariables = Exact<{
}>;


export type PostDetailsQuery = { __typename?: 'Query', getPostById: { __typename?: 'Bounty', id: number, title: string, createdAt: any, body: string, type: string, cover_image: string | null, deadline: string, reward_amount: number, applicants_count: number, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> }, applications: Array<{ __typename?: 'BountyApplication', id: number, date: string, workplan: string, author: { __typename?: 'User', id: number, name: string, avatar: string } }> } | { __typename?: 'Question', id: number, title: string, createdAt: any, body: string, type: string, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> } } | { __typename?: 'Story', id: number, title: string, createdAt: any, body: string, type: string, cover_image: string | null, is_published: boolean | null, nostr_event_id: string | null, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> }, project: { __typename?: 'Project', id: number, title: string, thumbnail_image: string | null, hashtag: string } | null } };
export type PostDetailsQuery = { __typename?: 'Query', getPostById: { __typename?: 'Bounty', id: number, title: string, excerpt: string, createdAt: any, body: string, type: string, cover_image: string | null, deadline: string, reward_amount: number, applicants_count: number, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> }, applications: Array<{ __typename?: 'BountyApplication', id: number, date: string, workplan: string, author: { __typename?: 'User', id: number, name: string, avatar: string } }> } | { __typename?: 'Question', id: number, title: string, excerpt: string, createdAt: any, body: string, type: string, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> } } | { __typename?: 'Story', id: number, title: string, excerpt: string, createdAt: any, body: string, type: string, cover_image: string | null, is_published: boolean | null, nostr_event_id: string | null, author: { __typename?: 'User', id: number, name: string, avatar: string, join_date: any, primary_nostr_key: string | null }, tags: Array<{ __typename?: 'Tag', id: number, title: string }>, votes: { __typename?: 'Votes', total: number, total_anonymous_votes: number, voters: Array<{ __typename?: 'Voter', amount_voted: number, user: { __typename?: 'User', id: number, name: string, avatar: string } }> }, project: { __typename?: 'Project', id: number, title: string, thumbnail_image: string | null, hashtag: string } | null } };

export type GetTagInfoQueryVariables = Exact<{
tag: InputMaybe<Scalars['String']>;
Expand Down Expand Up @@ -2634,6 +2634,7 @@ export const PostDetailsDocument = gql`
... on Story {
id
title
excerpt
createdAt
author {
id
Expand Down Expand Up @@ -2673,6 +2674,7 @@ export const PostDetailsDocument = gql`
... on Bounty {
id
title
excerpt
createdAt
author {
id
Expand Down Expand Up @@ -2717,6 +2719,7 @@ export const PostDetailsDocument = gql`
... on Question {
id
title
excerpt
createdAt
author {
id
Expand Down