simple-pinecone-php 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +262 -0
- package/lib/pinecone.php +102 -0
- package/package.json +103 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
<!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. -->
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# simple-pinecone-php
|
|
21
|
+
|
|
22
|
+
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [](https://www.npmjs.com/package/simple-pinecone-php) [](https://www.npmjs.com/package/simple-pinecone-php) [](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github)
|
|
23
|
+
|
|
24
|
+
<a href="https://www.buymeacoffee.com/H96WwChMy" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee"></a>
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
> Access the Pinecone APIs thorugh PHP.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## :cloud: Installation
|
|
51
|
+
|
|
52
|
+
```shell
|
|
53
|
+
composer require IonicaBizau/simple-pinecone-php
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
If you do not use composer, you can simply download the file `lib/pinecone.php` and include it in your project:
|
|
57
|
+
```
|
|
58
|
+
<?php
|
|
59
|
+
require "pinecone.php";
|
|
60
|
+
$pinecone = new Pinecone("your api key", "https://your-host-url");
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
## Example
|
|
80
|
+
|
|
81
|
+
```php
|
|
82
|
+
<?php
|
|
83
|
+
require "pinecone.php";
|
|
84
|
+
|
|
85
|
+
$pinecone = new Pinecone(PINECONE_API_KEY, PINECONE_HOST);
|
|
86
|
+
$indexes = $pinecone->getRequest("https://api.pinecone.io/indexes");
|
|
87
|
+
|
|
88
|
+
header("Content-Type: application/json");
|
|
89
|
+
|
|
90
|
+
// List the vector IDs
|
|
91
|
+
$vectorIds = $pinecone->getRequest("/vectors/list", [
|
|
92
|
+
"namespace" => "example-namespace"
|
|
93
|
+
]);
|
|
94
|
+
echo json_encode($vectorIds);
|
|
95
|
+
|
|
96
|
+
// Upsert vector
|
|
97
|
+
$text = "Hello World";
|
|
98
|
+
|
|
99
|
+
// Define the function to compute the embedding (via OpenAI etc)
|
|
100
|
+
$embedding = getEmbedding($text);
|
|
101
|
+
$vector = [
|
|
102
|
+
'id' => md5($text),
|
|
103
|
+
'values' => $embedding,
|
|
104
|
+
'metadata' => [
|
|
105
|
+
'title' => "Example"
|
|
106
|
+
]
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
// Upsert the vector
|
|
110
|
+
$res = $pinecone->postRequest("/vectors/upsert", [
|
|
111
|
+
"vectors" => [$vector]
|
|
112
|
+
]);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## :memo: Documentation
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
### `Pinecone($apiKey, $hostUrl, $apiVersion)`
|
|
125
|
+
Constructor to initialize the Pinecone API client.
|
|
126
|
+
|
|
127
|
+
#### Params
|
|
128
|
+
|
|
129
|
+
- **String** `$apiKey`: The API key for authentication.
|
|
130
|
+
- **String** `$hostUrl`: The base URL of the Pinecone API.
|
|
131
|
+
- **String** `$apiVersion`: The API version, default is `'2025-01'`.
|
|
132
|
+
|
|
133
|
+
### `pinecone.request($method, $pathname, $data)`
|
|
134
|
+
Makes an HTTP request to the Pinecone API.
|
|
135
|
+
|
|
136
|
+
#### Params
|
|
137
|
+
|
|
138
|
+
- **String** `$method`: HTTP method (GET or POST).
|
|
139
|
+
- **String** `$pathname`: The API endpoint path.
|
|
140
|
+
- **Array** `$data`: The data to send with the request.
|
|
141
|
+
|
|
142
|
+
#### Return
|
|
143
|
+
- **Array** The API response as an associative array.
|
|
144
|
+
|
|
145
|
+
### `pinecone.getRequest($pathname, $queryStringParamsAsArray)`
|
|
146
|
+
Sends a GET request to the Pinecone API.
|
|
147
|
+
|
|
148
|
+
#### Params
|
|
149
|
+
|
|
150
|
+
- **String** `$pathname`: The API endpoint path.
|
|
151
|
+
- **Array** `$queryStringParamsAsArray`: Query parameters as an associative array.
|
|
152
|
+
|
|
153
|
+
#### Return
|
|
154
|
+
- **Array** The API response as an associative array.
|
|
155
|
+
|
|
156
|
+
### `pinecone.postRequest($pathname, $bodyAsArray)`
|
|
157
|
+
Sends a POST request to the Pinecone API.
|
|
158
|
+
|
|
159
|
+
#### Params
|
|
160
|
+
|
|
161
|
+
- **String** `$pathname`: The API endpoint path.
|
|
162
|
+
- **Array** `$bodyAsArray`: The request body as an associative array.
|
|
163
|
+
|
|
164
|
+
#### Return
|
|
165
|
+
- **Array** The API response as an associative array.
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
## :question: Get Help
|
|
176
|
+
|
|
177
|
+
There are few ways to get help:
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
182
|
+
2. For bug reports and feature requests, open issues. :bug:
|
|
183
|
+
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
## :yum: How to contribute
|
|
199
|
+
Have an idea? Found a bug? See [how to contribute][contributing].
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
## :sparkling_heart: Support my projects
|
|
203
|
+
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously,
|
|
204
|
+
this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it).
|
|
205
|
+
|
|
206
|
+
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
- Starring and sharing the projects you like :rocket:
|
|
210
|
+
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book:
|
|
211
|
+
- [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
|
|
212
|
+
- [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
|
|
213
|
+
- **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6`
|
|
214
|
+
|
|
215
|
+

|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
Thanks! :heart:
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
## :scroll: License
|
|
244
|
+
|
|
245
|
+
[MIT][license] © [Ionică Bizău][website]
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
[license]: /LICENSE
|
|
253
|
+
[website]: https://ionicabizau.net
|
|
254
|
+
[contributing]: /CONTRIBUTING.md
|
|
255
|
+
[docs]: /DOCUMENTATION.md
|
|
256
|
+
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg
|
|
257
|
+
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg
|
|
258
|
+
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
|
|
259
|
+
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
|
|
260
|
+
[patreon]: https://www.patreon.com/ionicabizau
|
|
261
|
+
[amazon]: http://amzn.eu/hRo9sIZ
|
|
262
|
+
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
|
package/lib/pinecone.php
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace IonicaBizau\SimplePineconePHP;
|
|
4
|
+
|
|
5
|
+
class Pinecone {
|
|
6
|
+
private $apiKey;
|
|
7
|
+
private $hostUrl;
|
|
8
|
+
private $apiVersion;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Pinecone
|
|
12
|
+
* Constructor to initialize the Pinecone API client.
|
|
13
|
+
*
|
|
14
|
+
* @function
|
|
15
|
+
* @name Pinecone
|
|
16
|
+
* @param {String} $apiKey The API key for authentication.
|
|
17
|
+
* @param {String} $hostUrl The base URL of the Pinecone API.
|
|
18
|
+
* @param {String} $apiVersion The API version, default is `'2025-01'`.
|
|
19
|
+
*/
|
|
20
|
+
public function __construct($apiKey, $hostUrl, $apiVersion = '2025-01') {
|
|
21
|
+
$this->apiKey = $apiKey;
|
|
22
|
+
$this->hostUrl = rtrim($hostUrl, '/');
|
|
23
|
+
$this->apiVersion = $apiVersion;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* pinecone.request
|
|
28
|
+
* Makes an HTTP request to the Pinecone API.
|
|
29
|
+
*
|
|
30
|
+
* @function
|
|
31
|
+
* @name pinecone.request
|
|
32
|
+
* @param {String} $method HTTP method (GET or POST).
|
|
33
|
+
* @param {String} $pathname The API endpoint path.
|
|
34
|
+
* @param {Array} $data The data to send with the request.
|
|
35
|
+
* @return {Array} The API response as an associative array.
|
|
36
|
+
*/
|
|
37
|
+
private function request($method, $pathname, $data = []) {
|
|
38
|
+
$url = strpos($pathname, 'https://') === 0 ? $pathname : $this->hostUrl . '/' . ltrim($pathname, '/');
|
|
39
|
+
|
|
40
|
+
if ($method === 'GET' && !empty($data)) {
|
|
41
|
+
$url .= '?' . http_build_query($data);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
$ch = curl_init($url);
|
|
45
|
+
|
|
46
|
+
$headers = [
|
|
47
|
+
"Content-Type: application/json",
|
|
48
|
+
"Api-Key: {$this->apiKey}",
|
|
49
|
+
"X-Pinecone-API-Version: {$this->apiVersion}"
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
53
|
+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
54
|
+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
55
|
+
|
|
56
|
+
if ($method === 'POST') {
|
|
57
|
+
curl_setopt($ch, CURLOPT_POST, true);
|
|
58
|
+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
$response = curl_exec($ch);
|
|
62
|
+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
63
|
+
$error = curl_error($ch);
|
|
64
|
+
|
|
65
|
+
curl_close($ch);
|
|
66
|
+
|
|
67
|
+
if ($error) {
|
|
68
|
+
return ['error' => $error];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
$response = json_decode($response, true);
|
|
72
|
+
return $response;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* pinecone.getRequest
|
|
77
|
+
* Sends a GET request to the Pinecone API.
|
|
78
|
+
*
|
|
79
|
+
* @name pinecone.getRequest
|
|
80
|
+
* @function
|
|
81
|
+
* @param {String} $pathname The API endpoint path.
|
|
82
|
+
* @param {Array} $queryStringParamsAsArray Query parameters as an associative array.
|
|
83
|
+
* @return {Array} The API response as an associative array.
|
|
84
|
+
*/
|
|
85
|
+
public function getRequest($pathname, $queryStringParamsAsArray = []) {
|
|
86
|
+
return $this->request('GET', $pathname, $queryStringParamsAsArray);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* pinecone.postRequest
|
|
91
|
+
* Sends a POST request to the Pinecone API.
|
|
92
|
+
*
|
|
93
|
+
* @name pinecone.postRequest
|
|
94
|
+
* @function
|
|
95
|
+
* @param {String} $pathname The API endpoint path.
|
|
96
|
+
* @param {Array} $bodyAsArray The request body as an associative array.
|
|
97
|
+
* @return {Array} The API response as an associative array.
|
|
98
|
+
*/
|
|
99
|
+
public function postRequest($pathname, $bodyAsArray = []) {
|
|
100
|
+
return $this->request('POST', $pathname, $bodyAsArray);
|
|
101
|
+
}
|
|
102
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-pinecone-php",
|
|
3
|
+
"description": "Access the Pinecone APIs thorugh PHP.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"simple",
|
|
6
|
+
"pinecone",
|
|
7
|
+
"php",
|
|
8
|
+
"access",
|
|
9
|
+
"apis",
|
|
10
|
+
"thorugh"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"version": "1.0.0",
|
|
14
|
+
"main": "lib/pinecone.php",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
17
|
+
},
|
|
18
|
+
"author": "Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)",
|
|
19
|
+
"homepage": "https://github.com/IonicaBizau/simple-pinecone-php#readme",
|
|
20
|
+
"files": [
|
|
21
|
+
"bin/",
|
|
22
|
+
"app/",
|
|
23
|
+
"lib/",
|
|
24
|
+
"dist/",
|
|
25
|
+
"src/",
|
|
26
|
+
"scripts/",
|
|
27
|
+
"resources/",
|
|
28
|
+
"menu/",
|
|
29
|
+
"cli.js",
|
|
30
|
+
"index.js",
|
|
31
|
+
"index.d.ts",
|
|
32
|
+
"package-lock.json",
|
|
33
|
+
"bloggify.js",
|
|
34
|
+
"bloggify.json",
|
|
35
|
+
"bloggify/"
|
|
36
|
+
],
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+ssh://git@github.com/IonicaBizau/simple-pinecone-php.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/IonicaBizau/simple-pinecone-php/issues"
|
|
43
|
+
},
|
|
44
|
+
"blah": {
|
|
45
|
+
"installation_command": [
|
|
46
|
+
{
|
|
47
|
+
"content": "composer require IonicaBizau/simple-pinecone-php",
|
|
48
|
+
"language": "shell"
|
|
49
|
+
},
|
|
50
|
+
"If you do not use composer, you can simply download the file `lib/pinecone.php` and include it in your project:",
|
|
51
|
+
{
|
|
52
|
+
"content": [
|
|
53
|
+
"<?php",
|
|
54
|
+
" require \"pinecone.php\";",
|
|
55
|
+
" $pinecone = new Pinecone(\"your api key\", \"https://your-host-url\");"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
"example": [
|
|
60
|
+
{
|
|
61
|
+
"h2": "Example"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"code": {
|
|
65
|
+
"content": [
|
|
66
|
+
"<?php",
|
|
67
|
+
" require \"pinecone.php\";",
|
|
68
|
+
"",
|
|
69
|
+
" $pinecone = new Pinecone(PINECONE_API_KEY, PINECONE_HOST);",
|
|
70
|
+
" $indexes = $pinecone->getRequest(\"https://api.pinecone.io/indexes\");",
|
|
71
|
+
"",
|
|
72
|
+
" header(\"Content-Type: application/json\");",
|
|
73
|
+
"",
|
|
74
|
+
" // List the vector IDs",
|
|
75
|
+
" $vectorIds = $pinecone->getRequest(\"/vectors/list\", [",
|
|
76
|
+
" \"namespace\" => \"example-namespace\"",
|
|
77
|
+
" ]);",
|
|
78
|
+
" echo json_encode($vectorIds);",
|
|
79
|
+
"",
|
|
80
|
+
" // Upsert vector",
|
|
81
|
+
" $text = \"Hello World\";",
|
|
82
|
+
"",
|
|
83
|
+
" // Define the function to compute the embedding (via OpenAI etc)",
|
|
84
|
+
" $embedding = getEmbedding($text);",
|
|
85
|
+
" $vector = [",
|
|
86
|
+
" 'id' => md5($text),",
|
|
87
|
+
" 'values' => $embedding,",
|
|
88
|
+
" 'metadata' => [",
|
|
89
|
+
" 'title' => \"Example\"",
|
|
90
|
+
" ]",
|
|
91
|
+
" ];",
|
|
92
|
+
"",
|
|
93
|
+
" // Upsert the vector",
|
|
94
|
+
" $res = $pinecone->postRequest(\"/vectors/upsert\", [",
|
|
95
|
+
" \"vectors\" => [$vector]",
|
|
96
|
+
" ]);"
|
|
97
|
+
],
|
|
98
|
+
"language": "php"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
}
|