-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from outl1ne/Virtual-branch
feat: Add VectorStores capabilities with list, delete, and response classes.
- Loading branch information
Showing
15 changed files
with
412 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Capabilities/VectorStores/Responses/VectorStoreDeleteResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Capabilities/VectorStores/Responses/VectorStoreListResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/Capabilities/VectorStores/Responses/VectorStoreResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.