Skip to content

Commit

Permalink
Merge pull request #65 from outl1ne/Virtual-branch
Browse files Browse the repository at this point in the history
feat: Add VectorStores capabilities with list, delete, and response classes.
  • Loading branch information
allantatter authored Oct 11, 2024
2 parents 2f5b689 + 2c0377b commit 8f28b9d
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/Capabilities/VectorStores/CreateVectorStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Exception;
use Outl1ne\NovaOpenAI\Capabilities\CapabilityClient;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\ResponseFormat;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses\VectorStoreResponse;

class CreateVectorStore extends CapabilityClient
{
protected string $method = 'vector_stores';

public function makeRequest(
?array $fileIds = null,
?string $name = null,
?array $expiresAfter = null,
?array $chunkingStrategy = null,
?array $metadata = null,
): VectorStoreResponse {
$this->request->appendArgument('file_ids', $fileIds);
$this->request->appendArgument('name', $name);
$this->request->appendArgument('expires_after', $expiresAfter);
$this->request->appendArgument('chunking_strategy', $chunkingStrategy);
$this->request->appendArgument('metadata', $metadata);

$this->pending();

try {
$response = $this->openAI->http()->post("vector_stores", [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
...$this->request->arguments,
]),
]);

return $this->handleResponse(new VectorStoreResponse($response));
} catch (Exception $e) {
$this->handleException($e);
}
}
}
33 changes: 33 additions & 0 deletions src/Capabilities/VectorStores/DeleteVectorStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Exception;
use Outl1ne\NovaOpenAI\Capabilities\CapabilityClient;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses\VectorStoreDeleteResponse;

class DeleteVectorStore extends CapabilityClient
{
protected string $method = 'vector_stores';

public function makeRequest(
string $vectorStoreId,
): VectorStoreDeleteResponse {
$this->pending();

try {
$response = $this->openAI->http()->delete("vector_stores/{$vectorStoreId}", [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
...$this->request->arguments,
]),
]);

return $this->handleResponse(new VectorStoreDeleteResponse($response));
} catch (Exception $e) {
$this->handleException($e);
}
}
}
38 changes: 38 additions & 0 deletions src/Capabilities/VectorStores/ListVectorStores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Exception;
use Outl1ne\NovaOpenAI\Capabilities\CapabilityClient;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses\VectorStoreListResponse;

class ListVectorStores extends CapabilityClient
{
protected string $method = 'vector_stores';

public function makeRequest(
?int $limit = null,
?string $order = null,
?string $after = null,
?string $before = null,
): VectorStoreListResponse {
$this->request->appendArgument('limit', $limit);
$this->request->appendArgument('order', $order);
$this->request->appendArgument('after', $after);
$this->request->appendArgument('before', $before);

$this->pending();

try {
$response = $this->openAI->http()->get("vector_stores", [
'query' => [
...$this->request->arguments,
],
]);

return $this->handleResponse(new VectorStoreListResponse($response));
} catch (Exception $e) {
$this->handleException($e);
}
}
}
41 changes: 41 additions & 0 deletions src/Capabilities/VectorStores/ModifyVectorStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Exception;
use Outl1ne\NovaOpenAI\Capabilities\CapabilityClient;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\ResponseFormat;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses\VectorStoreResponse;

class ModifyVectorStore extends CapabilityClient
{
protected string $method = 'vector_stores';

public function makeRequest(
string $vectorStoreId,
?string $name = null,
?array $expiresAfter = null,
?array $metadata = null,
): VectorStoreResponse {
$this->request->appendArgument('name', $name);
$this->request->appendArgument('expires_after', $expiresAfter);
$this->request->appendArgument('metadata', $metadata);

$this->pending();

try {
$response = $this->openAI->http()->post("vector_stores/{$vectorStoreId}", [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
...$this->request->arguments,
]),
]);

return $this->handleResponse(new VectorStoreResponse($response));
} catch (Exception $e) {
$this->handleException($e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses;

use Outl1ne\NovaOpenAI\Capabilities\Responses\Response;

class VectorStoreDeleteResponse extends Response
{
public string $id;

public function __construct(...$arguments)
{
parent::__construct(...$arguments);

$this->id = $this->data['id'];
$this->appendMeta('id', $this->data['id']);
$this->appendMeta('object', $this->data['object']);
$this->appendMeta('deleted', $this->data['deleted']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses;

use Outl1ne\NovaOpenAI\Capabilities\Responses\Response;

class VectorStoreListResponse extends Response
{
public array $vectorStores;
public bool $hasMore;

public function __construct(...$arguments)
{
parent::__construct(...$arguments);

$this->vectorStores = $this->data['data'];
$this->hasMore = $this->data['has_more'];
$this->appendMeta('object', $this->data['object']);
$this->appendMeta('first_id', $this->data['first_id']);
$this->appendMeta('last_id', $this->data['last_id']);
$this->appendMeta('has_more', $this->data['has_more']);
}
}
28 changes: 28 additions & 0 deletions src/Capabilities/VectorStores/Responses/VectorStoreResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses;

use Outl1ne\NovaOpenAI\Capabilities\Responses\Response;

class VectorStoreResponse extends Response
{
public string $id;

public function __construct(...$arguments)
{
parent::__construct(...$arguments);

$this->id = $this->data['id'];
$this->appendMeta('id', $this->data['id']);
$this->appendMeta('object', $this->data['object']);
$this->appendMeta('name', $this->data['name']);
$this->appendMeta('status', $this->data['status']);
$this->appendMeta('usage_bytes', $this->data['usage_bytes']);
$this->appendMeta('created_at', $this->data['created_at']);
$this->appendMeta('file_counts', $this->data['file_counts']);
$this->appendMeta('metadata', $this->data['metadata']);
$this->appendMeta('expires_after', $this->data['expires_after']);
$this->appendMeta('expires_at', $this->data['expires_at']);
$this->appendMeta('last_active_at', $this->data['last_active_at']);
}
}
34 changes: 34 additions & 0 deletions src/Capabilities/VectorStores/RetrieveVectorStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Exception;
use Outl1ne\NovaOpenAI\Capabilities\CapabilityClient;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\ResponseFormat;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\Responses\VectorStoreResponse;

class RetrieveVectorStore extends CapabilityClient
{
protected string $method = 'vector_stores';

public function makeRequest(
string $vectorStoreId,
): VectorStoreResponse {
$this->pending();

try {
$response = $this->openAI->http()->post("vector_stores/{$vectorStoreId}", [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
...$this->request->arguments,
]),
]);

return $this->handleResponse(new VectorStoreResponse($response));
} catch (Exception $e) {
$this->handleException($e);
}
}
}
68 changes: 68 additions & 0 deletions src/Capabilities/VectorStores/VectorStores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\VectorStores;

use Outl1ne\NovaOpenAI\Capabilities\Capability;

class VectorStores extends Capability
{
public function create(
?array $fileIds = null,
?string $name = null,
?array $expiresAfter = null,
?array $chunkingStrategy = null,
?array $metadata = null,
) {
return (new CreateVectorStore($this))->makeRequest(
$fileIds,
$name,
$expiresAfter,
$chunkingStrategy,
$metadata,
);
}

public function list(
?int $limit = null,
?string $order = null,
?string $after = null,
?string $before = null,
) {
return (new ListVectorStores($this))->makeRequest(
$limit,
$order,
$after,
$before,
);
}

public function retrieve(
string $vectorStoreId,
) {
return (new RetrieveVectorStore($this))->makeRequest(
$vectorStoreId,
);
}

public function modify(
string $vectorStoreId,
?string $name = null,
?array $expiresAfter = null,
?array $metadata = null,
) {
return (new ModifyVectorStore($this))->makeRequest(
$vectorStoreId,
$name,
$expiresAfter,
$metadata,
);
}

public function delete(
string $vectorStoreId,
) {
return (new DeleteVectorStore($this))->makeRequest(
$vectorStoreId,
);
}
}
15 changes: 15 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Outl1ne\NovaOpenAI;

class Helpers
{
static public function json(string $response): ?object
{
$response = trim($response);
$response = preg_replace('/^```json/', '', $response);
$response = preg_replace('/```$/', '', $response);
$response = trim($response);
return json_decode($response);
}
}
12 changes: 11 additions & 1 deletion src/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Outl1ne\NovaOpenAI\Capabilities\Threads\Threads;
use Outl1ne\NovaOpenAI\Capabilities\Assistants\Assistants;
use Outl1ne\NovaOpenAI\Capabilities\Embeddings\Embeddings;
use Outl1ne\NovaOpenAI\Capabilities\VectorStores\VectorStores;

class OpenAI
{
Expand Down Expand Up @@ -49,12 +50,21 @@ public function threads(): Threads
return new Threads($this);
}


public function files(): Files
{
return new Files($this);
}

public function vectorStores(): VectorStores
{
return new VectorStores($this);
}

public function json(string $response): ?object
{
return Helpers::json($response);
}

public function http(): Client
{
return new Client([
Expand Down
Loading

0 comments on commit 8f28b9d

Please sign in to comment.