Skip to content

Commit 48869e0

Browse files
committed
Merge branch '3.x'
2 parents 42a1042 + 2471687 commit 48869e0

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

src/RAG/Embeddings/OpenAIEmbeddingsProvider.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,48 @@
44

55
namespace NeuronAI\RAG\Embeddings;
66

7-
use GuzzleHttp\Client;
87
use NeuronAI\RAG\Document;
8+
use NeuronAI\HttpClient\GuzzleHttpClient;
9+
use NeuronAI\HttpClient\HasHttpClient;
10+
use NeuronAI\HttpClient\HttpClientInterface;
11+
use NeuronAI\HttpClient\HttpRequest;
912

10-
use function json_decode;
1113
use function array_chunk;
1214
use function array_map;
1315
use function array_merge;
1416

1517
class OpenAIEmbeddingsProvider extends AbstractEmbeddingsProvider
1618
{
17-
protected Client $client;
19+
use HasHttpClient;
1820

1921
protected string $baseUri = 'https://api.openai.com/v1/embeddings';
2022

2123
public function __construct(
2224
protected string $key,
2325
protected string $model,
24-
protected ?int $dimensions = 1024
26+
protected ?int $dimensions = 1024,
27+
?HttpClientInterface $httpClient = null,
2528
) {
26-
$this->client = new Client([
27-
'base_uri' => $this->baseUri,
28-
'headers' => [
29+
$this->httpClient = ($httpClient ?? new GuzzleHttpClient())
30+
->withBaseUri($this->baseUri)
31+
->withHeaders([
2932
'Accept' => 'application/json',
3033
'Content-Type' => 'application/json',
3134
'Authorization' => 'Bearer ' . $this->key,
32-
]
33-
]);
35+
]);
3436
}
3537

3638
public function embedDocuments(array $documents): array
3739
{
3840
$chunks = array_chunk($documents, 100);
3941

4042
foreach ($chunks as $chunk) {
41-
$response = $this->client->post('', [
42-
'json' => [
43-
'model' => $this->model,
44-
'input' => array_map(fn (Document $document): string => $document->getContent(), $chunk),
45-
'encoding_format' => 'float',
46-
...($this->dimensions ? ['dimensions' => $this->dimensions] : []),
47-
48-
]
49-
])->getBody()->getContents();
50-
51-
$response = json_decode($response, true);
43+
$response = $this->httpClient->request(HttpRequest::post('', [
44+
'model' => $this->model,
45+
'input' => array_map(fn (Document $document): string => $document->getContent(), $chunk),
46+
'encoding_format' => 'float',
47+
...($this->dimensions ? ['dimensions' => $this->dimensions] : []),
48+
]))->json();
5249

5350
foreach ($response['data'] as $index => $item) {
5451
$chunk[$index]->embedding = $item['embedding'];
@@ -60,17 +57,12 @@ public function embedDocuments(array $documents): array
6057

6158
public function embedText(string $text): array
6259
{
63-
$response = $this->client->post('', [
64-
'json' => [
65-
'model' => $this->model,
66-
'input' => $text,
67-
'encoding_format' => 'float',
68-
...($this->dimensions ? ['dimensions' => $this->dimensions] : []),
69-
70-
]
71-
])->getBody()->getContents();
72-
73-
$response = json_decode($response, true);
60+
$response = $this->httpClient->request(HttpRequest::post('', [
61+
'model' => $this->model,
62+
'input' => $text,
63+
'encoding_format' => 'float',
64+
...($this->dimensions ? ['dimensions' => $this->dimensions] : []),
65+
]))->json();
7466

7567
return $response['data'][0]['embedding'];
7668
}

src/RAG/VectorStore/QdrantVectorStore.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,7 @@ protected function initialize(): void
5454
return;
5555
}
5656

57-
$this->httpClient->request(
58-
HttpRequest::put(
59-
uri: '',
60-
body: [
61-
'vectors' => [
62-
'size' => $this->dimension,
63-
'distance' => 'Cosine',
64-
],
65-
],
66-
)
67-
);
57+
$this->createCollection();
6858
}
6959

7060
/**
@@ -182,4 +172,19 @@ public function similaritySearch(array $embedding): iterable
182172
return $document;
183173
}, $response['result']['points']);
184174
}
175+
176+
protected function createCollection(): void
177+
{
178+
$this->httpClient->request(
179+
HttpRequest::put(
180+
uri: '',
181+
body: [
182+
'vectors' => [
183+
'size' => $this->dimension,
184+
'distance' => 'Cosine',
185+
],
186+
],
187+
)
188+
);
189+
}
185190
}

0 commit comments

Comments
 (0)