Skip to content

Commit 934d685

Browse files
committed
add docs
1 parent 2353162 commit 934d685

12 files changed

+169
-5
lines changed

Sources/AnthropicSwiftSDK/Entity/Batch/BatchParameter.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88
import FunctionCalling
99

1010
public struct BatchParameter {
11+
/// Input messages.
1112
public let messages: [Message]
13+
/// The model that will complete your prompt.
14+
///
15+
/// See [models](https://docs.anthropic.com/en/docs/models-overview) for additional details and options.
1216
public let model: Model
17+
/// System prompt.
18+
///
19+
/// A system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role.
1320
public let system: [SystemPrompt]
21+
/// The maximum number of tokens to generate before stopping.
1422
public let maxTokens: Int
23+
/// An object describing metadata about the request.
1524
public let metaData: MetaData?
25+
/// Custom text sequences that will cause the model to stop generating.
1626
public let stopSequence: [String]?
1727
/// Whether the response should be handles as streaming or not,
1828
///
@@ -21,10 +31,15 @@ public struct BatchParameter {
2131
/// - Note: Streaming is not supported for batch requests.
2232
/// For more details, see https://docs.anthropic.com/en/docs/build-with-claude/message-batches#can-i-use-the-message-batches-api-with-other-api-features
2333
public let stream: Bool = false
34+
/// Amount of randomness injected into the response.
2435
public let temperature: Double?
36+
/// Use nucleus sampling.
2537
public let topP: Double?
38+
/// Only sample from the top K options for each subsequent token.
2639
public let topK: Int?
40+
/// Definitions of tools that the model may use.
2741
public let toolContainer: ToolContainer?
42+
/// How the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself.
2843
public let toolChoice: ToolChoice
2944

3045
public init(

Sources/AnthropicSwiftSDK/Entity/Batch/BatchRequestCounts.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
// Created by 伊藤史 on 2024/10/09.
66
//
77

8+
/// Tallies requests within the Message Batch, categorized by their status.
89
public struct BatchRequestCounts: Decodable {
10+
/// Number of requests in the Message Batch that are processing.
911
let processing: Int
12+
/// Number of requests in the Message Batch that have completed successfully.
13+
///
14+
/// This is zero until processing of the entire Message Batch has ended.
1015
let succeeeded: Int
16+
/// Number of requests in the Message Batch that encountered an error.
17+
///
18+
/// This is zero until processing of the entire Message Batch has ended.
1119
let errored: Int
20+
/// Number of requests in the Message Batch that have been canceled.
21+
///
22+
/// This is zero until processing of the entire Message Batch has ended.
1223
let canceled: Int
24+
/// Number of requests in the Message Batch that have expired.
25+
///
26+
/// This is zero until processing of the entire Message Batch has ended.
1327
let expired: Int
1428
}

Sources/AnthropicSwiftSDK/Entity/Batch/BatchResultType.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
// Created by 伊藤史 on 2024/10/09.
66
//
77

8-
/// https://docs.anthropic.com/en/docs/build-with-claude/message-batches#retrieving-batch-results
8+
/// Once batch processing has ended, each Messages request in the batch will have a result.
99
public enum BatchResultType: String, Decodable {
10-
case succeeded // include the message result as jsonl
10+
/// Request was successful. Includes the message result.
11+
case succeeded
12+
/// Request encountered an error and a message was not created.
13+
///
14+
/// Possible errors include invalid requests and internal server errors. You will not be billed for these requests.
1115
case errored
16+
/// User canceled the batch before this request could be sent to the model. You will not be billed for these requests.
1217
case cancelled
18+
/// Batch reached its 24 hour expiration before this request could be sent to the model. You will not be billed for these requests.
1319
case expired
1420
}

Sources/AnthropicSwiftSDK/Entity/Batch/MessageBatch.swift

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

88
public struct MessageBatch {
9+
/// Developer-provided ID created for each request in a Message Batch. Useful for matching results to requests, as results may be given out of request order.
10+
///
11+
/// Must be unique for each request within the Message Batch.
912
public let customId: String
13+
/// Messages API creation parameters for the individual request.
14+
///
15+
/// See the [Messages API](https://docs.anthropic.com/en/api/messages) reference for full documentation on available parameters.
1016
public let parameter: BatchParameter
1117

1218
public init(customId: String, parameter: BatchParameter) {

Sources/AnthropicSwiftSDK/Entity/Batch/ProcessingStatus.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by 伊藤史 on 2024/10/09.
66
//
77

8-
/// https://docs.anthropic.com/en/api/retrieving-message-batches
8+
/// Processing status of the Message Batch.
99
public enum ProcessingStatus: String, RawRepresentable, Decodable {
1010
case inProgress = "in_progress"
1111
case canceling

Sources/AnthropicSwiftSDK/MessageBatches.swift

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,28 @@ import Foundation
99
import FunctionCalling
1010
import SwiftyJSONLines
1111

12+
/// A class responsible for managing message batches in the Anthropic API.
1213
public struct MessageBatches {
14+
/// The API key used for authentication with the Anthropic API.
1315
private let apiKey: String
16+
/// The URL session used for network requests.
1417
private let session: URLSession
1518

19+
/// Initializes a new instance of `MessageBatches`.
20+
///
21+
/// - Parameters:
22+
/// - apiKey: The API key for authentication.
23+
/// - session: The URL session for network requests.
1624
init(apiKey: String, session: URLSession) {
1725
self.apiKey = apiKey
1826
self.session = session
1927
}
2028

29+
/// Creates new message batches.
30+
///
31+
/// - Parameter batches: An array of `MessageBatch` to be created.
32+
/// - Returns: A `BatchResponse` containing the details of the created batches.
33+
/// - Throws: An error if the request fails.
2134
public func createBatches(batches: [MessageBatch]) async throws -> BatchResponse {
2235
try await createBatches(
2336
batches: batches,
@@ -26,6 +39,14 @@ public struct MessageBatches {
2639
)
2740
}
2841

42+
/// Creates new message batches with custom header providers.
43+
///
44+
/// - Parameters:
45+
/// - batches: An array of `MessageBatch` to be created.
46+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
47+
/// - authenticationHeaderProvider: The provider for authentication headers.
48+
/// - Returns: A `BatchResponse` containing the details of the created batches.
49+
/// - Throws: An error if the request fails.
2950
public func createBatches(
3051
batches: [MessageBatch],
3152
anthropicHeaderProvider: AnthropicHeaderProvider,
@@ -51,6 +72,11 @@ public struct MessageBatches {
5172
return try anthropicJSONDecoder.decode(BatchResponse.self, from: data)
5273
}
5374

75+
/// Retrieves a specific message batch by its ID.
76+
///
77+
/// - Parameter batchId: The ID of the batch to retrieve.
78+
/// - Returns: A `BatchResponse` containing the details of the retrieved batch.
79+
/// - Throws: An error if the request fails.
5480
public func retrieve(batchId: String) async throws -> BatchResponse {
5581
try await retrieve(
5682
batchId: batchId,
@@ -59,6 +85,14 @@ public struct MessageBatches {
5985
)
6086
}
6187

88+
/// Retrieves a specific message batch by its ID with custom header providers.
89+
///
90+
/// - Parameters:
91+
/// - batchId: The ID of the batch to retrieve.
92+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
93+
/// - authenticationHeaderProvider: The provider for authentication headers.
94+
/// - Returns: A `BatchResponse` containing the details of the retrieved batch.
95+
/// - Throws: An error if the request fails.
6296
public func retrieve(
6397
batchId: String,
6498
anthropicHeaderProvider: AnthropicHeaderProvider,
@@ -84,6 +118,11 @@ public struct MessageBatches {
84118
return try anthropicJSONDecoder.decode(BatchResponse.self, from: data)
85119
}
86120

121+
/// Retrieves the results of a specific message batch by its ID.
122+
///
123+
/// - Parameter batchId: The ID of the batch to retrieve results for.
124+
/// - Returns: An array of `BatchResultResponse` containing the results of the batch.
125+
/// - Throws: An error if the request fails.
87126
public func results(of batchId: String) async throws -> [BatchResultResponse] {
88127
try await results(
89128
of: batchId,
@@ -92,6 +131,14 @@ public struct MessageBatches {
92131
)
93132
}
94133

134+
/// Retrieves the results of a specific message batch by its ID with custom header providers.
135+
///
136+
/// - Parameters:
137+
/// - batchId: The ID of the batch to retrieve results for.
138+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
139+
/// - authenticationHeaderProvider: The provider for authentication headers.
140+
/// - Returns: An array of `BatchResultResponse` containing the results of the batch.
141+
/// - Throws: An error if the request fails.
95142
public func results(
96143
of batchId: String,
97144
anthropicHeaderProvider: AnthropicHeaderProvider,
@@ -117,6 +164,11 @@ public struct MessageBatches {
117164
return try JSONLines(data: data).toObjects(with: anthropicJSONDecoder)
118165
}
119166

167+
/// Streams the results of a specific message batch by its ID.
168+
///
169+
/// - Parameter batchId: The ID of the batch to stream results for.
170+
/// - Returns: An `AsyncThrowingStream` of `BatchResultResponse` containing the streamed results of the batch.
171+
/// - Throws: An error if the request fails.
120172
public func results(streamOf batchId: String) async throws -> AsyncThrowingStream<BatchResultResponse, Error> {
121173
try await results(
122174
streamOf: batchId,
@@ -125,6 +177,14 @@ public struct MessageBatches {
125177
)
126178
}
127179

180+
/// Streams the results of a specific message batch by its ID with custom header providers.
181+
///
182+
/// - Parameters:
183+
/// - batchId: The ID of the batch to stream results for.
184+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
185+
/// - authenticationHeaderProvider: The provider for authentication headers.
186+
/// - Returns: An `AsyncThrowingStream` of `BatchResultResponse` containing the streamed results of the batch.
187+
/// - Throws: An error if the request fails.
128188
public func results(
129189
streamOf batchId: String,
130190
anthropicHeaderProvider: AnthropicHeaderProvider,
@@ -164,6 +224,14 @@ public struct MessageBatches {
164224
}
165225
}
166226

227+
/// Lists message batches with optional pagination.
228+
///
229+
/// - Parameters:
230+
/// - beforeId: The ID to list batches before.
231+
/// - afterId: The ID to list batches after.
232+
/// - limit: The maximum number of batches to list.
233+
/// - Returns: A `BatchListResponse` containing the list of batches.
234+
/// - Throws: An error if the request fails.
167235
public func list(beforeId: String? = nil, afterId: String? = nil, limit: Int = 20) async throws -> BatchListResponse {
168236
try await list(
169237
beforeId: beforeId,
@@ -174,6 +242,16 @@ public struct MessageBatches {
174242
)
175243
}
176244

245+
/// Lists message batches with optional pagination and custom header providers.
246+
///
247+
/// - Parameters:
248+
/// - beforeId: The ID to list batches before.
249+
/// - afterId: The ID to list batches after.
250+
/// - limit: The maximum number of batches to list.
251+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
252+
/// - authenticationHeaderProvider: The provider for authentication headers.
253+
/// - Returns: A `BatchListResponse` containing the list of batches.
254+
/// - Throws: An error if the request fails.
177255
public func list(
178256
beforeId: String?,
179257
afterId: String?,
@@ -213,6 +291,11 @@ public struct MessageBatches {
213291
return try anthropicJSONDecoder.decode(BatchListResponse.self, from: data)
214292
}
215293

294+
/// Cancels a specific message batch by its ID.
295+
///
296+
/// - Parameter batchId: The ID of the batch to cancel.
297+
/// - Returns: A `BatchResponse` containing the details of the canceled batch.
298+
/// - Throws: An error if the request fails.
216299
public func cancel(batchId: String) async throws -> BatchResponse {
217300
try await cancel(
218301
batchId: batchId,
@@ -221,6 +304,14 @@ public struct MessageBatches {
221304
)
222305
}
223306

307+
/// Cancels a specific message batch by its ID with custom header providers.
308+
///
309+
/// - Parameters:
310+
/// - batchId: The ID of the batch to cancel.
311+
/// - anthropicHeaderProvider: The provider for Anthropic-specific headers.
312+
/// - authenticationHeaderProvider: The provider for authentication headers.
313+
/// - Returns: A `BatchResponse` containing the details of the canceled batch.
314+
/// - Throws: An error if the request fails.
224315
public func cancel(
225316
batchId: String,
226317
anthropicHeaderProvider: AnthropicHeaderProvider,

Sources/AnthropicSwiftSDK/Network/Request/CancelMessageBatchRequest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
// Created by 伊藤史 on 2024/10/16.
66
//
77

8+
/// Batches may be canceled any time before processing ends.
9+
///
10+
/// 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.
11+
/// 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.
812
struct CancelMessageBatchRequest: Request {
913
let method: HttpMethod = .post
1014
var path: String {
1115
"\(RequestType.batches.basePath)/\(batchId)/cancel"
1216
}
1317
let queries: [String: CustomStringConvertible]? = nil
1418
let body: Never? = nil
19+
/// ID of the Message Batch.
1520
let batchId: String
1621
}

Sources/AnthropicSwiftSDK/Network/Request/ListMessageBatchesRequest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
// Created by 伊藤史 on 2024/10/16.
66
//
77

8+
/// List all Message Batches within a Workspace. Most recently created batches are returned first.
89
struct ListMessageBatchesRequest: Request {
910
let method: HttpMethod = .get
1011
let path: String = RequestType.batches.basePath
1112
let queries: [String: CustomStringConvertible]?
1213
let body: Never? = nil
1314

1415
enum Parameter: String {
16+
/// ID of the object to use as a cursor for pagination. When provided, returns the page of results immediately before this object.
1517
case beforeId = "before_id"
18+
/// ID of the object to use as a cursor for pagination. When provided, returns the page of results immediately after this object.
1619
case afterId = "after_id"
20+
/// Number of items to return per page.
21+
///
22+
/// Defaults to 20. Ranges from 1 to 100.
1723
case limit
1824
}
1925
}

Sources/AnthropicSwiftSDK/Network/Request/MessageBatchesRequest.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
// Created by 伊藤史 on 2024/10/09.
66
//
77

8+
/// The Message Batches API can be used to process multiple Messages API requests at once.
9+
///
10+
/// Once a Message Batch is created, it begins processing immediately.
11+
/// Batches can take up to 24 hours to complete.
12+
///
13+
/// The Message Batches API supports the following models: Claude 3 Haiku, Claude 3 Opus, and Claude 3.5 Sonnet.
14+
/// All features available in the Messages API, including beta features, are available through the Message Batches API.
15+
/// While in beta, batches may contain up to 10,000 requests and be up to 32 MB in total size.
816
struct MessageBatchesRequest: Request {
917
typealias Body = MessageBatchesRequestBody
1018

@@ -16,7 +24,6 @@ struct MessageBatchesRequest: Request {
1624

1725
// MARK: Request Body
1826

19-
/// https://docs.anthropic.com/en/api/creating-message-batches
2027
struct MessageBatchesRequestBody: Encodable {
2128
/// List of requests for prompt completion. Each is an individual request to create a Message.
2229
let requests: [Batch]
@@ -26,7 +33,6 @@ struct MessageBatchesRequestBody: Encodable {
2633
}
2734
}
2835

29-
/// https://docs.anthropic.com/en/api/creating-message-batches
3036
struct Batch: Encodable {
3137
/// Developer-provided ID created for each request in a Message Batch. Useful for matching results to requests, as results may be given out of request order.
3238
///

Sources/AnthropicSwiftSDK/Network/Request/RetrieveMessageBatchResultsRequest.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// Created by 伊藤史 on 2024/10/16.
66
//
77

8+
/// Streams the results of a Message Batch as a .jsonl file.
9+
///
10+
/// Each line in the file is a JSON object containing the result of a single request in the Message Batch.
11+
/// Results are not guaranteed to be in the same order as requests. Use the custom_id field to match results to requests.
12+
///
13+
/// While in beta, this endpoint requires passing the anthropic-beta header with value message-batches-2024-09-24
814
struct RetrieveMessageBatchResultsRequest: Request {
915
let method: HttpMethod = .get
1016
var path: String {
@@ -13,5 +19,6 @@ struct RetrieveMessageBatchResultsRequest: Request {
1319
let queries: [String: CustomStringConvertible]? = nil
1420
let body: Never? = nil
1521

22+
/// ID of the Message Batch.
1623
let batchId: String
1724
}

0 commit comments

Comments
 (0)