Skip to content

Commit

Permalink
refactor(webhook): extract text and media search
Browse files Browse the repository at this point in the history
We will reuse them when proessing batch
  • Loading branch information
MrOrz committed Dec 27, 2023
1 parent bbbfee8 commit 57e80ff
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 78 deletions.
41 changes: 5 additions & 36 deletions src/webhook/handlers/initState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ import type {
Context,
CooccurredMessage,
} from 'src/types/chatbotState';
import gql from 'src/lib/gql';
import {
createPostbackAction,
ellipsis,
POSTBACK_NO_ARTICLE_FOUND,
createHighlightContents,
createTextMessage,
createArticleSourceReply,
searchText,
} from './utils';
import ga from 'src/lib/ga';
import detectDialogflowIntent from 'src/lib/detectDialogflowIntent';
import choosingArticle from './choosingArticle';
import {
ListArticlesInInitStateQuery,
ListArticlesInInitStateQueryVariables,
} from 'typegen/graphql';

const SIMILARITY_THRESHOLD = 0.95;

Expand Down Expand Up @@ -81,43 +77,16 @@ const initState = async ({
}

// Search for articles
const {
data: { ListArticles },
} = await gql`
query ListArticlesInInitState($text: String!) {
ListArticles(
filter: { moreLikeThis: { like: $text } }
orderBy: [{ _score: DESC }]
first: 4
) {
edges {
node {
text
id
articleType
}
highlight {
text
hyperlinks {
title
summary
}
}
}
}
}
`<ListArticlesInInitStateQuery, ListArticlesInInitStateQueryVariables>({
text: input,
});
const result = await searchText(input);

const inputSummary = ellipsis(input, 12);

if (ListArticles?.edges.length) {
if (result?.edges.length) {
// Track if find similar Articles in DB.
visitor.event({ ec: 'UserInput', ea: 'ArticleSearch', el: 'ArticleFound' });

// Track which Article is searched. And set tracking event as non-interactionHit.
ListArticles.edges.forEach((edge) => {
result.edges.forEach((edge) => {
visitor.event({
ec: 'Article',
ea: 'Search',
Expand All @@ -126,7 +95,7 @@ const initState = async ({
});
});

const edgesSortedWithSimilarity = ListArticles.edges
const edgesSortedWithSimilarity = result.edges
.map((edge) => ({
...edge,
similarity: stringSimilarity.compareTwoStrings(
Expand Down
49 changes: 7 additions & 42 deletions src/webhook/handlers/processMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createTextMessage,
createAskArticleSubmissionConsentReply,
createHighlightContents,
searchMedia,
} from './utils';
import gql from 'src/lib/gql';
import ga from 'src/lib/ga';
Expand Down Expand Up @@ -44,50 +45,14 @@ export default async function (message: CooccurredMessage, userId: string) {
msgs: [message],
};

const {
data: { ListArticles },
} = await gql`
query ListArticlesInProcessMedia($mediaUrl: String!) {
ListArticles(
filter: {
mediaUrl: $mediaUrl
articleTypes: [TEXT, IMAGE, AUDIO, VIDEO]
transcript: { shouldCreate: true }
}
orderBy: [{ _score: DESC }]
first: 9
) {
edges {
score
mediaSimilarity
node {
id
articleType
attachmentUrl(variant: THUMBNAIL)
}
highlight {
text
hyperlinks {
title
summary
}
}
}
}
}
`<ListArticlesInProcessMediaQuery, ListArticlesInProcessMediaQueryVariables>(
{
mediaUrl: proxyUrl,
},
{ userId }
);
const result = await searchMedia(proxyUrl, userId);

if (ListArticles && ListArticles.edges.length) {
if (result && result.edges.length) {
// Track if find similar Articles in DB.
visitor.event({ ec: 'UserInput', ea: 'ArticleSearch', el: 'ArticleFound' });

// Track which Article is searched. And set tracking event as non-interactionHit.
ListArticles.edges.forEach((edge) => {
result.edges.forEach((edge) => {
visitor.event({
ec: 'Article',
ea: 'Search',
Expand All @@ -96,14 +61,14 @@ export default async function (message: CooccurredMessage, userId: string) {
});
});

const edgesSortedWithSimilarity = [...ListArticles.edges].sort(
const edgesSortedWithSimilarity = [...result.edges].sort(
(a, b) => b.mediaSimilarity - a.mediaSimilarity
);

const hasIdenticalDocs =
edgesSortedWithSimilarity[0].mediaSimilarity >= SIMILARITY_THRESHOLD;

if (ListArticles.edges.length === 1 && hasIdenticalDocs) {
if (result.edges.length === 1 && hasIdenticalDocs) {
visitor.send();

return await choosingArticle({
Expand All @@ -118,7 +83,7 @@ export default async function (message: CooccurredMessage, userId: string) {
});
}

const articleOptions = ListArticles.edges
const articleOptions = result.edges
.map(
(
{
Expand Down
80 changes: 80 additions & 0 deletions src/webhook/handlers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import type {
CreateReferenceWordsReplyFragment,
CreateAiReplyMutation,
CreateAiReplyMutationVariables,
ListArticlesInInitStateQuery,
ListArticlesInInitStateQueryVariables,
ListArticlesInProcessMediaQuery,
ListArticlesInProcessMediaQueryVariables,
} from 'typegen/graphql';

import type { Input as ChoosingReplyInput } from './choosingReply';
Expand Down Expand Up @@ -851,3 +855,79 @@ export function getLineContentProxyURL(messageId: string) {

return `${process.env.RUMORS_LINE_BOT_URL}/getcontent?token=${jwt}`;
}

export async function searchText(
text: string
): Promise<ListArticlesInInitStateQuery['ListArticles']> {
const {
data: { ListArticles },
} = await gql`
query ListArticlesInInitState($text: String!) {
ListArticles(
filter: { moreLikeThis: { like: $text } }
orderBy: [{ _score: DESC }]
first: 4
) {
edges {
node {
text
id
articleType
}
highlight {
text
hyperlinks {
title
summary
}
}
}
}
}
`<ListArticlesInInitStateQuery, ListArticlesInInitStateQueryVariables>({
text,
});
return ListArticles;
}

export async function searchMedia(
mediaUrl: string,
userId: string
): Promise<ListArticlesInProcessMediaQuery['ListArticles']> {
const {
data: { ListArticles },
} = await gql`
query ListArticlesInProcessMedia($mediaUrl: String!) {
ListArticles(
filter: {
mediaUrl: $mediaUrl
articleTypes: [TEXT, IMAGE, AUDIO, VIDEO]
transcript: { shouldCreate: true }
}
orderBy: [{ _score: DESC }]
first: 9
) {
edges {
score
mediaSimilarity
node {
id
articleType
attachmentUrl(variant: THUMBNAIL)
}
highlight {
text
hyperlinks {
title
summary
}
}
}
}
}
`<ListArticlesInProcessMediaQuery, ListArticlesInProcessMediaQueryVariables>(
{ mediaUrl },
{ userId }
);
return ListArticles;
}

0 comments on commit 57e80ff

Please sign in to comment.