Queue AI processing to avoid request > 30 sec
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Forms;
|
||||
use App\Console\Commands\GenerateTemplate;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AiGenerateFormRequest;
|
||||
use App\Models\Forms\AI\AiFormCompletion;
|
||||
use App\Service\OpenAi\GptCompleter;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -13,26 +14,24 @@ class AiFormController extends Controller
|
||||
public function generateForm(AiGenerateFormRequest $request)
|
||||
{
|
||||
$this->middleware('throttle:4,1');
|
||||
$completer = (new GptCompleter(config('services.openai.api_key')))
|
||||
->setSystemMessage('You are a robot helping to generate forms.');
|
||||
$completer->completeChat([
|
||||
["role" => "user", "content" => Str::of(GenerateTemplate::FORM_STRUCTURE_PROMPT)
|
||||
->replace('[REPLACE]', $request->form_prompt)->toString()]
|
||||
], 3000);
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form successfully generated!',
|
||||
'form' => $this->cleanOutput($completer->getArray())
|
||||
'message' => 'We\'re working on your form, please wait ~1 min.',
|
||||
'ai_form_completion_id' => AiFormCompletion::create([
|
||||
'form_prompt' => $request->input('form_prompt'),
|
||||
'ip' => $request->ip()
|
||||
])->id
|
||||
]);
|
||||
}
|
||||
|
||||
private function cleanOutput($formData)
|
||||
public function show(AiFormCompletion $aiFormCompletion)
|
||||
{
|
||||
// Add property uuids
|
||||
foreach ($formData['properties'] as &$property) {
|
||||
$property['id'] = Str::uuid()->toString();
|
||||
if ($aiFormCompletion->ip != request()->ip()) {
|
||||
return $this->error('You are not authorized to view this AI completion.', 403);
|
||||
}
|
||||
|
||||
return $formData;
|
||||
return $this->success([
|
||||
'ai_form_completion' => $aiFormCompletion
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
88
app/Jobs/Form/GenerateAiForm.php
Normal file
88
app/Jobs/Form/GenerateAiForm.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Form;
|
||||
|
||||
use App\Console\Commands\GenerateTemplate;
|
||||
use App\Http\Requests\AiGenerateFormRequest;
|
||||
use App\Models\Forms\AI\AiFormCompletion;
|
||||
use App\Service\OpenAi\GptCompleter;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class GenerateAiForm implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(public AiFormCompletion $completion)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->completion->update([
|
||||
'status' => AiFormCompletion::STATUS_PROCESSING
|
||||
]);
|
||||
|
||||
$completer = (new GptCompleter(config('services.openai.api_key')))
|
||||
->setSystemMessage('You are a robot helping to generate forms.');
|
||||
|
||||
try {
|
||||
$completer->completeChat([
|
||||
["role" => "user", "content" => Str::of(GenerateTemplate::FORM_STRUCTURE_PROMPT)
|
||||
->replace('[REPLACE]', $this->completion->form_prompt)->toString()]
|
||||
], 3000);
|
||||
|
||||
$this->completion->update([
|
||||
'status' => AiFormCompletion::STATUS_COMPLETED,
|
||||
'result' => $this->cleanOutput($completer->getArray())
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->completion->update([
|
||||
'status' => AiFormCompletion::STATUS_FAILED,
|
||||
'result' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function generateForm(AiGenerateFormRequest $request)
|
||||
{
|
||||
$completer = (new GptCompleter(config('services.openai.api_key')))
|
||||
->setSystemMessage('You are a robot helping to generate forms.');
|
||||
$completer->completeChat([
|
||||
["role" => "user", "content" => Str::of(GenerateTemplate::FORM_STRUCTURE_PROMPT)
|
||||
->replace('[REPLACE]', $request->form_prompt)->toString()]
|
||||
], 3000);
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form successfully generated!',
|
||||
'form' => $this->cleanOutput($completer->getArray())
|
||||
]);
|
||||
}
|
||||
|
||||
private function cleanOutput($formData)
|
||||
{
|
||||
// Add property uuids
|
||||
foreach ($formData['properties'] as &$property) {
|
||||
$property['id'] = Str::uuid()->toString();
|
||||
}
|
||||
|
||||
return $formData;
|
||||
}
|
||||
}
|
||||
38
app/Models/Forms/AI/AiFormCompletion.php
Normal file
38
app/Models/Forms/AI/AiFormCompletion.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Forms\AI;
|
||||
|
||||
use App\Jobs\Form\GenerateAiForm;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class AiFormCompletion extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
const STATUS_PENDING = 'pending';
|
||||
const STATUS_PROCESSING = 'processing';
|
||||
const STATUS_COMPLETED = 'completed';
|
||||
const STATUS_FAILED = 'failed';
|
||||
|
||||
protected $table = 'ai_form_completions';
|
||||
|
||||
protected $fillable = [
|
||||
'form_prompt',
|
||||
'status',
|
||||
'result',
|
||||
'ip'
|
||||
];
|
||||
|
||||
protected $attributes = [
|
||||
'status' => self::STATUS_PENDING
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
// Dispatch completion job on creation
|
||||
static::created(function (self $completion) {
|
||||
GenerateAiForm::dispatch($completion);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user