|
2 | 2 | Injectable, |
3 | 3 | NotFoundException, |
4 | 4 | ConflictException, |
| 5 | + InternalServerErrorException, |
5 | 6 | } from '@nestjs/common'; |
6 | 7 | import { EntityManager } from 'typeorm'; |
7 | 8 | import { |
@@ -212,9 +213,7 @@ export class CategoryService { |
212 | 213 | queryRunnerManager: EntityManager, |
213 | 214 | ): Promise<DeleteCategoryOutput> { |
214 | 215 | try { |
215 | | - const category = await this.categoryRepository.findOne({ |
216 | | - where: { id: categoryId }, |
217 | | - }); |
| 216 | + const category = await this.categoryRepository.findById(categoryId); |
218 | 217 | if (!category) { |
219 | 218 | throw new NotFoundException('Category not found.'); |
220 | 219 | } |
@@ -476,6 +475,70 @@ export class CategoryService { |
476 | 475 | } |
477 | 476 | } |
478 | 477 |
|
| 478 | + async autoCategorizeWithId(user: User, link: string) { |
| 479 | + try { |
| 480 | + const categories = await this.categoryRepository.findByUserId(user.id); |
| 481 | + if (categories.length === 0) { |
| 482 | + throw new NotFoundException('Categories not found'); |
| 483 | + } |
| 484 | + |
| 485 | + const { title, siteName, description } = await getLinkInfo(link); |
| 486 | + |
| 487 | + const content = await getLinkContent(link); |
| 488 | + |
| 489 | + const question = `You are a machine tasked with auto-categorizing articles based on information obtained through web scraping. |
| 490 | +You can only answer a single category name. Here is the article's information: |
| 491 | +<title>${title && `title: "${title.trim()}"`}</title> |
| 492 | +<content>${ |
| 493 | + content && |
| 494 | + `content: "${content.replace(/\s/g, '').slice(0, 300).trim()}"` |
| 495 | + }</content> |
| 496 | +<description>${ |
| 497 | + description && `description: "${description.trim()}"` |
| 498 | + }</description> |
| 499 | +<siteName>${siteName && `site name: "${siteName.trim()}"`}</siteName> |
| 500 | +Please provide the most suitable category among the following. Here is Category options: [${[ |
| 501 | + ...categories, |
| 502 | + 'None', |
| 503 | + ].join(', ')}] |
| 504 | +
|
| 505 | +Given the following categories, please provide the most suitable category for the article. |
| 506 | +<categories>${categories |
| 507 | + .map((category) => |
| 508 | + JSON.stringify({ id: category.id, name: category.name }), |
| 509 | + ) |
| 510 | + .join('\n')}</categories> |
| 511 | +
|
| 512 | +Present your reply options in JSON format below. |
| 513 | +\`\`\`json |
| 514 | +{ |
| 515 | + "id": id, |
| 516 | + "name": "category name" |
| 517 | +} |
| 518 | +\`\`\` |
| 519 | +`; |
| 520 | + |
| 521 | + const response = await this.openaiService.createChatCompletion({ |
| 522 | + question, |
| 523 | + temperature: 0, |
| 524 | + responseType: 'json', |
| 525 | + }); |
| 526 | + |
| 527 | + const categoryStr = response.choices[0].message?.content; |
| 528 | + |
| 529 | + if (categoryStr) { |
| 530 | + const { id, name } = JSON.parse( |
| 531 | + categoryStr.replace(/^```json|```$/g, '').trim(), |
| 532 | + ); |
| 533 | + return { category: { id, name } }; |
| 534 | + } |
| 535 | + |
| 536 | + throw new InternalServerErrorException('Failed to categorize'); |
| 537 | + } catch (e) { |
| 538 | + throw e; |
| 539 | + } |
| 540 | + } |
| 541 | + |
479 | 542 | async autoCategorizeForTest( |
480 | 543 | autoCategorizeBody: AutoCategorizeBodyDto, |
481 | 544 | ): Promise<AutoCategorizeOutput> { |
|
0 commit comments