Skip to content

Commit f32feb7

Browse files
authored
Merge pull request #405 from openai-php/add-assistants-v2-api
Implement vector store endpoints and add assistants API v2 support
2 parents a4e519a + 628918f commit f32feb7

File tree

160 files changed

+3922
-1456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+3922
-1456
lines changed

README.md

Lines changed: 90 additions & 141 deletions
Large diffs are not rendered by default.

src/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use OpenAI\Contracts\ClientContract;
88
use OpenAI\Contracts\Resources\ThreadsContract;
9+
use OpenAI\Contracts\Resources\VectorStoresContract;
910
use OpenAI\Contracts\TransporterContract;
1011
use OpenAI\Resources\Assistants;
1112
use OpenAI\Resources\Audio;
@@ -21,6 +22,7 @@
2122
use OpenAI\Resources\Models;
2223
use OpenAI\Resources\Moderations;
2324
use OpenAI\Resources\Threads;
25+
use OpenAI\Resources\VectorStores;
2426

2527
final class Client implements ClientContract
2628
{
@@ -174,4 +176,14 @@ public function batches(): Batches
174176
{
175177
return new Batches($this->transporter);
176178
}
179+
180+
/**
181+
* Create and update vector stores that assistants can interact with
182+
*
183+
* @see https://platform.openai.com/docs/api-reference/vector-stores
184+
*/
185+
public function vectorStores(): VectorStoresContract
186+
{
187+
return new VectorStores($this->transporter);
188+
}
177189
}

src/Contracts/ClientContract.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OpenAI\Contracts\Resources\ModelsContract;
1717
use OpenAI\Contracts\Resources\ModerationsContract;
1818
use OpenAI\Contracts\Resources\ThreadsContract;
19+
use OpenAI\Contracts\Resources\VectorStoresContract;
1920

2021
interface ClientContract
2122
{
@@ -121,4 +122,11 @@ public function threads(): ThreadsContract;
121122
* @see https://platform.openai.com/docs/api-reference/batch
122123
*/
123124
public function batches(): BatchesContract;
125+
126+
/**
127+
* Create and update vector stores that assistants can interact with
128+
*
129+
* @see https://platform.openai.com/docs/api-reference/vector-stores
130+
*/
131+
public function vectorStores(): VectorStoresContract;
124132
}

src/Contracts/Resources/AssistantsContract.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface AssistantsContract
1111
/**
1212
* Create an assistant with a model and instructions.
1313
*
14-
* @see https://platform.openai.com/docs/api-reference/assistants/object
14+
* @see https://platform.openai.com/docs/api-reference/assistants/createAssistant
1515
*
1616
* @param array<string, mixed> $parameters
1717
*/
@@ -48,11 +48,4 @@ public function delete(string $id): AssistantDeleteResponse;
4848
* @param array<string, mixed> $parameters
4949
*/
5050
public function list(array $parameters = []): AssistantListResponse;
51-
52-
/**
53-
* Manage files attached to an assistant.
54-
*
55-
* @see https://platform.openai.com/docs/api-reference/assistants
56-
*/
57-
public function files(): AssistantsFilesContract;
5851
}

src/Contracts/Resources/AssistantsFilesContract.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Contracts/Resources/ThreadsMessagesContract.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,4 @@ public function modify(string $threadId, string $messageId, array $parameters):
4040
* @param array<string, mixed> $parameters
4141
*/
4242
public function list(string $threadId, array $parameters = []): ThreadMessageListResponse;
43-
44-
/**
45-
* Manage files attached to a thread message.
46-
*
47-
* @see https://platform.openai.com/docs/api-reference/messages/file-object
48-
*/
49-
public function files(): ThreadsMessagesFilesContract;
5043
}

src/Contracts/Resources/ThreadsMessagesFilesContract.php

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace OpenAI\Contracts\Resources;
4+
5+
use OpenAI\Responses\VectorStores\VectorStoreDeleteResponse;
6+
use OpenAI\Responses\VectorStores\VectorStoreListResponse;
7+
use OpenAI\Responses\VectorStores\VectorStoreResponse;
8+
9+
interface VectorStoresContract
10+
{
11+
/**
12+
* Create a vector store
13+
*
14+
* @see https://platform.openai.com/docs/api-reference/vector-stores/create
15+
*
16+
* @param array<string, mixed> $parameters
17+
*/
18+
public function create(array $parameters): VectorStoreResponse;
19+
20+
/**
21+
* Returns a list of vector stores.
22+
*
23+
* @see https://platform.openai.com/docs/api-reference/vector-stores/list
24+
*/
25+
public function list(): VectorStoreListResponse;
26+
27+
/**
28+
* Retrieves a vector store.
29+
*
30+
* @see https://platform.openai.com/docs/api-reference/vector-stores/retrieve
31+
*/
32+
public function retrieve(string $vectorStore): VectorStoreResponse;
33+
34+
/**
35+
* Modify a vector store
36+
*
37+
* @see https://platform.openai.com/docs/api-reference/vector-stores/modify
38+
*
39+
* @param array<string, mixed> $parameters
40+
*/
41+
public function modify(string $vectorStore, array $parameters): VectorStoreResponse;
42+
43+
/**
44+
* Delete a vector store.
45+
*
46+
* https://platform.openai.com/docs/api-reference/vector-stores/delete
47+
*/
48+
public function delete(string $vectorStore): VectorStoreDeleteResponse;
49+
50+
/**
51+
* Manage the files related to the vector store
52+
*
53+
* @see https://platform.openai.com/docs/api-reference/vector-stores-files
54+
*/
55+
public function files(): VectorStoresFilesContract;
56+
57+
/**
58+
* Manage the file batches related to the vector store
59+
*
60+
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches
61+
*/
62+
public function batches(): VectorStoresFileBatchesContract;
63+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace OpenAI\Contracts\Resources;
4+
5+
use OpenAI\Responses\VectorStores\FileBatches\VectorStoreFileBatchResponse;
6+
use OpenAI\Responses\VectorStores\Files\VectorStoreFileListResponse;
7+
8+
interface VectorStoresFileBatchesContract
9+
{
10+
/**
11+
* Create a file batch on a vector store
12+
*
13+
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/createBatch
14+
*
15+
* @param array<string, mixed> $parameters
16+
*/
17+
public function create(string $vectorStoreId, array $parameters): VectorStoreFileBatchResponse;
18+
19+
/**
20+
* Retrieves a file batch within a vector store.
21+
*
22+
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/getBatch
23+
*/
24+
public function retrieve(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse;
25+
26+
/**
27+
* Cancel a vector store file batch
28+
*
29+
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/cancelBatch
30+
*/
31+
public function cancel(string $vectorStoreId, string $fileBatchId): VectorStoreFileBatchResponse;
32+
33+
/**
34+
* Lists the files within a file batch within a vector store
35+
*
36+
* @see https://platform.openai.com/docs/api-reference/vector-stores-file-batches/listBatchFiles
37+
*/
38+
public function listFiles(string $vectorStoreId, string $fileBatchId): VectorStoreFileListResponse;
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace OpenAI\Contracts\Resources;
4+
5+
use OpenAI\Responses\VectorStores\Files\VectorStoreFileDeleteResponse;
6+
use OpenAI\Responses\VectorStores\Files\VectorStoreFileListResponse;
7+
use OpenAI\Responses\VectorStores\Files\VectorStoreFileResponse;
8+
9+
interface VectorStoresFilesContract
10+
{
11+
/**
12+
* Create a file on a vector store
13+
*
14+
* @see https://platform.openai.com/docs/api-reference/vector-stores-files/createFile
15+
*
16+
* @param array<string, mixed> $parameters
17+
*/
18+
public function create(string $vectorStoreId, array $parameters): VectorStoreFileResponse;
19+
20+
/**
21+
* Returns a list of files within a vector store.
22+
*
23+
* @see https://platform.openai.com/docs/api-reference/vector-stores-files/listFiles
24+
*/
25+
public function list(string $vectorStoreId): VectorStoreFileListResponse;
26+
27+
/**
28+
* Retrieves a file within a vector store.
29+
*
30+
* @see https://platform.openai.com/docs/api-reference/vector-stores-files/getFile
31+
*/
32+
public function retrieve(string $vectorStoreId, string $fileId): VectorStoreFileResponse;
33+
34+
/**
35+
* Delete a file within a vector store.
36+
*
37+
* https://platform.openai.com/docs/api-reference/vector-stores/delete
38+
*/
39+
public function delete(string $vectorStoreId, string $fileId): VectorStoreFileDeleteResponse;
40+
}

src/OpenAI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function client(string $apiKey, ?string $organization = null): Cli
1515
return self::factory()
1616
->withApiKey($apiKey)
1717
->withOrganization($organization)
18-
->withHttpHeader('OpenAI-Beta', 'assistants=v1')
18+
->withHttpHeader('OpenAI-Beta', 'assistants=v2')
1919
->make();
2020
}
2121

src/Resources/Assistants.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace OpenAI\Resources;
66

77
use OpenAI\Contracts\Resources\AssistantsContract;
8-
use OpenAI\Contracts\Resources\AssistantsFilesContract;
98
use OpenAI\Responses\Assistants\AssistantDeleteResponse;
109
use OpenAI\Responses\Assistants\AssistantListResponse;
1110
use OpenAI\Responses\Assistants\AssistantResponse;
@@ -19,15 +18,15 @@ final class Assistants implements AssistantsContract
1918
/**
2019
* Create an assistant with a model and instructions.
2120
*
22-
* @see https://platform.openai.com/docs/api-reference/assistants/object
21+
* @see https://platform.openai.com/docs/api-reference/assistants/createAssistant
2322
*
2423
* @param array<string, mixed> $parameters
2524
*/
2625
public function create(array $parameters): AssistantResponse
2726
{
2827
$payload = Payload::create('assistants', $parameters);
2928

30-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
29+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
3130
$response = $this->transporter->requestObject($payload);
3231

3332
return AssistantResponse::from($response->data(), $response->meta());
@@ -42,7 +41,7 @@ public function retrieve(string $id): AssistantResponse
4241
{
4342
$payload = Payload::retrieve('assistants', $id);
4443

45-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
44+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
4645
$response = $this->transporter->requestObject($payload);
4746

4847
return AssistantResponse::from($response->data(), $response->meta());
@@ -59,7 +58,7 @@ public function modify(string $id, array $parameters): AssistantResponse
5958
{
6059
$payload = Payload::modify('assistants', $id, $parameters);
6160

62-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
61+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
6362
$response = $this->transporter->requestObject($payload);
6463

6564
return AssistantResponse::from($response->data(), $response->meta());
@@ -91,19 +90,9 @@ public function list(array $parameters = []): AssistantListResponse
9190
{
9291
$payload = Payload::list('assistants', $parameters);
9392

94-
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
93+
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
9594
$response = $this->transporter->requestObject($payload);
9695

9796
return AssistantListResponse::from($response->data(), $response->meta());
9897
}
99-
100-
/**
101-
* Manage files attached to an assistant.
102-
*
103-
* @see https://platform.openai.com/docs/api-reference/assistants
104-
*/
105-
public function files(): AssistantsFilesContract
106-
{
107-
return new AssistantsFiles($this->transporter);
108-
}
10998
}

0 commit comments

Comments
 (0)