Skip to content

Commit 8da3dbd

Browse files
author
michal.zyga
committed
add retrieve & cancel batch methods
1 parent 7b0890c commit 8da3dbd

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

core/src/main/scala/sttp/openai/OpenAI.scala

+34
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,44 @@ class OpenAI(authToken: String, baseUri: Uri = OpenAIUris.OpenAIBaseUri) {
11211121
*
11221122
* @param createBatchRequest
11231123
* Request body that will be used to create a batch.
1124+
* @return
1125+
* The created Batch object.
11241126
*/
11251127
def createBatch(createBatchRequest: BatchRequestBody): Request[Either[OpenAIException, BatchResponse]] =
11261128
openAIAuthRequest
11271129
.post(openAIUris.Batches)
11281130
.body(createBatchRequest)
11291131
.response(asJson_parseErrors[BatchResponse])
11301132

1133+
/** Retrieves a batch.
1134+
*
1135+
* [[https://platform.openai.com/docs/api-reference/batch/retreive]]
1136+
*
1137+
* @param batchId
1138+
* The ID of the batch to retrieve.
1139+
* @return
1140+
* The Batch object matching the specified ID.
1141+
*/
1142+
def retrieveBatch(batchId: String): Request[Either[OpenAIException, BatchResponse]] =
1143+
openAIAuthRequest
1144+
.get(openAIUris.batch(batchId))
1145+
.response(asJson_parseErrors[BatchResponse])
1146+
1147+
/** Cancels an in-progress batch. The batch will be in status cancelling for up to 10 minutes, before changing to cancelled, where it will
1148+
* have partial results (if any) available in the output file.
1149+
*
1150+
* [[https://platform.openai.com/docs/api-reference/batch/cancel]]
1151+
*
1152+
* @param batchId
1153+
* The ID of the batch to cancel.
1154+
* @return
1155+
* The Batch object matching the specified ID.
1156+
*/
1157+
def cancelBatch(batchId: String): Request[Either[OpenAIException, BatchResponse]] =
1158+
openAIAuthRequest
1159+
.post(openAIUris.cancelBatch(batchId))
1160+
.response(asJson_parseErrors[BatchResponse])
1161+
11311162
protected val openAIAuthRequest: PartialRequest[Either[String, String]] = basicRequest.auth
11321163
.bearer(authToken)
11331164

@@ -1163,6 +1194,9 @@ private class OpenAIUris(val baseUri: Uri) {
11631194
def fineTuningJobCheckpoints(fineTuningJobId: String): Uri = fineTuningJob(fineTuningJobId).addPath("checkpoints")
11641195
def cancelFineTuningJob(fineTuningJobId: String): Uri = fineTuningJob(fineTuningJobId).addPath("cancel")
11651196

1197+
def batch(batchId: String): Uri = Batches.addPath(batchId)
1198+
def cancelBatch(batchId: String): Uri = batch(batchId).addPath("cancel")
1199+
11661200
def file(fileId: String): Uri = Files.addPath(fileId)
11671201
def fileContent(fileId: String): Uri = Files.addPath(fileId, "content")
11681202
def model(modelId: String): Uri = Models.addPath(modelId)

core/src/main/scala/sttp/openai/OpenAISyncClient.scala

+27
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,37 @@ class OpenAISyncClient private (
805805
*
806806
* @param createBatchRequest
807807
* Request body that will be used to create a batch.
808+
* @return
809+
* The created Batch object.
808810
*/
809811
def createBatch(createBatchRequest: BatchRequestBody): BatchResponse =
810812
sendOrThrow(openAI.createBatch(createBatchRequest))
811813

814+
/** Retrieves a batch.
815+
*
816+
* [[https://platform.openai.com/docs/api-reference/batch/retreive]]
817+
*
818+
* @param batchId
819+
* The ID of the batch to retrieve.
820+
* @return
821+
* The Batch object matching the specified ID.
822+
*/
823+
def retrieveBatch(batchId: String): BatchResponse =
824+
sendOrThrow(openAI.retrieveBatch(batchId))
825+
826+
/** Cancels an in-progress batch. The batch will be in status cancelling for up to 10 minutes, before changing to cancelled, where it will
827+
* have partial results (if any) available in the output file.
828+
*
829+
* [[https://platform.openai.com/docs/api-reference/batch/cancel]]
830+
*
831+
* @param batchId
832+
* The ID of the batch to cancel.
833+
* @return
834+
* The Batch object matching the specified ID.
835+
*/
836+
def cancelBatch(batchId: String): BatchResponse =
837+
sendOrThrow(openAI.cancelBatch(batchId))
838+
812839
/** Closes and releases resources of http client if was not provided explicitly, otherwise works no-op. */
813840
def close(): Unit = if (closeClient) backend.close() else ()
814841

0 commit comments

Comments
 (0)