Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
56 / 56 |
|
100.00% |
27 / 27 |
CRAP | |
100.00% |
1 / 1 |
| Pinecone | |
100.00% |
56 / 56 |
|
100.00% |
27 / 27 |
33 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
| inference | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| assistant | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| listIndexes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createForModel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| configureIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasIndex | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| createCollection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| listCollections | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeCollection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteCollection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createBackup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| listBackups | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeBackup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteBackup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createIndexFromBackup | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| listRestoreJobs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeRestoreJob | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createAssistant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| listAssistants | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeAssistant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateAssistant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteAssistant | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Mbvb1223\Pinecone; |
| 6 | |
| 7 | use GuzzleHttp\Client; |
| 8 | use Mbvb1223\Pinecone\Assistant\AssistantClient; |
| 9 | use Mbvb1223\Pinecone\Control\ControlPlane; |
| 10 | use Mbvb1223\Pinecone\Data\Index; |
| 11 | use Mbvb1223\Pinecone\Errors\PineconeException; |
| 12 | use Mbvb1223\Pinecone\Inference\InferenceClient; |
| 13 | use Mbvb1223\Pinecone\Utils\Configuration; |
| 14 | |
| 15 | class Pinecone |
| 16 | { |
| 17 | private readonly Configuration $config; |
| 18 | private readonly ControlPlane $controlPlane; |
| 19 | private ?InferenceClient $inferenceClient = null; |
| 20 | |
| 21 | /** @var array<string, Index> */ |
| 22 | private array $indexCache = []; |
| 23 | |
| 24 | /** |
| 25 | * @param array{controllerHost?: string, additionalHeaders?: array<string, string>, timeout?: int}|null $config |
| 26 | */ |
| 27 | public function __construct(?string $apiKey = null, ?array $config = null) |
| 28 | { |
| 29 | $this->config = new Configuration($apiKey, $config); |
| 30 | $client = new Client([ |
| 31 | 'base_uri' => $this->config->getControllerHost(), |
| 32 | 'timeout' => $this->config->getTimeout(), |
| 33 | 'headers' => $this->config->getDefaultHeaders(), |
| 34 | ]); |
| 35 | $this->controlPlane = new ControlPlane($client); |
| 36 | } |
| 37 | |
| 38 | // ===== Factory methods to get sub-components ===== |
| 39 | public function index(string $name): Index |
| 40 | { |
| 41 | if (trim($name) === '') { |
| 42 | throw new PineconeException('Index name must not be empty.'); |
| 43 | } |
| 44 | |
| 45 | if (isset($this->indexCache[$name])) { |
| 46 | return $this->indexCache[$name]; |
| 47 | } |
| 48 | |
| 49 | $indexInfo = $this->describeIndex($name); |
| 50 | $host = $indexInfo['host'] ?? null; |
| 51 | if (!$host) { |
| 52 | throw new PineconeException("Index '$name' does not have a host URL."); |
| 53 | } |
| 54 | $client = new Client([ |
| 55 | 'base_uri' => "https://{$host}", |
| 56 | 'timeout' => $this->config->getTimeout(), |
| 57 | 'headers' => $this->config->getDefaultHeaders(), |
| 58 | ]); |
| 59 | |
| 60 | $index = new Index($client); |
| 61 | $this->indexCache[$name] = $index; |
| 62 | |
| 63 | return $index; |
| 64 | } |
| 65 | |
| 66 | public function inference(): InferenceClient |
| 67 | { |
| 68 | if ($this->inferenceClient === null) { |
| 69 | $this->inferenceClient = new InferenceClient($this->config); |
| 70 | } |
| 71 | |
| 72 | return $this->inferenceClient; |
| 73 | } |
| 74 | |
| 75 | public function assistant(string $name): AssistantClient |
| 76 | { |
| 77 | if (trim($name) === '') { |
| 78 | throw new PineconeException('Assistant name must not be empty.'); |
| 79 | } |
| 80 | |
| 81 | $assistantInfo = $this->describeAssistant($name); |
| 82 | |
| 83 | return new AssistantClient($this->config, $name, $assistantInfo); |
| 84 | } |
| 85 | |
| 86 | // ===== Index control plane methods ===== |
| 87 | |
| 88 | /** @return array<int, array<string, mixed>> */ |
| 89 | public function listIndexes(): array |
| 90 | { |
| 91 | return $this->controlPlane->listIndexes(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param array{dimension?: int, metric?: string, spec?: array<string, mixed>, ...} $requestData |
| 96 | * @return array<string, mixed> |
| 97 | */ |
| 98 | public function createIndex(string $name, array $requestData): array |
| 99 | { |
| 100 | return $this->controlPlane->createIndex($name, $requestData); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param array{cloud: string, region: string, embed: array{model: string, ...}, ...} $requestData |
| 105 | * @return array<string, mixed> |
| 106 | */ |
| 107 | public function createForModel(string $name, array $requestData): array |
| 108 | { |
| 109 | return $this->controlPlane->createForModel($name, $requestData); |
| 110 | } |
| 111 | |
| 112 | /** @return array<string, mixed> */ |
| 113 | public function describeIndex(string $name): array |
| 114 | { |
| 115 | return $this->controlPlane->describeIndex($name); |
| 116 | } |
| 117 | |
| 118 | public function deleteIndex(string $name): void |
| 119 | { |
| 120 | $this->controlPlane->deleteIndex($name); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param array<string, mixed> $requestData |
| 125 | * @return array<string, mixed> |
| 126 | */ |
| 127 | public function configureIndex(string $name, array $requestData): array |
| 128 | { |
| 129 | return $this->controlPlane->configureIndex($name, $requestData); |
| 130 | } |
| 131 | |
| 132 | public function hasIndex(string $name): bool |
| 133 | { |
| 134 | try { |
| 135 | $this->describeIndex($name); |
| 136 | |
| 137 | return true; |
| 138 | } catch (PineconeException) { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // ===== Collection control plane methods ===== |
| 144 | |
| 145 | /** |
| 146 | * @param array{name: string, source: string} $config |
| 147 | * @return array<string, mixed> |
| 148 | */ |
| 149 | public function createCollection(array $config): array |
| 150 | { |
| 151 | return $this->controlPlane->createCollection($config); |
| 152 | } |
| 153 | |
| 154 | /** @return array<int, array<string, mixed>> */ |
| 155 | public function listCollections(): array |
| 156 | { |
| 157 | return $this->controlPlane->listCollections(); |
| 158 | } |
| 159 | |
| 160 | /** @return array<string, mixed> */ |
| 161 | public function describeCollection(string $name): array |
| 162 | { |
| 163 | return $this->controlPlane->describeCollection($name); |
| 164 | } |
| 165 | |
| 166 | public function deleteCollection(string $name): void |
| 167 | { |
| 168 | $this->controlPlane->deleteCollection($name); |
| 169 | } |
| 170 | |
| 171 | // ===== Backup control plane methods ===== |
| 172 | |
| 173 | /** |
| 174 | * @param array{source_index_name: string, name?: string, description?: string} $config |
| 175 | * @return array<string, mixed> |
| 176 | */ |
| 177 | public function createBackup(array $config): array |
| 178 | { |
| 179 | return $this->controlPlane->createBackup($config); |
| 180 | } |
| 181 | |
| 182 | /** @return array<int, array<string, mixed>> */ |
| 183 | public function listBackups(): array |
| 184 | { |
| 185 | return $this->controlPlane->listBackups(); |
| 186 | } |
| 187 | |
| 188 | /** @return array<string, mixed> */ |
| 189 | public function describeBackup(string $id): array |
| 190 | { |
| 191 | return $this->controlPlane->describeBackup($id); |
| 192 | } |
| 193 | |
| 194 | public function deleteBackup(string $id): void |
| 195 | { |
| 196 | $this->controlPlane->deleteBackup($id); |
| 197 | } |
| 198 | |
| 199 | // ===== Restore control plane methods ===== |
| 200 | |
| 201 | /** |
| 202 | * @param array{name: string, ...} $config |
| 203 | * @return array<string, mixed> |
| 204 | */ |
| 205 | public function createIndexFromBackup(string $backupId, array $config): array |
| 206 | { |
| 207 | return $this->controlPlane->createIndexFromBackup($backupId, $config); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @param array<string, mixed> $params |
| 212 | * @return array<int, array<string, mixed>> |
| 213 | */ |
| 214 | public function listRestoreJobs(array $params = []): array |
| 215 | { |
| 216 | return $this->controlPlane->listRestoreJobs($params); |
| 217 | } |
| 218 | |
| 219 | /** @return array<string, mixed> */ |
| 220 | public function describeRestoreJob(string $id): array |
| 221 | { |
| 222 | return $this->controlPlane->describeRestoreJob($id); |
| 223 | } |
| 224 | |
| 225 | // ===== Assistant control plane methods ===== |
| 226 | |
| 227 | /** |
| 228 | * @param array{name: string, instructions?: string, metadata?: array<string, mixed>} $config |
| 229 | * @return array<string, mixed> |
| 230 | */ |
| 231 | public function createAssistant(array $config): array |
| 232 | { |
| 233 | return $this->controlPlane->createAssistant($config); |
| 234 | } |
| 235 | |
| 236 | /** @return array<int, array<string, mixed>> */ |
| 237 | public function listAssistants(): array |
| 238 | { |
| 239 | return $this->controlPlane->listAssistants(); |
| 240 | } |
| 241 | |
| 242 | /** @return array<string, mixed> */ |
| 243 | public function describeAssistant(string $name): array |
| 244 | { |
| 245 | return $this->controlPlane->describeAssistant($name); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param array{instructions?: string, metadata?: array<string, mixed>} $config |
| 250 | * @return array<string, mixed> |
| 251 | */ |
| 252 | public function updateAssistant(string $name, array $config): array |
| 253 | { |
| 254 | return $this->controlPlane->updateAssistant($name, $config); |
| 255 | } |
| 256 | |
| 257 | public function deleteAssistant(string $name): void |
| 258 | { |
| 259 | $this->controlPlane->deleteAssistant($name); |
| 260 | } |
| 261 | } |