Skip to content

Commit 7af2b57

Browse files
committed
Add async option to qdrant store
1 parent fc32bc6 commit 7af2b57

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

src/store/src/Bridge/Qdrant/Store.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(
3333
private readonly string $collectionName,
3434
private readonly int $embeddingsDimension = 1536,
3535
private readonly string $embeddingsDistance = 'Cosine',
36+
private readonly bool $async = false,
3637
) {
3738
}
3839

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

5960
public function add(VectorDocument ...$documents): void
6061
{
61-
$this->request('PUT', \sprintf('collections/%s/points', $this->collectionName), [
62-
'points' => array_map($this->convertToIndexableArray(...), $documents),
63-
]);
62+
$this->request(
63+
'PUT',
64+
\sprintf('collections/%s/points', $this->collectionName),
65+
[
66+
'points' => array_map($this->convertToIndexableArray(...), $documents),
67+
],
68+
['wait' => $this->async ? 'false' : 'true'],
69+
);
6470
}
6571

6672
/**
@@ -102,17 +108,19 @@ public function drop(): void
102108

103109
/**
104110
* @param array<string, mixed> $payload
111+
* @param array<string, mixed> $queryParameters
105112
*
106113
* @return array<string, mixed>
107114
*/
108-
private function request(string $method, string $endpoint, array $payload = []): array
115+
private function request(string $method, string $endpoint, array $payload = [], array $queryParameters = []): array
109116
{
110117
$url = \sprintf('%s/%s', $this->endpointUrl, $endpoint);
111118

112119
$response = $this->httpClient->request($method, $url, [
113120
'headers' => [
114121
'api-key' => $this->apiKey,
115122
],
123+
'query' => $queryParameters,
116124
'json' => $payload,
117125
]);
118126

@@ -146,7 +154,7 @@ private function convertToVectorDocument(array $data): VectorDocument
146154
id: Uuid::fromString($id),
147155
vector: $vector,
148156
metadata: new Metadata($data['payload']),
149-
score: $data['score'] ?? null
157+
score: $data['score'] ?? null,
150158
);
151159
}
152160
}

src/store/tests/Bridge/Qdrant/StoreTest.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,52 @@ public function testStoreCannotAddOnInvalidResponse()
145145
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');
146146

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

153153
public function testStoreCanAdd()
154154
{
155-
$httpClient = new MockHttpClient([
156-
new JsonMockResponse([
155+
$document = new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3]));
156+
157+
$httpClient = new MockHttpClient(static function (string $method, string $url, array $options) use ($document): JsonMockResponse {
158+
self::assertArrayHasKey('wait', $options['query']);
159+
self::assertEquals('true', $options['query']['wait']);
160+
161+
return new JsonMockResponse([
162+
'time' => 0.002,
163+
'status' => 'ok',
164+
'result' => [
165+
'points' => [
166+
[
167+
'id' => (string) $document->id,
168+
'payload' => (array) $document->metadata,
169+
'vector' => $document->vector->getData(),
170+
],
171+
],
172+
],
173+
], [
174+
'http_code' => 200,
175+
]);
176+
}, 'http://127.0.0.1:6333');
177+
178+
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');
179+
180+
$store->add($document);
181+
182+
$this->assertSame(1, $httpClient->getRequestsCount());
183+
}
184+
185+
public function testStoreCanAddAsynchronously()
186+
{
187+
$document = new VectorDocument(Uuid::v4(), new Vector([0.1, 0.2, 0.3]));
188+
189+
$httpClient = new MockHttpClient(static function (string $method, string $url, array $options) use ($document): JsonMockResponse {
190+
self::assertArrayHasKey('wait', $options['query']);
191+
self::assertEquals('false', $options['query']['wait']);
192+
193+
return new JsonMockResponse([
157194
'time' => 0.002,
158195
'status' => 'ok',
159196
'result' => [
@@ -162,12 +199,12 @@ public function testStoreCanAdd()
162199
],
163200
], [
164201
'http_code' => 200,
165-
]),
166-
], 'http://127.0.0.1:6333');
202+
]);
203+
}, 'http://127.0.0.1:6333');
167204

168-
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');
205+
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test', async: true);
169206

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

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

0 commit comments

Comments
 (0)