Add (ai gen): auto-select categories

This commit is contained in:
2023-10-02 17:54:05 +08:00
parent e04c519ab9
commit b2751e5133
2 changed files with 25 additions and 5 deletions

View File

@@ -8,13 +8,15 @@
class OpenAI class OpenAI
{ {
public static function writeProductArticle($excerpt, $photos) public static function writeProductArticle($excerpt, $photos, $categories)
{ {
//$excerpt = substr($excerpt, 0, 1500); //$excerpt = substr($excerpt, 0, 1500);
$category_str = implode("|", $categories);
$system_prompt = ' $system_prompt = '
You are tasked with writing a comprehensive product introduction & review article using the provided excerpt. Write as if you are reviewing the product by a third party, but no pronouns.The emphasis should be on the performance, features, and notable aspects of the product. The review must not delve into marketplace-related information. Return the output in the following json format:\n\n You are tasked with writing a comprehensive product introduction & review article using the provided excerpt. Write as if you are reviewing the product by a third party, but no pronouns.The emphasis should be on the performance, features, and notable aspects of the product. The review must not delve into marketplace-related information. Return the output in the following json format:\n\n
{"title": "(Article Title, start with product name, 60-70 characters)","excerpt": "(One sentence summary, 150-160 characters of an article, do not use start sentence with verb.)","cliffhanger": "(One sentence 70-80 characters of article, cliff-hanging sentence to attract readers)","body": "(Markdown format, 700-900 word count)"}\n\n {"category":"('. $category_str .')","title": "(Article Title, start with product name, 60-70 characters)","excerpt": "(One sentence summary, 150-160 characters of an article, do not use start sentence with verb.)","cliffhanger": "(One sentence 70-80 characters of article, cliff-hanging sentence to attract readers)","body": "(Markdown format, 700-900 word count)"}\n\n
Mandatory Requirements:\n Mandatory Requirements:\n
- Write in US grade 8-9 English\n - Write in US grade 8-9 English\n
- Use the following sections whenever applicable:\n - Use the following sections whenever applicable:\n
@@ -26,6 +28,7 @@ public static function writeProductArticle($excerpt, $photos)
- do not make up facts, use facts provided by excerpt only\n - do not make up facts, use facts provided by excerpt only\n
- No article titles inside markdown\n - No article titles inside markdown\n
- All article sections use ### - All article sections use ###
- Pick the closest given category
'; ';

View File

@@ -10,6 +10,7 @@
use App\Models\AiWriteup; use App\Models\AiWriteup;
use App\Models\Post; use App\Models\Post;
use App\Models\PostCategory; use App\Models\PostCategory;
use App\Models\Category;
use App\Models\ShopeeSellerCategory; use App\Models\ShopeeSellerCategory;
use App\Models\ShopeeSellerScrape; use App\Models\ShopeeSellerScrape;
use App\Models\ShopeeSellerScrapedImage; use App\Models\ShopeeSellerScrapedImage;
@@ -54,7 +55,15 @@ public static function handle(ShopeeSellerScrape $shopee_seller_scrape)
$ai_writeup = AiWriteup::where('source', 'shopee')->where('source_url', $shopee_task->product_task->response->url)->first(); $ai_writeup = AiWriteup::where('source', 'shopee')->where('source_url', $shopee_task->product_task->response->url)->first();
if (is_null($ai_writeup)) { if (is_null($ai_writeup)) {
$ai_output = OpenAI::writeProductArticle($excerpt, $photos);
$categories = [
'Beauty',
'Technology',
'Home & Living',
'Health & Fitness'
];
$ai_output = OpenAI::writeProductArticle($excerpt, $photos, $categories);
//dd($ai_output); //dd($ai_output);
@@ -65,11 +74,19 @@ public static function handle(ShopeeSellerScrape $shopee_seller_scrape)
inspector()->reportException($e); inspector()->reportException($e);
throw ($e); throw ($e);
} else { } else {
$picked_category = Category::where('name', $ai_output->category)->where('country_locale_id', $shopee_seller_scrape->category->country_locale_id)->first();
if (is_null($picked_category))
{
$picked_category = $shopee_seller_scrape->category;
}
// save // save
$ai_writeup = new AiWriteup; $ai_writeup = new AiWriteup;
$ai_writeup->source = 'shopee'; $ai_writeup->source = 'shopee';
$ai_writeup->source_url = $shopee_task->product_task->response->url; $ai_writeup->source_url = $shopee_task->product_task->response->url;
$ai_writeup->category_id = $shopee_seller_scrape->category->id; $ai_writeup->category_id = $picked_category->category->id;
$ai_writeup->title = $ai_output->title; $ai_writeup->title = $ai_output->title;
$ai_writeup->excerpt = $ai_output->excerpt; $ai_writeup->excerpt = $ai_output->excerpt;
$ai_writeup->featured_image = ''; $ai_writeup->featured_image = '';
@@ -120,7 +137,7 @@ public static function handle(ShopeeSellerScrape $shopee_seller_scrape)
PostCategory::create([ PostCategory::create([
'post_id' => $post->id, 'post_id' => $post->id,
'category_id' => $shopee_seller_scrape->category->id, 'category_id' => $picked_category->id,
]); ]);
if (app()->environment() == 'production') { if (app()->environment() == 'production') {