init project
This commit is contained in:
38
database/factories/EmbeddingEntryFactory.php
Normal file
38
database/factories/EmbeddingEntryFactory.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\EmbeddingEntry;
|
||||
use App\Services\EmbeddingVector;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends Factory<EmbeddingEntry>
|
||||
*/
|
||||
class EmbeddingEntryFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$sourceText = fake()->sentence();
|
||||
$dimensions = (int) config('ai.providers.ollama.models.embeddings.dimensions', 768);
|
||||
$embedding = EmbeddingVector::normalize(
|
||||
array_map(fn () => fake()->randomFloat(6, -1, 1), range(1, $dimensions))
|
||||
);
|
||||
|
||||
return [
|
||||
'source_text' => $sourceText,
|
||||
'content_hash' => hash('sha256', Str::of($sourceText)->squish()->lower()->value()),
|
||||
'embedding' => $embedding,
|
||||
'embedding_dimensions' => $dimensions,
|
||||
'provider' => 'ollama',
|
||||
'model' => config('ai.providers.ollama.models.embeddings.default', 'nomic-embed-text'),
|
||||
'tokens' => fake()->numberBetween(1, 100),
|
||||
];
|
||||
}
|
||||
}
|
||||
35
database/factories/EmbeddingUsageEventFactory.php
Normal file
35
database/factories/EmbeddingUsageEventFactory.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\EmbeddingUsageEvent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<EmbeddingUsageEvent>
|
||||
*/
|
||||
class EmbeddingUsageEventFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$tool = fake()->randomElement(['embed', 'search', 'compare']);
|
||||
$inputCount = fake()->numberBetween(1, 8);
|
||||
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'tool' => $tool,
|
||||
'provider' => 'ollama',
|
||||
'model' => config('ai.providers.ollama.models.embeddings.default', 'nomic-embed-text'),
|
||||
'embedding_dimensions' => (int) config('ai.providers.ollama.models.embeddings.dimensions', 768),
|
||||
'input_count' => $inputCount,
|
||||
'result_count' => $tool === 'search' ? fake()->numberBetween(0, 10) : $inputCount,
|
||||
'tokens' => fake()->optional()->numberBetween(5, 500),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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
|
||||
{
|
||||
$driver = Schema::getConnection()->getDriverName();
|
||||
$dimensions = (int) config('ai.providers.ollama.models.embeddings.dimensions', 768);
|
||||
|
||||
if ($driver === 'pgsql') {
|
||||
Schema::ensureVectorExtensionExists();
|
||||
}
|
||||
|
||||
Schema::create('embedding_entries', function (Blueprint $table) use ($dimensions, $driver) {
|
||||
$table->id();
|
||||
$table->text('source_text');
|
||||
$table->string('content_hash', 64);
|
||||
$table->string('provider');
|
||||
$table->string('model');
|
||||
$table->unsignedSmallInteger('embedding_dimensions')->default($dimensions);
|
||||
$table->unsignedInteger('tokens')->nullable();
|
||||
|
||||
if ($driver === 'pgsql') {
|
||||
$table->vector('embedding', dimensions: $dimensions);
|
||||
$table->vectorIndex('embedding');
|
||||
} else {
|
||||
$table->json('embedding');
|
||||
}
|
||||
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(
|
||||
['content_hash', 'provider', 'model', 'embedding_dimensions'],
|
||||
'embedding_entries_unique_embedding'
|
||||
);
|
||||
$table->index(['provider', 'model', 'embedding_dimensions']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('embedding_entries');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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('embedding_usage_events', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('tool', 32);
|
||||
$table->string('provider');
|
||||
$table->string('model');
|
||||
$table->unsignedSmallInteger('embedding_dimensions');
|
||||
$table->unsignedInteger('input_count')->default(0);
|
||||
$table->unsignedInteger('result_count')->default(0);
|
||||
$table->unsignedInteger('tokens')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_id', 'created_at']);
|
||||
$table->index(['user_id', 'tool', 'created_at']);
|
||||
$table->index(['user_id', 'model']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('embedding_usage_events');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user