Skip to content

Fixes Nullability warnings #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions OpenAI.Playground/TestHelpers/ChatCompletionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static async Task RunSimpleChatCompletionTest(IOpenAIService sdk)

if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
Console.WriteLine(completionResult.Choices.First().Message?.Content);
}
else
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public static async Task RunSimpleCompletionStreamTest(IOpenAIService sdk)
{
if (completion.Successful)
{
Console.Write(completion.Choices.First().Message.Content);
Console.Write(completion.Choices.First().Message?.Content);
}
else
{
Expand Down Expand Up @@ -278,4 +278,4 @@ public static async Task RunChatFunctionCallTestAsStream(IOpenAIService sdk)
throw;
}
}
}
}
103 changes: 53 additions & 50 deletions OpenAI.Playground/TestHelpers/FileTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,67 +36,70 @@ public static async Task RunSimpleFileTest(IOpenAIService sdk)
await Task.Delay(10_000);
foreach (var uploadedFile in uploadedFiles.Data)
{
ConsoleExtensions.WriteLine($"Retrieving {uploadedFile.FileName}", ConsoleColor.DarkCyan);
var retrieveFileResponse = await sdk.Files.RetrieveFile(uploadedFile.Id);
if (retrieveFileResponse.Successful)
if (uploadedFile != null)
{
ConsoleExtensions.WriteLine($"{retrieveFileResponse.FileName} retrieved", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
ConsoleExtensions.WriteLine($"Retrieving {uploadedFile.FileName}", ConsoleColor.DarkCyan);
var retrieveFileResponse = await sdk.Files.RetrieveFile(uploadedFile.Id);
if (retrieveFileResponse.Successful)
{
ConsoleExtensions.WriteLine($"{retrieveFileResponse.FileName} retrieved", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}

var retrieveFileContentResponse = await sdk.Files.RetrieveFileContent(uploadedFile.Id);
if (retrieveFileContentResponse.Successful && retrieveFileContentResponse.Content?.Equals(sampleFileAsString) == true)
{
ConsoleExtensions.WriteLine($"retrieved content as string:{Environment.NewLine}{retrieveFileContentResponse.Content} ", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
var retrieveFileContentResponse = await sdk.Files.RetrieveFileContent(uploadedFile.Id);
if (retrieveFileContentResponse.Successful && retrieveFileContentResponse.Content?.Equals(sampleFileAsString) == true)
{
ConsoleExtensions.WriteLine($"retrieved content as string:{Environment.NewLine}{retrieveFileContentResponse.Content} ", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}

var retrieveFileContentResponseAsByteArray = await sdk.Files.RetrieveFileContent<byte[]>(uploadedFile.Id);
if (retrieveFileContentResponseAsByteArray.Content != null && sampleFileAsString == Encoding.UTF8.GetString(retrieveFileContentResponseAsByteArray.Content))
{
ConsoleExtensions.WriteLine($"retrieved content as byteArray:{Environment.NewLine}{Encoding.UTF8.GetString(retrieveFileContentResponseAsByteArray.Content)} ", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
var retrieveFileContentResponseAsByteArray = await sdk.Files.RetrieveFileContent<byte[]>(uploadedFile.Id);
if (retrieveFileContentResponseAsByteArray.Content != null && sampleFileAsString == Encoding.UTF8.GetString(retrieveFileContentResponseAsByteArray.Content))
{
ConsoleExtensions.WriteLine($"retrieved content as byteArray:{Environment.NewLine}{Encoding.UTF8.GetString(retrieveFileContentResponseAsByteArray.Content)} ", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}

var retrieveFileContentResponseAsStream = await sdk.Files.RetrieveFileContent<Stream>(uploadedFile.Id);
var retrieveFileContentResponseAsStream = await sdk.Files.RetrieveFileContent<Stream>(uploadedFile.Id);

if (retrieveFileContentResponseAsStream.Content != null)
{
var reader = new StreamReader(retrieveFileContentResponseAsStream.Content!);
var content = await reader.ReadToEndAsync();
if (content.Equals(sampleFileAsString))
if (retrieveFileContentResponseAsStream.Content != null)
{
ConsoleExtensions.WriteLine($"retrieved content as Stream:{Environment.NewLine}{content} ", ConsoleColor.DarkGreen);
var reader = new StreamReader(retrieveFileContentResponseAsStream.Content!);
var content = await reader.ReadToEndAsync();
if (content.Equals(sampleFileAsString))
{
ConsoleExtensions.WriteLine($"retrieved content as Stream:{Environment.NewLine}{content} ", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
}
else
{
ConsoleExtensions.WriteLine($"Retrieve {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}

// var fileContent = sdk.Files.RetrieveFileContent(file.Id);
ConsoleExtensions.WriteLine($"Deleting file {uploadedFile.FileName}", ConsoleColor.DarkCyan);
var deleteResponse = await sdk.Files.DeleteFile(uploadedFile.Id);
if (deleteResponse.Successful)
{
ConsoleExtensions.WriteLine($"{retrieveFileResponse.FileName} deleted", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Delete {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
// var fileContent = sdk.Files.RetrieveFileContent(file.Id);
ConsoleExtensions.WriteLine($"Deleting file {uploadedFile.FileName}", ConsoleColor.DarkCyan);
var deleteResponse = await sdk.Files.DeleteFile(uploadedFile.Id);
if (deleteResponse.Successful)
{
ConsoleExtensions.WriteLine($"{retrieveFileResponse.FileName} deleted", ConsoleColor.DarkGreen);
}
else
{
ConsoleExtensions.WriteLine($"Delete {retrieveFileResponse.FileName} failed", ConsoleColor.Red);
}
}
}
}
Expand Down Expand Up @@ -125,4 +128,4 @@ public static async Task CleanAllFiles(IOpenAIService sdk)
Console.WriteLine(e);
}
}
}
}
3 changes: 2 additions & 1 deletion OpenAI.Playground/TestHelpers/FineTuningTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static async Task CleanUpAllFineTunings(IOpenAIService sdk)
var fineTunes = await sdk.FineTunes.ListFineTunes();
foreach (var datum in fineTunes.Data)
{
if (datum.FineTunedModel == null) continue;
await sdk.FineTunes.DeleteFineTune(datum.FineTunedModel);
}
}
}
}
34 changes: 6 additions & 28 deletions OpenAI.SDK/Managers/OpenAIAudioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,11 @@ public async Task<AudioCreateTranscriptionResponse> CreateTranslation(AudioCreat

private async Task<AudioCreateTranscriptionResponse> Create(AudioCreateTranscriptionRequest audioCreateTranscriptionRequest, string uri, CancellationToken cancellationToken = default)
{
var multipartContent = new MultipartFormDataContent();

if (audioCreateTranscriptionRequest is {File: not null, FileStream: not null})
{
throw new ArgumentException("Either File or FileStream must be set, but not both.");
}

if (audioCreateTranscriptionRequest.File != null)
{
multipartContent.Add(
new ByteArrayContent(audioCreateTranscriptionRequest.File),
"file",
audioCreateTranscriptionRequest.FileName
);
}
else if (audioCreateTranscriptionRequest.FileStream != null)
var multipartContent = new MultipartFormDataContent
{
multipartContent.Add(
new StreamContent(audioCreateTranscriptionRequest.FileStream),
"file",
audioCreateTranscriptionRequest.FileName
);
}

multipartContent.Add(new StringContent(audioCreateTranscriptionRequest.Model), "model");

{new ByteArrayContent(audioCreateTranscriptionRequest.File), "file", audioCreateTranscriptionRequest.FileName ?? string.Empty},
{new StringContent(audioCreateTranscriptionRequest.Model ?? string.Empty), "model"}
};
if (audioCreateTranscriptionRequest.Language != null)
{
multipartContent.Add(new StringContent(audioCreateTranscriptionRequest.Language), "language");
Expand All @@ -71,8 +50,7 @@ private async Task<AudioCreateTranscriptionResponse> Create(AudioCreateTranscrip
}


if (null == audioCreateTranscriptionRequest.ResponseFormat ||
StaticValues.AudioStatics.ResponseFormat.Json == audioCreateTranscriptionRequest.ResponseFormat ||
if (StaticValues.AudioStatics.ResponseFormat.Json == audioCreateTranscriptionRequest.ResponseFormat ||
StaticValues.AudioStatics.ResponseFormat.VerboseJson == audioCreateTranscriptionRequest.ResponseFormat)
{
return await _httpClient.PostFileAndReadAsAsync<AudioCreateTranscriptionResponse>(uri, multipartContent, cancellationToken);
Expand All @@ -83,4 +61,4 @@ private async Task<AudioCreateTranscriptionResponse> Create(AudioCreateTranscrip
Text = await _httpClient.PostFileAndReadAsStringAsync(uri, multipartContent, cancellationToken)
};
}
}
}
18 changes: 14 additions & 4 deletions OpenAI.SDK/Managers/OpenAIImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class OpenAIService : IImageService
/// Creates an image given a prompt.
/// </summary>
/// <param name="imageCreateModel"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ImageCreateResponse> CreateImage(ImageCreateRequest imageCreateModel, CancellationToken cancellationToken = default)
{
Expand All @@ -21,6 +22,7 @@ public async Task<ImageCreateResponse> CreateImage(ImageCreateRequest imageCreat
/// Creates an edited or extended image given an original image and a prompt.
/// </summary>
/// <param name="imageEditCreateRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest imageEditCreateRequest, CancellationToken cancellationToken = default)
{
Expand All @@ -45,13 +47,17 @@ public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest im
multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
}

if (imageEditCreateRequest.Mask != null)
if (imageEditCreateRequest.Mask != null && imageEditCreateRequest.MaskName != null)
{
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Mask), "mask", imageEditCreateRequest.MaskName);
}

multipartContent.Add(new StringContent(imageEditCreateRequest.Prompt), "prompt");
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);

if (imageEditCreateRequest.Image != null && imageEditCreateRequest.ImageName != null)
{
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);
}

return await _httpClient.PostFileAndReadAsAsync<ImageCreateResponse>(_endpointProvider.ImageEditCreate(), multipartContent, cancellationToken);
}
Expand All @@ -60,6 +66,7 @@ public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest im
/// Creates a variation of a given image.
/// </summary>
/// <param name="imageEditCreateRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ImageCreateResponse> CreateImageVariation(ImageVariationCreateRequest imageEditCreateRequest, CancellationToken cancellationToken = default)
{
Expand All @@ -84,8 +91,11 @@ public async Task<ImageCreateResponse> CreateImageVariation(ImageVariationCreate
multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
}

multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);
if (imageEditCreateRequest.Image != null && imageEditCreateRequest.ImageName != null)
{
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);
}

return await _httpClient.PostFileAndReadAsAsync<ImageCreateResponse>(_endpointProvider.ImageVariationCreate(), multipartContent, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,22 @@ public record AudioCreateTranscriptionRequest : IOpenAiModels.IModel, IOpenAiMod
/// <summary>
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
public byte[]? File { get; set; }

/// <summary>
/// The stream of the audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
public Stream? FileStream { get; set; }
public byte[] File { get; set; } = Array.Empty<byte>();

/// <summary>
/// FileName
/// </summary>
public string FileName { get; set; }
public string? FileName { get; set; } = string.Empty;

/// <summary>
/// ID of the model to use. Only whisper-1 is currently available.
/// </summary>
public string Model { get; set; }
public string? Model { get; set; }

/// <summary>
/// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower
/// values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to
/// automatically increase the temperature until certain thresholds are hit.
/// </summary>
public float? Temperature { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ChatCompletionCreateRequest : IModelValidate, IOpenAiModels.ITemper
/// short as 1 message or fill many pages.
/// </summary>
[JsonPropertyName("messages")]
public IList<ChatMessage> Messages { get; set; }
public IList<ChatMessage>? Messages { get; set; }

/// <summary>
/// A list of functions the model may generate JSON inputs for.
Expand Down Expand Up @@ -86,7 +86,7 @@ public IList<string>? StopCalculated

if (Stop != null)
{
return new List<string> {Stop};
return new List<string> { Stop };
}

return StopAsList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IList<string>? PromptCalculated

if (Prompt != null)
{
return new List<string> {Prompt};
return new List<string> { Prompt };
}


Expand Down Expand Up @@ -125,7 +125,7 @@ public IList<string>? StopCalculated

if (Stop != null)
{
return new List<string> {Stop};
return new List<string> { Stop };
}

return StopAsList;
Expand Down
2 changes: 1 addition & 1 deletion OpenAI.SDK/ObjectModels/RequestModels/EditCreateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public record EditCreateRequest : IModelValidate, IOpenAiModels.ITemperature, IO
/// The instruction that tells the model how to edit the prompt.
/// </summary>
[JsonPropertyName("instruction")]
public string Instruction { get; set; }
public string? Instruction { get; set; }

/// <summary>
/// Defaults to 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public IList<string>? InputCalculated

if (Input != null)
{
return new List<string> {Input};
return new List<string> { Input };
}

return InputAsList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace OpenAI.ObjectModels.RequestModels;

public record FineTuneCancelRequest
{
[JsonPropertyName("fine_tune_id")] public string FineTuneId { get; set; }
}
[JsonPropertyName("fine_tune_id")] public string? FineTuneId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public record FineTuneCreateRequest : IOpenAiModels.IModel
/// more details.
/// </summary>
[JsonPropertyName("training_file")]
public string TrainingFile { get; set; }
public string? TrainingFile { get; set; }

/// <summary>
/// The ID of an uploaded file that contains validation data.
Expand Down Expand Up @@ -119,4 +119,4 @@ public record FineTuneCreateRequest : IOpenAiModels.IModel
/// </summary>
[JsonPropertyName("model")]
public string? Model { get; set; }
}
}
4 changes: 2 additions & 2 deletions OpenAI.SDK/ObjectModels/RequestModels/ImageCreateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public ImageCreateRequest(string prompt)
/// A text description of the desired image(s). The maximum length is 1000 characters.
/// </summary>
[JsonPropertyName("prompt")]
public string Prompt { get; set; }
}
public string Prompt { get; set; } = string.Empty;
}
Loading