phpcurlpinecone

Accessing Pinecone via PHP


I am having trouble accessing Pinecone via PHP. I tried to follow the examples from the documentation, but it does not seem to work.

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require 'vendor/autoload.php';
require 'config.php';
require 'embeddings.php';

use \Probots\Pinecone\Client as Pinecone;

$pineconeApiKey = PINECONE_API_KEY;
$pineconeIndex = 'my-index';

// Initialize clients
$pinecone = new Pinecone($pineconeApiKey);

$respons = $pinecone->index()->list();
if($response->successful()) {
    var_dump($response);
}

I am getting:

_Fatal error: Uncaught ArgumentCountError: Too few arguments to function Probots\Pinecone\Client::_construct(), 1 passed in /home/.../pinecone.php on line 16 and exactly 2 expected in /home/.../probots-io/pinecone-php/src/Client.php:24


Then, I tried to pass the environment, because I saw that in the code: new Pinecone($key, "my-index"); and I had to change to remove control() from the call ($response = $pinecone->control()->index()->list()).

And now I am getting:

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: controller.my-index.pinecone.io (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://controller.my-index.pinecone.io/databases

How can I adjust the code so that it will work?


Solution

  • Finally I ended by writing my own PHP wrapper for Pinecone. It is pretty raw, but very intuitive to use by knowing the docs.

    <?php
      require "pinecone.php";
    
      $pinecone = new Pinecone(PINECONE_API_KEY, PINECONE_HOST);
      $indexes = $pinecone->getRequest("https://api.pinecone.io/indexes");
    
      header("Content-Type: application/json");
    
      // List the vector IDs
      $vectorIds = $pinecone->getRequest("/vectors/list", [
        "namespace" => "example-namespace"
      ]);
      echo json_encode($vectorIds);
    
      // Upsert vector
      $text = "Hello World";
    
      // Define the function to compute the embedding (via OpenAI etc)
      $embedding = getEmbedding($text);
      $vector = [
          'id' => md5($text),
          'values' => $embedding,
          'metadata' => [
              'title' => "Example"
          ]
      ];
    
      // Upsert the vector
      $res = $pinecone->postRequest("/vectors/upsert", [
          "vectors" => [$vector]
      ]);