Skip to content

Commit d71c237

Browse files
committed
Add endpoint to compute embeddings
1 parent 3101e40 commit d71c237

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

src/Connectors/LibrarianConnector.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace OneOffTech\LibrarianClient\Connectors;
44

5+
use OneOffTech\LibrarianClient\Dto\Embeddings;
6+
use OneOffTech\LibrarianClient\Requests\Embeddings\EmbeddingsRequest;
57
use OneOffTech\LibrarianClient\Resources\ClassifierResource;
68
use OneOffTech\LibrarianClient\Resources\DocumentResource;
79
use OneOffTech\LibrarianClient\Resources\LibraryResource;
@@ -120,4 +122,15 @@ public function extractions(string $library_id): StructuredExtractionResource
120122
{
121123
return new StructuredExtractionResource($library_id, $this);
122124
}
125+
126+
/**
127+
* Compute the embeddings of a text
128+
*/
129+
public function embeddings(array $text): Embeddings
130+
{
131+
return $this->send(new EmbeddingsRequest(
132+
text: $text
133+
))
134+
->dto();
135+
}
123136
}

src/Dto/Embeddings.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace OneOffTech\LibrarianClient\Dto;
4+
5+
use Saloon\Contracts\DataObjects\WithResponse;
6+
use Saloon\Traits\Responses\HasResponse;
7+
8+
class Embeddings implements WithResponse
9+
{
10+
use HasResponse;
11+
12+
public function __construct(
13+
public readonly array $vectors,
14+
) {}
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace OneOffTech\LibrarianClient\Requests\Embeddings;
4+
5+
use OneOffTech\LibrarianClient\Dto\Document;
6+
use OneOffTech\LibrarianClient\Dto\Embeddings;
7+
use OneOffTech\LibrarianClient\Dto\Extraction;
8+
use OneOffTech\LibrarianClient\Dto\ResponseSchema;
9+
use Saloon\Contracts\Body\HasBody;
10+
use Saloon\Enums\Method;
11+
use Saloon\Http\Request;
12+
use Saloon\Http\Response;
13+
use Saloon\Traits\Body\HasJsonBody;
14+
15+
class EmbeddingsRequest extends Request implements HasBody
16+
{
17+
use HasJsonBody;
18+
19+
protected Method $method = Method::POST;
20+
21+
public function __construct(
22+
protected readonly array $text,
23+
) {
24+
//
25+
}
26+
27+
public function resolveEndpoint(): string
28+
{
29+
return "/embeddings";
30+
}
31+
32+
protected function defaultBody(): array
33+
{
34+
return [
35+
'corpus' => $this->text,
36+
];
37+
}
38+
39+
public function createDtoFromResponse(Response $response): Embeddings
40+
{
41+
$data = $response->json();
42+
43+
return (new Embeddings(
44+
vectors: $data,
45+
))->setResponse($response);
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace OneOffTech\LibrarianClient\Tests\Embeddings;
4+
5+
use OneOffTech\LibrarianClient\Dto\Document;
6+
use OneOffTech\LibrarianClient\Requests\Embeddings\EmbeddingsRequest;
7+
use OneOffTech\LibrarianClient\Requests\StructuredExtraction\ExtractRequest;
8+
use OneOffTech\LibrarianClient\Tests\Base;
9+
use Saloon\Http\Faking\MockClient;
10+
use Saloon\Http\Faking\MockResponse;
11+
use Saloon\Http\Request;
12+
13+
class EmbeddingsTest extends Base
14+
{
15+
public function test_embeddings_computed(): void
16+
{
17+
$mockClient = MockClient::global([
18+
EmbeddingsRequest::class => MockResponse::fixture('embeddings'),
19+
]);
20+
21+
$connector = $this->connector($mockClient);
22+
23+
$embeddings = $connector->embeddings(['Test to compute the embedding']);
24+
25+
$mockClient->assertSent(EmbeddingsRequest::class);
26+
27+
$mockClient->assertSentCount(1);
28+
29+
$mockClient->assertSent(function (Request $request) {
30+
$body = $request->body()->all();
31+
32+
return $request->resolveEndpoint() === '/embeddings' &&
33+
$body['corpus'] === ['Test to compute the embedding'];
34+
});
35+
36+
$this->assertCount(1, $embeddings->vectors);
37+
38+
}
39+
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"statusCode": 200,
3+
"headers": {
4+
"content-length": "4709",
5+
"content-type": "application\/json"
6+
},
7+
"data": "[[-0.059276048,0.010882638,0.0041751303,0.0044194735,-0.0045263544,0.038700864,0.0114495745,-0.000969711,0.019714762,-0.0010636612,-0.025041018,-0.06351389,0.04281405,-0.0070718033,0.042404354,0.020706765,-0.017039016,0.09454101,-0.048695374,-0.002268183,0.060676485,-0.04520458,-0.037901394,-0.089893565,-0.038756937,0.043915972,0.029866138,-0.02612728,-0.007817831,-0.1791175,0.006456259,-0.09688031,0.11209115,0.008081085,0.06423214,-0.030936973,-0.03940218,0.025976136,-0.014117952,0.02393579,0.0061896467,0.010723438,-0.032414325,-0.036206346,0.013727928,-0.026197758,-0.029462624,-0.009658031,-0.028784785,0.010383157,-0.023361402,-0.041900884,0.01468643,0.013680671,0.050526246,-0.004537982,0.0495804,0.0117429765,-0.0036867084,0.045361802,-0.027166333,0.07533278,-0.09475587,0.033467956,0.04153689,0.053562403,-0.012551707,-0.03700496,0.044265836,0.096501276,-0.0039095986,0.022646382,-0.019990439,0.039986476,0.014924757,0.08406484,0.04281346,0.03687196,-0.01679755,-0.036614846,-0.0469818,0.012227081,0.054127082,0.009721339,-0.0139069855,0.0032064903,0.008678526,0.030516867,0.0027732323,-0.0033631083,-0.019156452,0.038758814,-0.015777288,0.0013411902,-0.03307592,-0.039994277,0.035478603,-0.006635605,0.049658254,0.3235882,-0.061168928,0.018621622,0.014812677,-0.028753288,-0.010965886,-0.08984572,0.018975848,-0.04595667,-0.076511905,-0.033455137,-0.0862197,-0.04549945,0.03614352,-0.045933045,-0.009157341,0.0040652044,0.055733632,0.03665857,-0.038531106,-0.015266282,-0.043110777,0.011419103,0.03576029,-0.02378604,0.06238532,-0.0026116546,0.04775437,0.1271381,0.0116365785,0.045899604,-0.040017616,-0.014143485,-0.014419755,0.023693578,-0.01183855,0.051190797,-0.020979822,0.009374239,-0.034226827,0.052027028,-0.042007003,0.042535886,0.025600221,0.016220115,-0.040890165,0.0915586,-0.06865674,0.00615741,-0.023821482,-0.046796016,0.03728477,0.03207936,-0.026745701,-0.0672096,0.003549044,0.023001797,-0.001314941,-0.023304295,-0.020152256,0.019569002,-0.02267861,-0.020126289,-0.0506385,0.10234009,-0.002298541,-0.025503756,0.004935542,-0.009135343,0.069100715,-0.07358561,0.05785394,0.014807494,-0.044770274,0.043227896,0.012372963,-0.03146985,-0.064335406,0.023552842,0.05502467,-0.0046599815,0.026844284,-0.038686194,-0.08980028,0.053589933,0.02202639,-0.02556853,-0.01698453,-0.0024267363,0.016542029,0.0076143434,0.03761201,-0.057005633,-0.08452472,0.0031875083,-0.03689876,-0.05651994,0.02267959,0.0051652915,-0.05365435,-0.024223711,0.08292658,0.052933205,0.03705129,0.004081317,-0.037219215,0.00858338,0.012325801,-0.016467426,0.04193746,0.09599649,0.015155182,-0.077653624,0.022573922,-0.015813231,0.034994744,0.02512482,0.027755301,0.016075835,0.020052465,0.02744323,-0.06073945,-0.069616415,-0.00787447,-0.31324807,-0.031185606,0.027859077,0.014033041,-0.0059174644,-0.051186807,0.058523826,0.021442043,0.09636708,0.013294621,0.017595686,0.012861808,-0.03191916,0.04610303,0.00666075,0.005393273,0.014449419,-0.003356712,-0.035564873,0.0021108156,-0.043918513,0.0041876915,0.019378273,-0.017595446,0.03427904,-0.04223833,0.20629506,0.014577919,0.03346714,-0.029496891,0.009899783,0.020232385,-0.055584293,-0.08073637,0.060712483,-0.0037059449,-0.037646264,0.045504898,0.030514155,-0.043069884,-0.02019447,-0.009330043,0.017569074,-0.09229143,0.015949346,-0.03573113,-0.017352553,-0.007871505,-0.07171557,0.038666625,0.0462308,-0.05734667,0.060921025,0.02494968,-0.04691088,-0.022936752,-0.053107943,-0.04088555,-0.025756354,0.016232468,0.027838124,-0.0595005,-0.026318403,-0.05713916,0.020908019,-0.031951815,-0.009022139,-0.031208085,0.012008347,-0.019754881,-0.05173871,-0.010691994,0.016494976,0.02397478,0.050285093,-0.027854055,0.030410504,0.005068105,0.014084492,0.022593029,0.034113962,0.033599965,0.037721485,-0.029907666,0.029115232,0.005079528,0.02360313,0.038897924,0.054030284,-0.016548732,-0.056719277,-0.028295757,0.004553482,-0.02534995,-0.01763806,-0.008069128,-0.24710673,-0.011691595,0.003782448,0.059422925,-0.036797184,-0.057800733,0.02677965,-0.00578621,-0.04804559,-0.020746399,0.040168423,0.03712978,0.073478006,-0.044839967,0.014431565,0.0039305063,0.06223929,-0.08022141,0.021789037,-0.055024944,-0.027215136,0.0070375195,0.19694482,-0.07894451,0.016696244,-0.014190832,0.03313904,-0.03536337,0.05768876,-0.035070784,0.0078765815,0.006795496,0.056484737,-0.027205953,-0.026003905,0.076112024,0.025143908,-0.0072467355,0.029650003,-0.04075613,0.12291957,-0.05175761,-0.060461324,0.025615912,0.034360886,0.074414566,0.050787814,0.055335738,-0.038533695,-0.012350527,-0.005059135,-0.008172732,0.014297007,-0.015495004,0.034502372,0.045985162,-0.03759253,-0.0003800891,-0.03569466,-0.022567116,-0.029952014,-0.045143105,0.048340715,0.003178329,0.00037190755]]"
8+
}

0 commit comments

Comments
 (0)