Skip to content

Commit 60e4034

Browse files
committed
[Store] Add $options parameter to Vectorizer methods for platform configuration
1 parent e7be456 commit 60e4034

File tree

3 files changed

+159
-8
lines changed

3 files changed

+159
-8
lines changed

src/store/src/Document/Vectorizer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public function __construct(
2828
) {
2929
}
3030

31-
public function vectorizeTextDocuments(array $documents): array
31+
public function vectorizeTextDocuments(array $documents, array $options = []): array
3232
{
3333
$documentCount = \count($documents);
3434
$this->logger->info('Starting vectorization process', ['document_count' => $documentCount]);
3535

3636
if ($this->model->supports(Capability::INPUT_MULTIPLE)) {
3737
$this->logger->debug('Using batch vectorization with model that supports multiple inputs');
38-
$result = $this->platform->invoke($this->model, array_map(fn (TextDocument $document) => $document->content, $documents));
38+
$result = $this->platform->invoke($this->model, array_map(fn (TextDocument $document) => $document->content, $documents), $options);
3939

4040
$vectors = $result->asVectors();
4141
$this->logger->debug('Batch vectorization completed', ['vector_count' => \count($vectors)]);
@@ -44,7 +44,7 @@ public function vectorizeTextDocuments(array $documents): array
4444
$results = [];
4545
foreach ($documents as $i => $document) {
4646
$this->logger->debug('Vectorizing document', ['document_index' => $i, 'document_id' => $document->id]);
47-
$results[] = $this->platform->invoke($this->model, $document->content);
47+
$results[] = $this->platform->invoke($this->model, $document->content, $options);
4848
}
4949

5050
$vectors = [];
@@ -67,11 +67,11 @@ public function vectorizeTextDocuments(array $documents): array
6767
return $vectorDocuments;
6868
}
6969

70-
public function vectorize(string|\Stringable $string): Vector
70+
public function vectorize(string|\Stringable $string, array $options = []): Vector
7171
{
7272
$this->logger->debug('Vectorizing string', ['string' => (string) $string]);
7373

74-
$result = $this->platform->invoke($this->model, (string) $string);
74+
$result = $this->platform->invoke($this->model, (string) $string, $options);
7575
$vectors = $result->asVectors();
7676

7777
if (!isset($vectors[0])) {

src/store/src/Document/VectorizerInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
interface VectorizerInterface
2323
{
2424
/**
25-
* @param TextDocument[] $documents
25+
* @param TextDocument[] $documents
26+
* @param array<string, mixed> $options Options to pass to the underlying platform
2627
*
2728
* @return VectorDocument[]
2829
*/
29-
public function vectorizeTextDocuments(array $documents): array;
30+
public function vectorizeTextDocuments(array $documents, array $options = []): array;
3031

3132
/**
3233
* Vectorizes a single string or Stringable object into a Vector.
34+
*
35+
* @param array<string, mixed> $options Options to pass to the underlying platform
3336
*/
34-
public function vectorize(string|\Stringable $string): Vector;
37+
public function vectorize(string|\Stringable $string, array $options = []): Vector;
3538
}

src/store/tests/Document/VectorizerTest.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\AI\Platform\Model;
2222
use Symfony\AI\Platform\ModelClientInterface;
2323
use Symfony\AI\Platform\Platform;
24+
use Symfony\AI\Platform\PlatformInterface;
2425
use Symfony\AI\Platform\Result\RawHttpResult;
2526
use Symfony\AI\Platform\Result\RawResultInterface;
2627
use Symfony\AI\Platform\Result\ResultInterface;
@@ -379,4 +380,151 @@ public function testVectorizeStringThrowsExceptionWhenNoVectorReturned()
379380

380381
$vectorizer->vectorize($text);
381382
}
383+
384+
public function testVectorizeTextDocumentsPassesOptionsToInvoke()
385+
{
386+
$documents = [
387+
new TextDocument(Uuid::v4(), 'Test document', new Metadata(['source' => 'test'])),
388+
];
389+
390+
$vector = new Vector([0.1, 0.2, 0.3]);
391+
$options = ['max_tokens' => 1000, 'temperature' => 0.5];
392+
393+
$platform = $this->createMock(PlatformInterface::class);
394+
$platform->expects($this->once())
395+
->method('invoke')
396+
->with(
397+
$this->isInstanceOf(Model::class),
398+
$this->equalTo('Test document'),
399+
$this->equalTo($options)
400+
)
401+
->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class)));
402+
403+
$model = new Embeddings();
404+
405+
$vectorizer = new Vectorizer($platform, $model);
406+
$result = $vectorizer->vectorizeTextDocuments($documents, $options);
407+
408+
$this->assertCount(1, $result);
409+
$this->assertEquals($vector, $result[0]->vector);
410+
}
411+
412+
public function testVectorizeTextDocumentsWithEmptyOptions()
413+
{
414+
$documents = [
415+
new TextDocument(Uuid::v4(), 'Test document'),
416+
];
417+
418+
$vector = new Vector([0.1, 0.2, 0.3]);
419+
420+
$platform = $this->createMock(PlatformInterface::class);
421+
$platform->expects($this->once())
422+
->method('invoke')
423+
->with(
424+
$this->isInstanceOf(Model::class),
425+
$this->equalTo('Test document'),
426+
$this->equalTo([])
427+
)
428+
->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class)));
429+
430+
$model = new Embeddings();
431+
432+
$vectorizer = new Vectorizer($platform, $model);
433+
$result = $vectorizer->vectorizeTextDocuments($documents);
434+
435+
$this->assertCount(1, $result);
436+
$this->assertEquals($vector, $result[0]->vector);
437+
}
438+
439+
public function testVectorizeStringPassesOptionsToInvoke()
440+
{
441+
$text = 'Test string';
442+
$vector = new Vector([0.1, 0.2, 0.3]);
443+
$options = ['temperature' => 0.7, 'max_tokens' => 500];
444+
445+
$platform = $this->createMock(PlatformInterface::class);
446+
$platform->expects($this->once())
447+
->method('invoke')
448+
->with(
449+
$this->isInstanceOf(Model::class),
450+
$this->equalTo($text),
451+
$this->equalTo($options)
452+
)
453+
->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class)));
454+
455+
$model = new Embeddings();
456+
457+
$vectorizer = new Vectorizer($platform, $model);
458+
$result = $vectorizer->vectorize($text, $options);
459+
460+
$this->assertEquals($vector, $result);
461+
}
462+
463+
public function testVectorizeStringWithEmptyOptions()
464+
{
465+
$text = 'Test string';
466+
$vector = new Vector([0.1, 0.2, 0.3]);
467+
468+
$platform = $this->createMock(PlatformInterface::class);
469+
$platform->expects($this->once())
470+
->method('invoke')
471+
->with(
472+
$this->isInstanceOf(Model::class),
473+
$this->equalTo($text),
474+
$this->equalTo([])
475+
)
476+
->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class)));
477+
478+
$model = new Embeddings();
479+
480+
$vectorizer = new Vectorizer($platform, $model);
481+
$result = $vectorizer->vectorize($text);
482+
483+
$this->assertEquals($vector, $result);
484+
}
485+
486+
public function testVectorizeTextDocumentsWithoutBatchSupportPassesOptions()
487+
{
488+
$model = $this->createMock(Model::class);
489+
$model->expects($this->once())
490+
->method('supports')
491+
->with(Capability::INPUT_MULTIPLE)
492+
->willReturn(false);
493+
494+
$documents = [
495+
new TextDocument(Uuid::v4(), 'Document 1'),
496+
new TextDocument(Uuid::v4(), 'Document 2'),
497+
];
498+
499+
$vectors = [
500+
new Vector([0.1, 0.2]),
501+
new Vector([0.3, 0.4]),
502+
];
503+
504+
$options = ['max_tokens' => 2000];
505+
506+
$platform = $this->createMock(PlatformInterface::class);
507+
508+
$invokeCallCount = 0;
509+
$platform->expects($this->exactly(2))
510+
->method('invoke')
511+
->willReturnCallback(function ($passedModel, $passedContent, $passedOptions) use ($options, $vectors, &$invokeCallCount) {
512+
$this->assertInstanceOf(Model::class, $passedModel);
513+
$this->assertEquals($options, $passedOptions);
514+
515+
$expectedContent = 0 === $invokeCallCount ? 'Document 1' : 'Document 2';
516+
$this->assertEquals($expectedContent, $passedContent);
517+
518+
$vector = $vectors[$invokeCallCount++];
519+
520+
return new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class));
521+
});
522+
523+
$vectorizer = new Vectorizer($platform, $model);
524+
$result = $vectorizer->vectorizeTextDocuments($documents, $options);
525+
526+
$this->assertCount(2, $result);
527+
$this->assertEquals($vectors[0], $result[0]->vector);
528+
$this->assertEquals($vectors[1], $result[1]->vector);
529+
}
382530
}

0 commit comments

Comments
 (0)