Add (ai gen)

This commit is contained in:
2023-10-01 00:35:27 +08:00
parent 645d0f1d02
commit f02081e30c
79 changed files with 4816 additions and 1041 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Helpers\FirstParty\OSSUploader;
use Exception;
use Illuminate\Support\Facades\Storage;
class OSSUploader
{
public static function readJson($storage_driver, $relative_directory, $filename)
{
$filepath = rtrim($relative_directory, '/').'/'.$filename;
try {
$jsonContent = Storage::disk($storage_driver)->get($filepath);
$decodedJson = json_decode($jsonContent, false, 512);
return $decodedJson;
} catch (\Exception $e) {
return null;
}
return null;
}
public static function readFile($storage_driver, $relative_directory, $filename)
{
$filepath = rtrim($relative_directory, '/').'/'.$filename;
try {
return Storage::disk($storage_driver)->get($filepath);
} catch (\Exception $e) {
return null;
}
return null;
}
public static function uploadJson($storage_driver, $relative_directory, $filename, $jsonData)
{
$jsonString = json_encode($jsonData, JSON_PRETTY_PRINT);
try {
return self::uploadFile($storage_driver, $relative_directory, $filename, $jsonString);
} catch (Exception $e) {
return false;
}
return false;
}
public static function uploadFile($storage_driver, $relative_directory, $filename, $file)
{
$filepath = rtrim($relative_directory, '/').'/'.$filename;
// if(!Storage::disk($storage_driver)->exists($relative_directory))
// {
// Storage::disk($storage_driver)->makeDirectory($relative_directory);
// }
return Storage::disk($storage_driver)->put($filepath, $file);
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Helpers\FirstParty\OpenAI;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class OpenAI
{
public static function writeProductArticle($excerpt, $photos)
{
$system_prompt = '
You are tasked with writing a comprehensive product introduction article using the provided excerpt. The emphasis should be on the performance, features, and notable aspects of the product. The review should avoid the use of personal pronouns and must not delve into marketplace-related information. Return the output in the following json format:\n\n
{"title": "(Article Title)","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, 500-700 word count)"}\n\n
Mandatory Requirements:\n
- Write in US grade 8-9 English\n
- Use the following sections whenever applicable:\n
-- ### Introduction\n
-- ### Overview\n
-- ### Specifications (use valid Markdown table format with header and seperator when possible) and explanation\n
-- ### Price\n
-- ### Should I Buy?\n
- do not make up facts, use facts provided by excerpt only\n
- No article titles inside markdown\n
- All article sections use ###
- Add at least 3 markdown images with article title as caption in every section except for Introduction
';
$user_prompt = "Excerpt: {$excerpt}\nPhotos:\n";
foreach ($photos as $photo) {
$user_prompt .= "{$photo}\n";
}
$output = (self::chatCompletion($system_prompt, $user_prompt, 'gpt-3.5-turbo', 2000));
if (! is_null($output)) {
try {
return json_decode($output, false, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
Log::error($output);
inspector()->reportException($e);
return null;
}
}
return null;
}
public static function chatCompletion($system_prompt, $user_prompt, $model, $max_token = 2500)
{
try {
$response = Http::timeout(800)->withToken(config('platform.ai.openai.api_key'))
->post('https://api.openai.com/v1/chat/completions', [
'model' => $model,
'max_tokens' => $max_token,
'messages' => [
['role' => 'system', 'content' => $system_prompt],
['role' => 'user', 'content' => $user_prompt],
],
]);
//dd($response->body());
$json_response = json_decode($response->body(), false, 512, JSON_THROW_ON_ERROR);
$reply = $json_response?->choices[0]?->message?->content;
return $reply;
} catch (Exception $e) {
Log::error($response->body());
inspector()->reportException($e);
throw ($e);
}
return null;
}
}