44
55namespace NeuronAI \RAG \Embeddings ;
66
7- use GuzzleHttp \Client ;
87use 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 ;
1113use function array_chunk ;
1214use function array_map ;
1315use function array_merge ;
1416
1517class 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 }
0 commit comments