Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InferenceClient | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| embed | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| rerank | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| listModels | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| handleResponse | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Mbvb1223\Pinecone\Inference; |
| 6 | |
| 7 | use GuzzleHttp\Client; |
| 8 | use GuzzleHttp\Exception\GuzzleException; |
| 9 | use Mbvb1223\Pinecone\Utils\Configuration; |
| 10 | use Mbvb1223\Pinecone\Errors\PineconeApiException; |
| 11 | use Mbvb1223\Pinecone\Errors\PineconeException; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | |
| 14 | class InferenceClient |
| 15 | { |
| 16 | private Client $httpClient; |
| 17 | private Configuration $config; |
| 18 | |
| 19 | public function __construct(Configuration $config) |
| 20 | { |
| 21 | $this->config = $config; |
| 22 | $this->httpClient = new Client([ |
| 23 | 'base_uri' => 'https://api.pinecone.io', |
| 24 | 'timeout' => $config->getTimeout(), |
| 25 | 'headers' => $config->getDefaultHeaders(), |
| 26 | ]); |
| 27 | } |
| 28 | |
| 29 | public function embed(string $model, array $inputs, array $parameters = []): array |
| 30 | { |
| 31 | try { |
| 32 | $payload = [ |
| 33 | 'model' => $model, |
| 34 | 'inputs' => $inputs, |
| 35 | 'parameters' => $parameters, |
| 36 | ]; |
| 37 | |
| 38 | $response = $this->httpClient->post('/embed', [ |
| 39 | 'json' => $payload, |
| 40 | ]); |
| 41 | |
| 42 | return $this->handleResponse($response); |
| 43 | } catch (GuzzleException $e) { |
| 44 | throw new PineconeException('Failed to generate embeddings: ' . $e->getMessage(), 0, $e); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function rerank(string $model, string $query, array $documents, array $parameters = []): array |
| 49 | { |
| 50 | try { |
| 51 | $payload = [ |
| 52 | 'model' => $model, |
| 53 | 'query' => $query, |
| 54 | 'documents' => $documents, |
| 55 | 'parameters' => $parameters, |
| 56 | ]; |
| 57 | |
| 58 | $response = $this->httpClient->post('/rerank', [ |
| 59 | 'json' => $payload, |
| 60 | ]); |
| 61 | |
| 62 | return $this->handleResponse($response); |
| 63 | } catch (GuzzleException $e) { |
| 64 | throw new PineconeException('Failed to rerank documents: ' . $e->getMessage(), 0, $e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function listModels(): array |
| 69 | { |
| 70 | try { |
| 71 | $response = $this->httpClient->get('/models'); |
| 72 | |
| 73 | return $this->handleResponse($response); |
| 74 | } catch (GuzzleException $e) { |
| 75 | throw new PineconeException('Failed to list models: ' . $e->getMessage(), 0, $e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private function handleResponse(ResponseInterface $response): array |
| 80 | { |
| 81 | $statusCode = $response->getStatusCode(); |
| 82 | $body = $response->getBody()->getContents(); |
| 83 | |
| 84 | if ($statusCode >= 400) { |
| 85 | $data = json_decode($body, true) ?? []; |
| 86 | $message = $data['message'] ?? 'API request failed'; |
| 87 | throw new PineconeApiException($message, $statusCode, $data); |
| 88 | } |
| 89 | |
| 90 | if (empty($body)) { |
| 91 | return []; |
| 92 | } |
| 93 | |
| 94 | $decoded = json_decode($body, true); |
| 95 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 96 | throw new PineconeException('Failed to decode JSON response: ' . json_last_error_msg()); |
| 97 | } |
| 98 | |
| 99 | return $decoded; |
| 100 | } |
| 101 | } |