Skip to content

Commit

Permalink
OpenAI-DotNet 4.2.0 (#16)
Browse files Browse the repository at this point in the history
- Fixed missing completions namespace
- Fixed missing image edit response disposal
- misc formatting
  • Loading branch information
StephenHodgson authored Jan 28, 2023
1 parent 7fba090 commit fce110d
Show file tree
Hide file tree
Showing 50 changed files with 101 additions and 98 deletions.
11 changes: 6 additions & 5 deletions OpenAI-DotNet-Tests/TestFixture_01_Models.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
using NUnit.Framework;
using System;
using System.Threading.Tasks;

namespace OpenAI.Tests
{
internal class TestFixture_01_Models
{
[Test]
public void Test_1_GetModels()
public async Task Test_1_GetModels()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
var results = api.ModelsEndpoint.GetModelsAsync().Result;
var results = await api.ModelsEndpoint.GetModelsAsync();
Assert.IsNotNull(results);
Assert.NotZero(results.Count);
}

[Test]
public void Test_2_RetrieveModelDetails()
public async Task Test_2_RetrieveModelDetails()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
var models = api.ModelsEndpoint.GetModelsAsync().Result;
var models = await api.ModelsEndpoint.GetModelsAsync();

Console.WriteLine($"Found {models.Count} models!");

Expand All @@ -28,7 +29,7 @@ public void Test_2_RetrieveModelDetails()

try
{
var result = api.ModelsEndpoint.GetModelDetailsAsync(model.Id).Result;
var result = await api.ModelsEndpoint.GetModelDetailsAsync(model.Id);
Assert.IsNotNull(result);
}
catch (Exception e)
Expand Down
1 change: 1 addition & 0 deletions OpenAI-DotNet-Tests/TestFixture_02_Completions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using OpenAI.Completions;
using OpenAI.Models;
using System;
using System.Collections.Generic;
Expand Down
5 changes: 3 additions & 2 deletions OpenAI-DotNet-Tests/TestFixture_03_Edits.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using NUnit.Framework;
using OpenAI.Edits;
using System;
using System.Threading.Tasks;

namespace OpenAI.Tests
{
internal class TestFixture_03_Edits
{
[Test]
public void Test_1_GetBasicEdit()
public async Task Test_1_GetBasicEdit()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.EditsEndpoint);
var request = new EditRequest("What day of the wek is it?", "Fix the spelling mistakes");
var result = api.EditsEndpoint.CreateEditAsync(request).Result;
var result = await api.EditsEndpoint.CreateEditAsync(request);
Assert.IsNotNull(result);
Assert.NotNull(result.Choices);
Assert.NotZero(result.Choices.Count);
Expand Down
13 changes: 7 additions & 6 deletions OpenAI-DotNet-Tests/TestFixture_04_Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
using OpenAI.Images;
using System;
using System.IO;
using System.Threading.Tasks;

namespace OpenAI.Tests
{
internal class TestFixture_04_Images
{
[Test]
public void Test_1_GenerateImages()
public async Task Test_1_GenerateImages()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.ImagesEndPoint);

var results = api.ImagesEndPoint.GenerateImageAsync("A house riding a velociraptor", 1, ImageSize.Small).Result;
var results = await api.ImagesEndPoint.GenerateImageAsync("A house riding a velociraptor", 1, ImageSize.Small);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -25,15 +26,15 @@ public void Test_1_GenerateImages()
}

[Test]
public void Test_2_GenerateImageEdits()
public async Task Test_2_GenerateImageEdits()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.ImagesEndPoint);

var imageAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_original.png");
var maskAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_mask.png");

var results = api.ImagesEndPoint.CreateImageEditAsync(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small).Result;
var results = await api.ImagesEndPoint.CreateImageEditAsync(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -45,14 +46,14 @@ public void Test_2_GenerateImageEdits()
}

[Test]
public void Test_3_GenerateImageVariations()
public async Task Test_3_GenerateImageVariations()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.ImagesEndPoint);

var imageAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_original.png");

var results = api.ImagesEndPoint.CreateImageVariationAsync(imageAssetPath, 1, ImageSize.Small).Result;
var results = await api.ImagesEndPoint.CreateImageVariationAsync(imageAssetPath, 1, ImageSize.Small);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand Down
7 changes: 4 additions & 3 deletions OpenAI-DotNet-Tests/TestFixture_05_Embeddings.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using NUnit.Framework;
using System.Threading.Tasks;
using NUnit.Framework;

namespace OpenAI.Tests
{
internal class TestFixture_05_Embeddings
{
[Test]
public void Test_1_CreateEmbedding()
public async Task Test_1_CreateEmbedding()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.EmbeddingsEndpoint);
var result = api.EmbeddingsEndpoint.CreateEmbeddingAsync("The food was delicious and the waiter...").Result;
var result = await api.EmbeddingsEndpoint.CreateEmbeddingAsync("The food was delicious and the waiter...");
Assert.IsNotNull(result);
Assert.IsNotEmpty(result.Data);
}
Expand Down
11 changes: 5 additions & 6 deletions OpenAI-DotNet/AuthInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Security.Authentication;
using System.Security.Authentication;
using System.Text.Json.Serialization;

namespace OpenAI
{
[Serializable]
internal class AuthInfo
{
[JsonConstructor]
public AuthInfo(string apiKey, string organization = null)
{
if (!apiKey.Contains("sk-"))
Expand All @@ -28,9 +27,9 @@ public AuthInfo(string apiKey, string organization = null)
}

[JsonPropertyName("apiKey")]
public string ApiKey { get; set; }
public string ApiKey { get; }

[JsonPropertyName("organization")]
public string Organization { get; set; }
public string Organization { get; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/BaseEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public abstract class BaseEndPoint
/// <returns>The completed basic url for the endpoint.</returns>
protected abstract string GetEndpoint();
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/BaseResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ public abstract class BaseResponse
[JsonIgnore]
public string RequestId { get; internal set; }
}
}
}
6 changes: 3 additions & 3 deletions OpenAI-DotNet/Completions/Choice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace OpenAI
namespace OpenAI.Completions
{
/// <summary>
/// Represents a completion choice returned by the <see cref="CompletionsEndpoint"/>.
Expand Down Expand Up @@ -34,8 +34,8 @@ public sealed class Choice
/// <summary>
/// Gets the main text of this completion
/// </summary>
public override string ToString() => $"{Index}|{FinishReason}|{Text}";
public override string ToString() => Text;

public static implicit operator string(Choice choice) => choice.Text;
}
}
}
4 changes: 2 additions & 2 deletions OpenAI-DotNet/Completions/CompletionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text.Json.Serialization;

namespace OpenAI
namespace OpenAI.Completions
{
/// <summary>
/// Represents a request to the <see cref="CompletionsEndpoint"/>. Mostly matches the parameters in
Expand All @@ -20,7 +20,7 @@ public sealed class CompletionRequest
/// If you are requesting more than one prompt, specify them as an array of strings.
/// </summary>
[JsonPropertyName("prompt")]
public string[] Prompts { get; set; } = new string[0];
public string[] Prompts { get; set; } = Array.Empty<string>();

/// <summary>
/// For convenience, if you are only requesting a single prompt, set it here
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Completions/CompletionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace OpenAI
namespace OpenAI.Completions
{
/// <summary>
/// Represents a result from calling the <see cref="CompletionsEndpoint"/>.
Expand Down
9 changes: 4 additions & 5 deletions OpenAI-DotNet/Completions/CompletionsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text.Json;
using System.Threading.Tasks;

namespace OpenAI
namespace OpenAI.Completions
{
/// <summary>
/// Text generation is the core function of the API. You give the API a prompt, and it generates a completion.
Expand All @@ -16,7 +16,7 @@ namespace OpenAI
/// (see the prompt library for inspiration).
/// <see href="https://beta.openai.com/docs/api-reference/completions"/>
/// </summary>
public class CompletionsEndpoint : BaseEndPoint
public sealed class CompletionsEndpoint : BaseEndPoint
{
/// <inheritdoc />
internal CompletionsEndpoint(OpenAIClient api) : base(api) { }
Expand Down Expand Up @@ -71,8 +71,7 @@ public async Task<CompletionResult> CreateCompletionAsync(
int? logProbabilities = null,
bool? echo = null,
string[] stopSequences = null,
Model model = null
)
Model model = null)
{
var request = new CompletionRequest(
model ?? Api.DefaultModel,
Expand Down Expand Up @@ -354,7 +353,7 @@ public async IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(

private CompletionResult DeserializeResult(HttpResponseMessage response, string result)
{
var completionResult = JsonSerializer.Deserialize<CompletionResult>(result);
var completionResult = JsonSerializer.Deserialize<CompletionResult>(result, Api.JsonSerializationOptions);

if (completionResult?.Completions == null || completionResult.Completions.Count == 0)
{
Expand Down
4 changes: 2 additions & 2 deletions OpenAI-DotNet/Completions/Logprobs.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace OpenAI
namespace OpenAI.Completions
{
/// <summary>
/// Object belonging to a <see cref="Choice"/>
Expand All @@ -20,4 +20,4 @@ public sealed class Logprobs
[JsonPropertyName("text_offset")]
public List<int> TextOffsets { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Edits/Choice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public sealed class Choice

public static implicit operator string(Choice choice) => choice.Text;
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Edits/EditRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ public EditRequest(
[JsonPropertyName("top_p")]
public double? TopP { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Edits/EditResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public override string ToString()
: "Edit result has no valid output";
}
}
}
}
1 change: 1 addition & 0 deletions OpenAI-DotNet/Edits/EditsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using OpenAI.Models;

namespace OpenAI.Edits
{
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Embeddings/Datum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public sealed class Datum
[JsonPropertyName("index")]
public int Index { get; set; }
}
}
}
6 changes: 3 additions & 3 deletions OpenAI-DotNet/Embeddings/EmbeddingsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using OpenAI.Models;
using System;
using System.Text.Json.Serialization;
using OpenAI.Models;

namespace OpenAI.Embeddings
{
Expand Down Expand Up @@ -43,4 +43,4 @@ public EmbeddingsRequest(string input, Model model = null, string user = null)
[JsonPropertyName("user")]
public string User { get; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Embeddings/EmbeddingsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public sealed class EmbeddingsResponse : BaseResponse
[JsonPropertyName("usage")]
public Usage Usage { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ string message
[JsonPropertyName("message")]
public string Message { get; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Files/FileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public sealed class FileData

public static implicit operator string(FileData fileData) => fileData.Id;
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Files/FileUploadRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ public void Dispose()
GC.SuppressFinalize(this);
}
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<IReadOnlyList<FileData>> ListFilesAsync()

if (response.IsSuccessStatusCode)
{
return JsonSerializer.Deserialize<FilesList>(resultAsString)?.Data;
return JsonSerializer.Deserialize<FilesList>(resultAsString, Api.JsonSerializationOptions)?.Data;
}

throw new HttpRequestException($"{nameof(ListFilesAsync)} Failed! HTTP status code: {response.StatusCode}.");
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/FineTuning/CreateFineTuneJobRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ public CreateFineTuneJobRequest(
[JsonPropertyName("suffix")]
public string Suffix { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/FineTuning/FineTuneJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public sealed class FineTuneJob
[JsonIgnore]
public DateTime UpdatedAt => DateTimeOffset.FromUnixTimeSeconds(UpdatedAtUnixTime).DateTime;
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/FineTuning/FineTuneJobResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ public static implicit operator FineTuneJob(FineTuneJobResponse jobResponse)
[JsonIgnore]
public DateTime UpdatedAt => DateTimeOffset.FromUnixTimeSeconds(UpdatedAtUnixTime).DateTime;
}
}
}
Loading

0 comments on commit fce110d

Please sign in to comment.