From 2d05389f54c45e78882afcd91c4a97ef2dd1e650 Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 12 Jan 2025 16:17:00 +0900 Subject: [PATCH 1/3] feat: change swagger condition --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 65561b3..b34b6ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,7 @@ async function bootstrap() { app.useGlobalPipes(new ValidationPipe()); app.setGlobalPrefix('api'); - if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'local') { + if (process.env.NODE_ENV !== 'prod') { const config = new DocumentBuilder() .setTitle('Quickchive') .setDescription('The API description') From 778720b2ab1e8ad1cd180aa3cd10ee8736b20170 Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 12 Jan 2025 19:08:00 +0900 Subject: [PATCH 2/3] fix: encode uri --- src/categories/category.service.ts | 109 ++++++++++++++--------------- src/contents/util/content.util.ts | 2 +- 2 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/categories/category.service.ts b/src/categories/category.service.ts index 0426a90..8f30912 100644 --- a/src/categories/category.service.ts +++ b/src/categories/category.service.ts @@ -488,65 +488,64 @@ export class CategoryService { } async autoCategorizeWithId(user: User, link: string) { - try { - const _categories = await this.categoryRepository.findByUserId(user.id); - if (_categories.length === 0) { - throw new NotFoundException('Categories not found'); - } + const _categories = await this.categoryRepository.findByUserId(user.id); + if (_categories.length === 0) { + throw new NotFoundException('Categories not found'); + } - const categories = _categories.map((category) => ({ - ...category, - depth: 0, - })); + const categories = _categories.map((category) => ({ + ...category, + depth: 0, + })); - categories.map((category, index) => { - categories.slice(index + 1).map((subCategory) => { - if (subCategory.parentId && subCategory.parentId === category.id) { - subCategory.depth = category.depth + 1; - } - }); + categories.map((category, index) => { + categories.slice(index + 1).map((subCategory) => { + if (subCategory.parentId && subCategory.parentId === category.id) { + subCategory.depth = category.depth + 1; + } }); + }); + + const { title, siteName, description } = await getLinkInfo(encodeURI(link)); + + const content = await getLinkContent(link); + + const question = `You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. + You can only answer a single category name. Here is the article's information: + ${title && `title: "${title.trim()}"`} + ${ + content && `content: "${content.replace(/\s/g, '').slice(0, 300).trim()}"` + } + ${ + description && `description: "${description.trim()}"` + } + ${siteName && `site name: "${siteName.trim()}"`} + + Given the following categories, please provide the most suitable category for the article. + - The deeper the category depth, the more specific the category is. + - If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more. + ${categories + .map((category) => + JSON.stringify({ + id: category.id, + name: category.name, + depth: category.depth, + }), + ) + .join('\n')} + + If there's no suitable category, must provide reply with "None". + + Present your reply options in JSON format below. + \`\`\`json + { + "id": id, + "name": "category name" + } + \`\`\` + `; - const { title, siteName, description } = await getLinkInfo(link); - - const content = await getLinkContent(link); - - const question = `You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. -You can only answer a single category name. Here is the article's information: -${title && `title: "${title.trim()}"`} -${ - content && - `content: "${content.replace(/\s/g, '').slice(0, 300).trim()}"` - } -${ - description && `description: "${description.trim()}"` - } -${siteName && `site name: "${siteName.trim()}"`} - -Given the following categories, please provide the most suitable category for the article. -- The deeper the category depth, the more specific the category is. -- If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more. -${categories - .map((category) => - JSON.stringify({ - id: category.id, - name: category.name, - depth: category.depth, - }), - ) - .join('\n')} - -If there's no suitable category, must provide reply with "None". - -Present your reply options in JSON format below. -\`\`\`json -{ - "id": id, - "name": "category name" -} -\`\`\` -`; - + try { const response = await this.openaiService.createChatCompletion({ question, temperature: 0, diff --git a/src/contents/util/content.util.ts b/src/contents/util/content.util.ts index ad865e1..40b4ff9 100644 --- a/src/contents/util/content.util.ts +++ b/src/contents/util/content.util.ts @@ -84,7 +84,7 @@ class OGCrawler { } } - throw new InternalServerErrorException('An unknown error occurred'); + throw new InternalServerErrorException(`An unknown error occurred`); } } From 419f0a55b14c9d57b35d9a7745542cdbbaa1a96c Mon Sep 17 00:00:00 2001 From: stae1102 Date: Sun, 12 Jan 2025 19:13:44 +0900 Subject: [PATCH 3/3] feat: change category recommendation prompt --- src/categories/category.service.ts | 42 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/categories/category.service.ts b/src/categories/category.service.ts index 8f30912..01fd617 100644 --- a/src/categories/category.service.ts +++ b/src/categories/category.service.ts @@ -511,20 +511,23 @@ export class CategoryService { const content = await getLinkContent(link); const question = `You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. - You can only answer a single category name. Here is the article's information: - ${title && `title: "${title.trim()}"`} - ${ + +You can only answer a single category name. Here is the article's information: +${title && `title: "${title.trim()}"`} +${ content && `content: "${content.replace(/\s/g, '').slice(0, 300).trim()}"` } - ${ +${ description && `description: "${description.trim()}"` } - ${siteName && `site name: "${siteName.trim()}"`} - - Given the following categories, please provide the most suitable category for the article. - - The deeper the category depth, the more specific the category is. - - If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more. - ${categories +${siteName && `site name: "${siteName.trim()}"`} + +Given the categories below, please provide the most suitable category for the article following the rules. +[RULES] +- The deeper the category depth, the more specific the category is. +- If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more. +- If there's no suitable category, must provide reply with "None". +${categories .map((category) => JSON.stringify({ id: category.id, @@ -533,16 +536,15 @@ export class CategoryService { }), ) .join('\n')} - - If there's no suitable category, must provide reply with "None". - - Present your reply options in JSON format below. - \`\`\`json - { - "id": id, - "name": "category name" - } - \`\`\` + + +Present your reply options in JSON format below. +\`\`\`json +{ + "id": id, + "name": "category name" +} +\`\`\` `; try {