44
55namespace App \Command ;
66
7- use Codewithkyrian \Transformers \Pipelines \Pipeline ;
8-
9- use function Codewithkyrian \Transformers \Pipelines \pipeline ;
10-
11- use Codewithkyrian \Transformers \Pipelines \Task ;
12- use Codewithkyrian \Transformers \Tensor \Tensor ;
7+ use App \Service \EmbeddingConfigService ;
138use Qdrant \Config ;
149use Qdrant \Http \Transport ;
1510use Qdrant \Models \PointsStruct ;
3126)]
3227final class ProductsVectorizeCommand extends Command
3328{
34- private const COLLECTION_NAME = 'products ' ;
35- private const DATA_FILE = __DIR__ .'/../../data/products.json ' ;
29+ private const string COLLECTION_NAME = 'products ' ;
30+ private const string DATA_FILE = __DIR__ .'/../../data/products.json ' ;
3631
3732 private Qdrant $ qdrantClient ;
38- private Pipeline $ embedder ;
33+
34+ private EmbeddingConfigService $ embeddingService ;
3935
4036 protected function configure (): void
4137 {
@@ -66,7 +62,7 @@ private function initializeServices(): void
6662 $ config = new Config ('http://localhost ' , 6333 );
6763 $ transport = new Transport (new Psr18Client (), $ config );
6864 $ this ->qdrantClient = new Qdrant ($ transport );
69- $ this ->embedder = pipeline (Task::Embeddings, ' onnx-community/Qwen3-Embedding-0.6B-ONNX ' );
65+ $ this ->embeddingService = new EmbeddingConfigService ( );
7066 }
7167
7268 private function loadProductData (): \Generator
@@ -99,7 +95,8 @@ private function prepareCollection(): void
9995 }
10096
10197 $ createCollection = new CreateCollection ();
102- $ createCollection ->addVector (new VectorParams (1024 , VectorParams::DISTANCE_COSINE ), 'default ' );
98+ $ vectorSize = $ this ->embeddingService ->getVectorSize ();
99+ $ createCollection ->addVector (new VectorParams ($ vectorSize , VectorParams::DISTANCE_COSINE ), 'default ' );
103100 $ this ->qdrantClient ->collections (self ::COLLECTION_NAME )->create ($ createCollection );
104101 }
105102
@@ -141,7 +138,7 @@ private function processBatch(array $batch): void
141138 }
142139
143140 $ text .= ' ' .$ product ['brand ' ].' ' .$ product ['category ' ];
144- $ embedding = ( $ this ->embedder ) ($ text, pooling: ' mean ' , normalize: true );
141+ $ vector = $ this ->embeddingService -> createEmbedding ($ text );
145142
146143 $ payload = [
147144 'name ' => $ product ['name ' ],
@@ -151,12 +148,6 @@ private function processBatch(array $batch): void
151148 'description ' => $ product ['description ' ],
152149 ];
153150
154- if (is_array ($ embedding )) {
155- $ vector = $ embedding [0 ];
156- } else {
157- $ vector = $ embedding instanceof Tensor ? $ embedding [0 ] : [];
158- }
159-
160151 $ pointsStruct ->addPoint (
161152 new PointStruct (
162153 $ product ['id ' ],
0 commit comments