3

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?

3
  • Which version are you using? It says there were breaking changes with the 1.0.0 release, so I'm guessing the documentation doesn't reflect that yet. Commented Mar 27 at 8:52
  • "I tried to pass the environment" - in the code, this second parameter is named $indexHost - is that what you are referring to, when you say "environment"? What actual value did you use for my-index(I don't assume this was meant literal)? cURL apparently can't resolve the host name, so I'm guessing that your my-index is either wrong, or needs to be registered with Pinecone first or something ...? Commented Mar 27 at 8:56
  • I can't reach controller.my-index.pinecone.io either. Are you sure this is a programming-related problem? Did you try to run this with cURL on the command line with any other result? Commented Mar 27 at 10:13

2 Answers 2

1

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]
  ]);
Sign up to request clarification or add additional context in comments.

Comments

0

The constructor is

    public function __construct(
        public string  $apiKey,
        public ?string $indexHost = null,
    ) {}

as per the permalink.

If you take a look at Git Blame, then you can see that this commit has changed the constructor from

    public function __construct(


        public string $apiKey,


        public string $environment,

    )

    {

        // (Temporary) Workaround for https://github.com/probots-io/pinecone-php/issues/3

        $this->sender()->addMiddleware(function (callable $handler) {

            return function (RequestInterface $request, array $options) use ($handler) {

                return $handler(FetchVectors::queryIdsWorkaround($request), $options);

            };

        });

    }

to

    public function __construct(


        public string  $apiKey,


        public ?string $indexHost = null,

    )

    {

        // (Temporary) Workaround for https://github.com/probots-io/pinecone-php/issues/3

        $this->sender()->addMiddleware(function (callable $handler) {

            return function (RequestInterface $request, array $options) use ($handler) {

                return $handler(FetchVectors::queryIdsWorkaround($request), $options);

            };

        });

    }

so it is clear that the commit we are discussing changed the second parameter from being a mandatory string for the environment into an optional String.

So you are using an older version than this commit and that obliges you to pass a second parameter.

There are two possible solutions: either you pass a string as a second parameter as per the version you currently use, or update the version, that is, pulling and checking out an appropriate branch, such as master.

2 Comments

Thanks, I see that, but what should I change in my code so that it would work?
@IonicăBizău the last paragraph of this answer describes two options: the first is to pass a string as the second parameter, like $pinecone = new Pinecone($pineconeApiKey, $yoursecondparameter); and the second is to refresh PineCone's version in which case you do not need to change the way you instantiate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.