Skip to content

Commit

Permalink
feat: Add support for Batch API in openai_dart (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Apr 16, 2024
1 parent 97d7977 commit 6b89f4a
Show file tree
Hide file tree
Showing 16 changed files with 3,137 additions and 26 deletions.
33 changes: 33 additions & 0 deletions packages/openai_dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Unofficial Dart client for [OpenAI](https://platform.openai.com/docs/api-referen
- Completions (legacy)
- Embeddings
- Fine-tuning
- Batch
- Images
- Models
- Moderations
Expand All @@ -41,6 +42,7 @@ Unofficial Dart client for [OpenAI](https://platform.openai.com/docs/api-referen
* [Completions (legacy)](#completions-legacy)
* [Embeddings](#embeddings)
* [Fine-tuning](#fine-tuning)
* [Batch](#batch)
* [Images](#images)
* [Models](#models)
* [Moderations](#moderations)
Expand Down Expand Up @@ -520,6 +522,37 @@ final res = await client.listFineTuningEvents(
);
```

### Batch

Create large batches of API requests to run asynchronously

**Create batch:**

```dart
const request = CreateBatchRequest(
inputFileId: 'file-abc123',
endpoint: BatchEndpoint.v1ChatCompletions,
completionWindow: BatchCompletionWindow.v24h,
);
final res = await client.createBatch(request: request);
```

**Retrieve batch:**

```dart
final res = await client.retrieveBatch(
batchId: 'batch_abc123',
);
```

**Cancel batch:**

```dart
final res = await client.cancelBatch(
batchId: 'batch_abc123',
);
```

### Images

Given a prompt and/or an input image, the model will generate a new image.
Expand Down
70 changes: 70 additions & 0 deletions packages/openai_dart/lib/src/generated/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,76 @@ class OpenAIClient {
return ListFineTuningJobCheckpointsResponse.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: createBatch
// ------------------------------------------

/// Creates and executes a batch from an uploaded file of requests
///
/// `request`: Represents a request to create a new batch.
///
/// `POST` `https://api.openai.com/v1/batches`
Future<Batch> createBatch({
required CreateBatchRequest request,
}) async {
final r = await makeRequest(
baseUrl: 'https://api.openai.com/v1',
path: '/batches',
method: HttpMethod.post,
isMultipart: false,
requestType: 'application/json',
responseType: 'application/json',
body: request,
);
return Batch.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: retrieveBatch
// ------------------------------------------

/// Retrieves a batch.
///
/// `batchId`: The ID of the batch to retrieve.
///
/// `GET` `https://api.openai.com/v1/batches/{batch_id}`
Future<Batch> retrieveBatch({
required String batchId,
}) async {
final r = await makeRequest(
baseUrl: 'https://api.openai.com/v1',
path: '/batches/$batchId',
method: HttpMethod.get,
isMultipart: false,
requestType: '',
responseType: 'application/json',
);
return Batch.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: cancelBatch
// ------------------------------------------

/// Cancels an in-progress batch.
///
/// `batchId`: The ID of the batch to cancel.
///
/// `POST` `https://api.openai.com/v1/batches/{batch_id}/cancel`
Future<Batch> cancelBatch({
required String batchId,
}) async {
final r = await makeRequest(
baseUrl: 'https://api.openai.com/v1',
path: '/batches/$batchId/cancel',
method: HttpMethod.post,
isMultipart: false,
requestType: '',
responseType: 'application/json',
);
return Batch.fromJson(_jsonDecode(r));
}

// ------------------------------------------
// METHOD: createImage
// ------------------------------------------
Expand Down

0 comments on commit 6b89f4a

Please sign in to comment.