Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@
->stringNode('collection_name')->cannotBeEmpty()->end()
->integerNode('dimensions')->end()
->stringNode('distance')->end()
->booleanNode('async')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
4 changes: 4 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
$arguments[5] = $store['distance'];
}

if (\array_key_exists('async', $store)) {
$arguments[6] = $store['async'];
}

$definition = new Definition(QdrantStore::class);
$definition
->addTag('ai.store')
Expand Down
1 change: 1 addition & 0 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2942,6 +2942,7 @@ private function getFullConfig(): array
'collection_name' => 'foo',
'dimensions' => 768,
'distance' => 'Cosine',
'async' => false,
],
],
'redis' => [
Expand Down
16 changes: 12 additions & 4 deletions src/store/src/Bridge/Qdrant/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
private readonly string $collectionName,
private readonly int $embeddingsDimension = 1536,
private readonly string $embeddingsDistance = 'Cosine',
private readonly bool $async = false,
) {
}

Expand All @@ -58,9 +59,14 @@ public function setup(array $options = []): void

public function add(VectorDocument ...$documents): void
{
$this->request('PUT', \sprintf('collections/%s/points', $this->collectionName), [
'points' => array_map($this->convertToIndexableArray(...), $documents),
]);
$this->request(
'PUT',
\sprintf('collections/%s/points', $this->collectionName),
[
'points' => array_map($this->convertToIndexableArray(...), $documents),
],
['wait' => $this->async ? 'false' : 'true'],
);
}

/**
Expand Down Expand Up @@ -102,17 +108,19 @@ public function drop(): void

/**
* @param array<string, mixed> $payload
* @param array<string, mixed> $queryParameters
*
* @return array<string, mixed>
*/
private function request(string $method, string $endpoint, array $payload = []): array
private function request(string $method, string $endpoint, array $payload = [], array $queryParameters = []): array
{
$url = \sprintf('%s/%s', $this->endpointUrl, $endpoint);

$response = $this->httpClient->request($method, $url, [
'headers' => [
'api-key' => $this->apiKey,
],
'query' => $queryParameters,
'json' => $payload,
]);

Expand Down
51 changes: 44 additions & 7 deletions src/store/tests/Bridge/Qdrant/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,52 @@ public function testStoreCannotAddOnInvalidResponse()
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');

$this->expectException(ClientException::class);
$this->expectExceptionMessage('HTTP 400 returned for "http://127.0.0.1:6333/collections/test/points".');
$this->expectExceptionMessage('HTTP 400 returned for "http://127.0.0.1:6333/collections/test/points?wait=true".');
$this->expectExceptionCode(400);
$store->add(new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3])));
}

public function testStoreCanAdd()
{
$httpClient = new MockHttpClient([
new JsonMockResponse([
$document = new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3]));

$httpClient = new MockHttpClient(static function (string $method, string $url, array $options) use ($document): JsonMockResponse {
self::assertArrayHasKey('wait', $options['query']);
self::assertEquals('true', $options['query']['wait']);

return new JsonMockResponse([
'time' => 0.002,
'status' => 'ok',
'result' => [
'points' => [
[
'id' => (string) $document->id,
'payload' => (array) $document->metadata,
'vector' => $document->vector->getData(),
],
],
],
], [
'http_code' => 200,
]);
}, 'http://127.0.0.1:6333');

$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');

$store->add($document);

$this->assertSame(1, $httpClient->getRequestsCount());
}

public function testStoreCanAddAsynchronously()
{
$document = new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3]));

$httpClient = new MockHttpClient(static function (string $method, string $url, array $options): JsonMockResponse {
self::assertArrayHasKey('wait', $options['query']);
self::assertEquals('false', $options['query']['wait']);

return new JsonMockResponse([
'time' => 0.002,
'status' => 'ok',
'result' => [
Expand All @@ -162,12 +199,12 @@ public function testStoreCanAdd()
],
], [
'http_code' => 200,
]),
], 'http://127.0.0.1:6333');
]);
}, 'http://127.0.0.1:6333');

$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test', async: true);

$store->add(new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3])));
$store->add($document);

$this->assertSame(1, $httpClient->getRequestsCount());
}
Expand Down