Skip to content

Commit

Permalink
rebuild create_index to handle serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
derpoho committed Feb 21, 2024
1 parent 9116f28 commit 959c6d9
Show file tree
Hide file tree
Showing 23 changed files with 714 additions and 757 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
}
],
"require": {
"php": "^8.1",
"saloonphp/saloon": "^2.7"
"php": "^8.2",
"saloonphp/saloon": "^3.6"
},
"autoload": {
"psr-4": {
Expand Down
811 changes: 409 additions & 402 deletions composer.lock

Large diffs are not rendered by default.

45 changes: 20 additions & 25 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Probots\Pinecone;

use Probots\Pinecone\Contracts\ClientContract;
use Probots\Pinecone\Requests\Exceptions\MissingHostException;
use Probots\Pinecone\Requests\Index\Vectors\FetchVectors;
use Probots\Pinecone\Resources\CollectionResource;
use Probots\Pinecone\Resources\IndexResource;
use Probots\Pinecone\Resources\ControlResource;
use Probots\Pinecone\Resources\DataResource;
use Psr\Http\Message\RequestInterface;
use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;
Expand All @@ -17,13 +18,11 @@ class Client extends Connector implements ClientContract

protected ?string $response = Response::class;

/**
* @param string $apiKey
* @param string $environment
*/
protected string $baseUrl = 'https://api.pinecone.io';

public function __construct(
public string $apiKey,
public string $environment,
public string $apiKey,
public ?string $indexHost = null,
)
{
// (Temporary) Workaround for https://github.com/probots-io/pinecone-php/issues/3
Expand All @@ -34,35 +33,31 @@ public function __construct(
});
}

/**
* @return string
*/
public function resolveBaseUrl(): string
{
return 'https://controller.' . $this->environment . '.pinecone.io';
return $this->baseUrl;
}

/**
* @param string|null $name
* @return IndexResource
*/
public function index(?string $name = null): IndexResource
public function control(): ControlResource
{
return new IndexResource($this, $name);
return new ControlResource($this);
}

/**
* @param string|null $name
* @return CollectionResource
* @throws MissingHostException
*/
public function collections(?string $name = null): CollectionResource
public function data(): DataResource
{
return new CollectionResource($this, $name);
$this->baseUrl = $this->indexHost;

if (!$this->indexHost) {
throw new MissingHostException('Index host is missing');
}

return new DataResource($this);
}

/**
* @return array
*/

protected function defaultHeaders(): array
{
return [
Expand Down
12 changes: 6 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ final class Factory
private ?string $apiKey = null;

/**
* The environment for the requests.
* The host for the Data requests.
*/
private ?string $environment = null;
private ?string $host = null;


/**
Expand All @@ -26,11 +26,11 @@ public function withApiKey(string $apiKey): self
}

/**
* Sets environment for the requests.
* Sets the index host for Data Operations
*/
public function withEnvironment(?string $environment): self
public function withHost(?string $host): self
{
$this->environment = $environment;
$this->host = $host;

return $this;
}
Expand All @@ -41,7 +41,7 @@ public function withEnvironment(?string $environment): self
*/
public function make(): Client
{
return new Client($this->apiKey, $this->environment);
return new Client($this->apiKey, $this->host);
}

}
4 changes: 2 additions & 2 deletions src/Pinecone.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ final class Pinecone
/**
* Creates a new Open AI Client with the given API token.
*/
public static function client(string $apiKey, string $environment = null): Client
public static function client(string $apiKey, string $indexHost = null): Client
{
return self::factory()
->withApiKey($apiKey)
->withEnvironment($environment)
->withHost($indexHost)
->make();
}

Expand Down
5 changes: 2 additions & 3 deletions src/Requests/Control/ConfigureIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Probots\Pinecone\Requests\Index;

use Saloon\Contracts\Body\HasBody;
use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

/**
Expand All @@ -23,7 +22,7 @@
* 404 | Index not found.
* 500 | Internal error. Can be caused by invalid parameters.
*/
class ConfigureIndex extends Request implements HasBody
class ConfigureIndex extends Request
{
use HasJsonBody;

Expand Down
5 changes: 2 additions & 3 deletions src/Requests/Control/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Probots\Pinecone\Requests\Collections;

use Saloon\Contracts\Body\HasBody;
use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

/**
Expand All @@ -22,7 +21,7 @@
* 409 | A collection with the name provided already exists.
* 500 | Internal error. Can be caused by invalid parameters.
*/
class CreateCollection extends Request implements HasBody
class CreateCollection extends Request
{
use HasJsonBody;

Expand Down
128 changes: 79 additions & 49 deletions src/Requests/Control/CreateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@

namespace Probots\Pinecone\Requests\Index;

use Saloon\Contracts\Body\HasBody;
use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

/**
* @link https://docs.pinecone.io/reference/create_index
*
* @param string $name
* @param int $dimension
* @param string|null $metric
* @param int|null $pods
* @param int|null $replicas
* @param string|null $pod_type
* @param array|null $metadataConfig
* @param string|null $sourceCollection
*
* @response
* string ""
*
Expand All @@ -28,73 +18,113 @@
* 409 | Index of given name already exists.
* 500 | Internal error. Can be caused by invalid parameters.
*/
class CreateIndex extends Request implements HasBody
class CreateIndex extends Request
{
use HasJsonBody;

/**
* @var Method
*/
protected Method $method = Method::POST;

/**
* @param string $name
* @param int $dimension
* @param string|null $metric
* @param int|null $pods
* @param int|null $replicas
* @param string|null $pod_type
* @param array|null $metadataConfig
* @param string|null $sourceCollection
*/
protected string $mode = 'pod';

protected string $cloud;
private string $region;
protected ?string $environment;
private ?int $replicas;
private ?string $pod_type;
private ?int $pods;
private ?int $shards;
private ?array $metadataConfig;
private ?string $sourceCollection;


public function __construct(
protected string $name,
protected int $dimension,
protected ?string $metric = 'cosine',
protected ?int $pods = 1,
protected ?int $replicas = 1,
protected ?string $pod_type = 'p1.x1',
protected ?array $metadataConfig = null,
protected ?string $sourceCollection = null,
) {}

/**
* @return string
*/
public function serverless(
?string $cloud = 'gcp',
?string $region = 'us-west-2'
): self
{

$this->mode = 'serverless';

$this->cloud = $cloud;
$this->region = $region;

return $this;
}

public function pod(
?string $environment = 'us-east1-gcp',
?int $replicas = 1,
?string $pod_type = 'p1.x1',
?int $pods = 1,
?int $shards = 1,
?array $metadataConfig = null,
?string $sourceCollection = null,
): self
{
$this->mode = 'pod';

$this->environment = $environment;
$this->replicas = $replicas;
$this->pod_type = $pod_type;
$this->pods = $pods;
$this->shards = $shards;
$this->metadataConfig = $metadataConfig;
$this->sourceCollection = $sourceCollection;

return $this;

}


public function resolveEndpoint(): string
{
return '/databases';
return '/indexes';
}

/**
* @return array
*/
protected function defaultBody(): array
{
$payload = [
'name' => $this->name,
'dimension' => $this->dimension,
'metric' => $this->metric,
'pods' => $this->pods,
'replicas' => $this->replicas,
'pod_type' => $this->pod_type,
'spec' => [],
];

if ($this->metadataConfig !== null) {
$payload['metadata_config'] = $this->metadataConfig;
if ($this->mode === 'serverless') {
$payload['spec'] = [
'cloud' => $this->cloud,
'region' => $this->region,
];
}

if ($this->sourceCollection !== null) {
$payload['source_collection'] = $this->sourceCollection;
if ($this->mode === 'pod') {
$payload['spec'] = [
'environment' => $this->environment,
'replicas' => $this->replicas,
'pod_type' => $this->pod_type,
'pods' => $this->pods,
'shards' => $this->shards,
];

if ($this->metadataConfig !== null) {
$payload['spec']['metadata_config'] = $this->metadataConfig;
}

if ($this->sourceCollection !== null) {
$payload['spec']['source_collection'] = $this->sourceCollection;
}
}

return $payload;
}

/**
* @param Response $response
* @return bool|null
*/
public function hasRequestFailed(Response $response): ?bool
public function hasRequestFailed(Response|\Saloon\Contracts\Response $response): ?bool
{
return $response->status() !== 201;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Control/DeleteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Probots\Pinecone\Requests\Collections;

use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

/**
* @link https://docs.pinecone.io/reference/delete_collection
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Control/DeleteIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Probots\Pinecone\Requests\Index;

use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

/**
* @link https://docs.pinecone.io/reference/delete_index
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Control/DescribeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Probots\Pinecone\Requests\Collections;

use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

/**
* @link https://docs.pinecone.io/reference/describe_collection
Expand Down
Loading

0 comments on commit 959c6d9

Please sign in to comment.