-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #537 from betalgo/feature/Batch-api
Batch API
- Loading branch information
Showing
17 changed files
with
363 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-3.5-turbo", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using OpenAI.Interfaces; | ||
using OpenAI.ObjectModels.RequestModels; | ||
|
||
namespace OpenAI.Playground.TestHelpers; | ||
|
||
internal static class BatchTestHelper | ||
{ | ||
public static async Task RunBatchOperationsTest(IOpenAIService sdk) | ||
{ | ||
ConsoleExtensions.WriteLine("Batch Operations Testing is starting:", ConsoleColor.Cyan); | ||
|
||
try | ||
{ | ||
ConsoleExtensions.WriteLine("Batch Create Test:", ConsoleColor.DarkCyan); | ||
|
||
const string fileName = "BatchDataSampleFile.jsonl"; | ||
var sampleFile = await FileExtensions.ReadAllBytesAsync($"SampleData/{fileName}"); | ||
ConsoleExtensions.WriteLine($"Uploading file {fileName}", ConsoleColor.DarkCyan); | ||
|
||
var fileUploadResult = await sdk.Files.UploadFile("batch", sampleFile, fileName); | ||
|
||
if (!fileUploadResult.Successful) | ||
{ | ||
throw new Exception("File upload failed"); | ||
} | ||
|
||
var batchCreateResult = await sdk.Batch.BatchCreate(new BatchCreateRequest | ||
{ | ||
InputFileId = fileUploadResult.Id, | ||
Endpoint = "/v1/chat/completions", | ||
CompletionWindow = "24h" | ||
}); | ||
|
||
if (!batchCreateResult.Successful) | ||
{ | ||
throw new Exception("Batch creation failed"); | ||
} | ||
|
||
ConsoleExtensions.WriteLine($"Batch ID: {batchCreateResult.Id}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($"Batch Status: {batchCreateResult.Status}", ConsoleColor.Green); | ||
|
||
ConsoleExtensions.WriteLine("Batch Retrieve Test:", ConsoleColor.DarkCyan); | ||
|
||
var batchRetrieveResult = await sdk.Batch.BatchRetrieve(batchCreateResult.Id); | ||
|
||
if (!batchRetrieveResult.Successful) | ||
{ | ||
throw new Exception("Batch retrieval failed"); | ||
} | ||
|
||
ConsoleExtensions.WriteLine($"Batch ID: {batchRetrieveResult.Id}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($"Batch Status: {batchRetrieveResult.Status}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($"Request Counts:", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($" Total: {batchRetrieveResult.RequestCounts.Total}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($" Completed: {batchRetrieveResult.RequestCounts.Completed}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($" Failed: {batchRetrieveResult.RequestCounts.Failed}", ConsoleColor.Green); | ||
|
||
ConsoleExtensions.WriteLine("Batch Cancel Test:", ConsoleColor.DarkCyan); | ||
|
||
var batchCancelResult = await sdk.Batch.BatchCancel(batchCreateResult.Id); | ||
|
||
if (!batchCancelResult.Successful) | ||
{ | ||
throw new Exception("Batch cancellation failed"); | ||
} | ||
|
||
ConsoleExtensions.WriteLine($"Batch ID: {batchCancelResult.Id}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($"Batch Status: {batchCancelResult.Status}", ConsoleColor.Green); | ||
ConsoleExtensions.WriteLine($"Cancelling At: {batchCancelResult.CancellingAt}", ConsoleColor.Green); | ||
} | ||
catch (Exception e) | ||
{ | ||
ConsoleExtensions.WriteLine($"Error: {e.Message}", ConsoleColor.Red); | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using OpenAI.ObjectModels.RequestModels; | ||
using OpenAI.ObjectModels.ResponseModels.BatchResponseModel; | ||
|
||
namespace OpenAI.Interfaces; | ||
|
||
public interface IBatchService | ||
{ | ||
/// <summary> | ||
/// Creates and executes a batch from an uploaded file of requests. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The created Batch object.</returns> | ||
Task<BatchResponse> BatchCreate(BatchCreateRequest request, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Retrieves a batch. | ||
/// </summary> | ||
/// <param name="batchId">The ID of the batch to retrieve.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The Batch object matching the specified ID.</returns> | ||
Task<BatchResponse?> BatchRetrieve(string batchId, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Cancels an in-progress batch. | ||
/// </summary> | ||
/// <param name="batchId">The ID of the batch to cancel.</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The Batch object matching the specified ID.</returns> | ||
Task<BatchResponse> BatchCancel(string batchId, CancellationToken cancellationToken = default); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using OpenAI.Extensions; | ||
using OpenAI.Interfaces; | ||
using OpenAI.ObjectModels.RequestModels; | ||
using OpenAI.ObjectModels.ResponseModels.BatchResponseModel; | ||
using System.Net.Http.Json; | ||
|
||
namespace OpenAI.Managers; | ||
|
||
public partial class OpenAIService : IBatchService | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<BatchResponse> BatchCreate(BatchCreateRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
return await _httpClient.PostAndReadAsAsync<BatchResponse>(_endpointProvider.BatchCreate(), request, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<BatchResponse?> BatchRetrieve(string batchId, CancellationToken cancellationToken = default) | ||
{ | ||
return await _httpClient.GetFromJsonAsync<BatchResponse>(_endpointProvider.BatchRetrieve(batchId), cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<BatchResponse> BatchCancel(string batchId, CancellationToken cancellationToken = default) | ||
{ | ||
return await _httpClient.PostAndReadAsAsync<BatchResponse>(_endpointProvider.BatchCancel(batchId),null, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
OpenAI.SDK/ObjectModels/RequestModels/BatchCreateRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OpenAI.ObjectModels.RequestModels; | ||
|
||
public record BatchCreateRequest | ||
{ | ||
/// <summary> | ||
/// The ID of an uploaded file that contains requests for the new batch. | ||
/// See [upload file](/docs/api-reference/files/create) for how to upload a file. | ||
/// Your input file must be formatted as a JSONL file, and must be uploaded with the purpose `batch`. | ||
/// </summary> | ||
[JsonPropertyName("input_file_id")] | ||
public string InputFileId { get; set; } | ||
|
||
/// <summary> | ||
/// The endpoint to be used for all requests in the batch. Currently only `/v1/chat/completions` is supported. | ||
/// </summary> | ||
[JsonPropertyName("endpoint")] | ||
public string Endpoint { get; set; } | ||
|
||
/// <summary> | ||
/// The time frame within which the batch should be processed. Currently only `24h` is supported. | ||
/// </summary> | ||
[JsonPropertyName("completion_window")] | ||
public string CompletionWindow { get; set; } | ||
|
||
/// <summary> | ||
/// Optional custom metadata for the batch. | ||
/// </summary> | ||
[JsonPropertyName("metadata")] | ||
public Dictionary<string, string>? MetaData { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.