-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBatchesRawService.php
More file actions
249 lines (236 loc) · 8.67 KB
/
BatchesRawService.php
File metadata and controls
249 lines (236 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
declare(strict_types=1);
namespace Anthropic\Services\Messages;
use Anthropic\Client;
use Anthropic\Core\Contracts\BaseResponse;
use Anthropic\Core\Contracts\BaseStream;
use Anthropic\Core\Exceptions\APIException;
use Anthropic\Core\Util;
use Anthropic\JsonLStream;
use Anthropic\Messages\Batches\BatchCreateParams;
use Anthropic\Messages\Batches\BatchCreateParams\Request;
use Anthropic\Messages\Batches\BatchListParams;
use Anthropic\Messages\Batches\DeletedMessageBatch;
use Anthropic\Messages\Batches\MessageBatch;
use Anthropic\Messages\Batches\MessageBatchIndividualResponse;
use Anthropic\Page;
use Anthropic\RequestOptions;
use Anthropic\ServiceContracts\Messages\BatchesRawContract;
/**
* @phpstan-import-type RequestShape from \Anthropic\Messages\Batches\BatchCreateParams\Request
* @phpstan-import-type RequestOpts from \Anthropic\RequestOptions
*/
final class BatchesRawService implements BatchesRawContract
{
// @phpstan-ignore-next-line
/**
* @internal
*/
public function __construct(private Client $client) {}
/**
* @api
*
* Send a batch of Message creation requests.
*
* The Message Batches API can be used to process multiple Messages API requests at once. Once a Message Batch is created, it begins processing immediately. Batches can take up to 24 hours to complete.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param array{requests: list<Request|RequestShape>}|BatchCreateParams $params
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<MessageBatch>
*
* @throws APIException
*/
public function create(
array|BatchCreateParams $params,
RequestOptions|array|null $requestOptions = null,
): BaseResponse {
[$parsed, $options] = BatchCreateParams::parseRequest(
$params,
$requestOptions,
);
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'post',
path: 'v1/messages/batches',
body: (object) $parsed,
options: $options,
convert: MessageBatch::class,
);
}
/**
* @api
*
* This endpoint is idempotent and can be used to poll for Message Batch completion. To access the results of a Message Batch, make a request to the `results_url` field in the response.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param string $messageBatchID ID of the Message Batch
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<MessageBatch>
*
* @throws APIException
*/
public function retrieve(
string $messageBatchID,
RequestOptions|array|null $requestOptions = null
): BaseResponse {
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'get',
path: ['v1/messages/batches/%1$s', $messageBatchID],
options: $requestOptions,
convert: MessageBatch::class,
);
}
/**
* @api
*
* List all Message Batches within a Workspace. Most recently created batches are returned first.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param array{
* afterID?: string, beforeID?: string, limit?: int
* }|BatchListParams $params
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<Page<MessageBatch>>
*
* @throws APIException
*/
public function list(
array|BatchListParams $params,
RequestOptions|array|null $requestOptions = null,
): BaseResponse {
[$parsed, $options] = BatchListParams::parseRequest(
$params,
$requestOptions,
);
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'get',
path: 'v1/messages/batches',
query: Util::array_transform_keys(
$parsed,
['afterID' => 'after_id', 'beforeID' => 'before_id']
),
options: $options,
convert: MessageBatch::class,
page: Page::class,
);
}
/**
* @api
*
* Delete a Message Batch.
*
* Message Batches can only be deleted once they've finished processing. If you'd like to delete an in-progress batch, you must first cancel it.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param string $messageBatchID ID of the Message Batch
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<DeletedMessageBatch>
*
* @throws APIException
*/
public function delete(
string $messageBatchID,
RequestOptions|array|null $requestOptions = null
): BaseResponse {
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'delete',
path: ['v1/messages/batches/%1$s', $messageBatchID],
options: $requestOptions,
convert: DeletedMessageBatch::class,
);
}
/**
* @api
*
* Batches may be canceled any time before processing ends. Once cancellation is initiated, the batch enters a `canceling` state, at which time the system may complete any in-progress, non-interruptible requests before finalizing cancellation.
*
* The number of canceled requests is specified in `request_counts`. To determine which requests were canceled, check the individual results within the batch. Note that cancellation may not result in any canceled requests if they were non-interruptible.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param string $messageBatchID ID of the Message Batch
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<MessageBatch>
*
* @throws APIException
*/
public function cancel(
string $messageBatchID,
RequestOptions|array|null $requestOptions = null
): BaseResponse {
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'post',
path: ['v1/messages/batches/%1$s/cancel', $messageBatchID],
options: $requestOptions,
convert: MessageBatch::class,
);
}
/**
* @api
*
* Streams the results of a Message Batch as a `.jsonl` file.
*
* Each line in the file is a JSON object containing the result of a single request in the Message Batch. Results are not guaranteed to be in the same order as requests. Use the `custom_id` field to match results to requests.
*
* Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
*
* @param string $messageBatchID ID of the Message Batch
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<BaseStream<MessageBatchIndividualResponse>>
*
* @throws APIException
*/
public function results(
string $messageBatchID,
RequestOptions|array|null $requestOptions = null
): BaseResponse {
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'get',
path: ['v1/messages/batches/%1$s/results', $messageBatchID],
headers: ['Accept' => 'application/x-jsonl'],
options: $requestOptions,
convert: MessageBatchIndividualResponse::class,
stream: JsonLStream::class,
);
}
/**
* @api
*
* @param string $messageBatchID ID of the Message Batch
* @param RequestOpts|null $requestOptions
*
* @return BaseResponse<BaseStream<MessageBatchIndividualResponse>>
*
* @throws APIException
*/
public function resultsStream(
string $messageBatchID,
RequestOptions|array|null $requestOptions = null
): BaseResponse {
// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'get',
path: ['v1/messages/batches/%1$s/results', $messageBatchID],
headers: ['Accept' => 'application/x-jsonl'],
options: $requestOptions,
convert: MessageBatchIndividualResponse::class,
stream: JsonLStream::class,
);
}
}