Skip to content

Commit

Permalink
Remove Image API-related methods and tests
Browse files Browse the repository at this point in the history
Deleted the ImagesApiTest class from tests and the image generation methods from the OpenAiClient class as the Image API is planned to be removed and thus no longer necessary to support in this package. The reduction in complexity will facilitate future maintenance of the package.
  • Loading branch information
rodion-m committed Aug 14, 2023
1 parent 2a22c48 commit 8a9a7da
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 99 deletions.
68 changes: 0 additions & 68 deletions src/OpenAI.ChatGpt/OpenAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,72 +357,4 @@ public IAsyncEnumerable<ChatCompletionResponse> StreamChatCompletionsRaw(
cancellationToken
);
}

// Will be moved to a separate package.
internal async Task<byte[]> GenerateImageBytes(
string prompt,
string? user = null,
OpenAiImageSize size = OpenAiImageSize._1024,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(prompt))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(prompt));
var request = new ImageGenerationRequest(prompt, SizeToString(size), 1, "b64_json", user);
var response = await _httpClient.PostAsJsonAsync(
ImagesEndpoint,
request,
cancellationToken: cancellationToken
);
var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);

if (!response.IsSuccessStatusCode)
{
throw new NotExpectedResponseException(response.StatusCode, responseContent);
}

var jsonResponse =
JsonSerializer.Deserialize<ImagesGenerationB64Response>(responseContent)!;
return Convert.FromBase64String(jsonResponse.Data[0].B64Json);
}

// Will be moved to a separate package.
internal async Task<Uri[]> GenerateImagesUris(
string prompt,
string? user = null,
OpenAiImageSize size = OpenAiImageSize._1024,
int count = 1,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(prompt))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(prompt));
if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count));
var request = new ImageGenerationRequest(prompt, SizeToString(size), count, "url", user);
var response = await _httpClient.PostAsJsonAsync(
ImagesEndpoint,
request,
options: _nullIgnoreSerializerOptions,
cancellationToken: cancellationToken
);
var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);

if (!response.IsSuccessStatusCode)
{
throw new NotExpectedResponseException(response.StatusCode, responseContent);
}

var jsonResponse =
JsonSerializer.Deserialize<ImagesGenerationUriResponse>(responseContent)!;
return jsonResponse.Data.Select(it => it.Url).ToArray();
}

private static string SizeToString(OpenAiImageSize size)
{
return size switch
{
OpenAiImageSize._256 => "256x256",
OpenAiImageSize._512 => "512x512",
OpenAiImageSize._1024 => "1024x1024",
_ => throw new ArgumentOutOfRangeException(nameof(size), size, null)
};
}
}

This file was deleted.

0 comments on commit 8a9a7da

Please sign in to comment.