feat: save ai usage
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\AiUsageStatus;
|
||||
use App\Enums\AiUsageType;
|
||||
use App\Models\AiUsage;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<AiUsage>
|
||||
*/
|
||||
class AiUsageFactory extends Factory
|
||||
{
|
||||
protected $model = AiUsage::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'type' => AiUsageType::MEAL_IMAGE_ANALYSIS,
|
||||
'status' => AiUsageStatus::SUCCESS,
|
||||
'input' => [
|
||||
'image' => [
|
||||
'mime_type' => 'image/png',
|
||||
'size' => 1024,
|
||||
],
|
||||
],
|
||||
'output' => [
|
||||
'title' => $this->faker->words(3, true),
|
||||
],
|
||||
'provider' => null,
|
||||
'model' => null,
|
||||
'prompt_tokens' => null,
|
||||
'completion_tokens' => null,
|
||||
'total_tokens' => null,
|
||||
'error_message' => null,
|
||||
'related_type' => null,
|
||||
'related_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function failed(): static
|
||||
{
|
||||
return $this->state(fn (): array => [
|
||||
'status' => AiUsageStatus::FAILED,
|
||||
'output' => null,
|
||||
'error_message' => $this->faker->sentence(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('ai_usages', function (Blueprint $table) {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignUlid('user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->string('type', 64);
|
||||
$table->string('status', 32);
|
||||
$table->json('input')->nullable();
|
||||
$table->json('output')->nullable();
|
||||
$table->string('provider')->nullable();
|
||||
$table->string('model')->nullable();
|
||||
$table->unsignedInteger('prompt_tokens')->nullable();
|
||||
$table->unsignedInteger('completion_tokens')->nullable();
|
||||
$table->unsignedInteger('total_tokens')->nullable();
|
||||
$table->text('error_message')->nullable();
|
||||
$table->string('related_type')->nullable();
|
||||
$table->string('related_id', 36)->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_id', 'type', 'created_at']);
|
||||
$table->index(['type', 'status', 'created_at']);
|
||||
$table->index(['related_type', 'related_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('ai_usages');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user