]> BookStack Code Mirror - bookstack/blob - app/Search/Vectors/Services/OpenAiVectorQueryService.php
8d291099846be3e3a3c9523903bd78961e0f882c
[bookstack] / app / Search / Vectors / Services / OpenAiVectorQueryService.php
1 <?php
2
3 namespace BookStack\Search\Vectors\Services;
4
5 use BookStack\Http\HttpRequestService;
6
7 class OpenAiVectorQueryService implements VectorQueryService
8 {
9     public function __construct(
10         protected string $endpoint,
11         protected string $key,
12         protected HttpRequestService $http,
13     ) {
14     }
15
16     protected function jsonRequest(string $method, string $uri, array $data): array
17     {
18         $fullUrl = rtrim($this->endpoint, '/') . '/' . ltrim($uri, '/');
19         $client = $this->http->buildClient(10);
20         $request = $this->http->jsonRequest($method, $fullUrl, $data)
21             ->withHeader('Authorization', 'Bearer ' . $this->key);
22
23         $response = $client->sendRequest($request);
24         return json_decode($response->getBody()->getContents(), true);
25     }
26
27     public function generateEmbeddings(string $text): array
28     {
29         $response = $this->jsonRequest('POST', 'v1/embeddings', [
30             'input' => $text,
31             'model' => 'text-embedding-3-small',
32         ]);
33
34         return $response['data'][0]['embedding'];
35     }
36 }