-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeedEmojiDataCommand.php
60 lines (49 loc) · 1.55 KB
/
SeedEmojiDataCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Upstash\Vector\DataUpsert;
use Upstash\Vector\Index;
use Upstash\Vector\Laravel\Facades\Vector;
use function Laravel\Prompts\spin;
class SeedEmojiDataCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:seed:emoji-data';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Seeds Upstash Vector with Emoji Data';
/**
* Execute the console command.
*/
public function handle()
{
spin(fn () => $this->seedEmojiData(), 'Seeding Emoji Data...');
$this->info('Seeding Complete!');
}
private function seedEmojiData(): void
{
// load emoji data from disk
$emojiData = File::get(storage_path('emoji_data.json'));
// decode the json data and convert it into a collection
$collection = collect(json_decode($emojiData, true));
// map the collection to a DataUpsert object
$collection = $collection->map(fn (array $data) => new DataUpsert(
id: trim($data['id']),
data: $data['data'],
metadata: $data['metadata'],
));
// upsert the data into the vector index in chunks of 500
$collection->chunk(500)->each(function (Collection $collection) {
Vector::upsertDataMany($collection->toArray());
});
}
}