Skip to content

Commit edf6990

Browse files
committed
Pinecone fixes
* should fix #12 * correctly handle JSON decoding errors * fix empty filter requests * match renamed API fields
1 parent ecfed2a commit edf6990

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

Storage/PineconeStorage.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ protected function runQuery($endpoint, mixed $data, $method = 'POST')
5757
throw new \Exception('Pinecone API returned no response. ' . $this->http->error);
5858
}
5959

60-
$result = json_decode((string) $response, true, 512, JSON_THROW_ON_ERROR);
61-
if ($result === null) {
62-
throw new \Exception('Pinecone API returned invalid JSON. ' . $response);
60+
try {
61+
$result = json_decode((string)$response, true, 512, JSON_THROW_ON_ERROR);
62+
} catch (\JsonException $e) {
63+
throw new \Exception('Pinecone API returned invalid JSON. ' . $response, 0, $e);
6364
}
6465

6566
if (isset($result['message'])) {
66-
throw new \Exception('Pinecone API returned error. ' . $result['message']);
67+
throw new \Exception('Pinecone API returned error. ' . $result['message'], $result['code'] ?? 0);
6768
}
6869

6970
return $result;
@@ -123,7 +124,12 @@ public function deletePageChunks($page, $firstChunkID)
123124
// delete all possible chunk IDs
124125
$ids = range($firstChunkID, $firstChunkID + 99, 1);
125126
$ids = array_map(static fn($id) => (string)$id, $ids);
126-
$this->runQuery('/vectors/delete', ['ids' => $ids]);
127+
try {
128+
$this->runQuery('/vectors/delete', ['ids' => $ids]);
129+
} catch (\Exception $e) {
130+
// 5 is the code for "namespace not found" See #12
131+
if($e->getCode() !== 5) throw $e;
132+
}
127133
}
128134

129135
/** @inheritdoc */
@@ -195,22 +201,18 @@ public function getSimilarChunks($vector, $lang = '', $limit = 4)
195201
{
196202
$limit *= 2; // we can't check ACLs, so we return more than requested
197203

204+
$query = [
205+
'vector' => $vector,
206+
'topK' => (int)$limit,
207+
'includeMetadata' => true,
208+
'includeValues' => true,
209+
];
210+
198211
if ($lang) {
199-
$filter = ['language' => ['$eq', $lang]];
200-
} else {
201-
$filter = [];
212+
$query['filter'] = ['language' => ['$eq', $lang]];
202213
}
203214

204-
$response = $this->runQuery(
205-
'/query',
206-
[
207-
'vector' => $vector,
208-
'topK' => (int)$limit,
209-
'include_metadata' => true,
210-
'include_values' => true,
211-
'filter' => $filter,
212-
]
213-
);
215+
$response = $this->runQuery('/query', $query);
214216
$chunks = [];
215217
foreach ($response['matches'] as $vector) {
216218
$chunks[] = new Chunk(

0 commit comments

Comments
 (0)