Skip to content

Commit 00debca

Browse files
authored
Merge pull request #37 from betalgo/dev
2 parents 81b4aa3 + d366495 commit 00debca

26 files changed

+273
-57
lines changed

OpenAI.Playground/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
var sdk = serviceProvider.GetRequiredService<IOpenAIService>();
2626

2727
//await ModelTestHelper.FetchModelsTest(sdk);
28+
await EditTestHelper.RunSimpleEditCreateTest(sdk);
2829
//await ImageTestHelper.RunSimpleCreateImageTest(sdk);
2930
//await ImageTestHelper.RunSimpleCreateImageEditTest(sdk);
30-
await ImageTestHelper.RunSimpleCreateImageVariationTest(sdk);
31+
//await ImageTestHelper.RunSimpleCreateImageVariationTest(sdk);
3132
//await ModerationTestHelper.CreateModerationTest(sdk);
3233
//await CompletionTestHelper.RunSimpleCompletionTest(sdk);
3334
//await EmbeddingTestHelper.RunSimpleEmbeddingTest(sdk);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using OpenAI.GPT3.Interfaces;
2+
using OpenAI.GPT3.ObjectModels;
3+
using OpenAI.GPT3.ObjectModels.RequestModels;
4+
5+
namespace OpenAI.Playground.TestHelpers
6+
{
7+
internal static class EditTestHelper
8+
{
9+
public static async Task RunSimpleEditCreateTest(IOpenAIService sdk)
10+
{
11+
ConsoleExtensions.WriteLine("Edit Create Testing is starting:", ConsoleColor.Cyan);
12+
13+
try
14+
{
15+
ConsoleExtensions.WriteLine("Edit Create Test:", ConsoleColor.DarkCyan);
16+
var completionResult = await sdk.Edit.CreateEdit(new EditCreateRequest()
17+
{
18+
Input = "What day of the wek is it?",
19+
Instruction = "Fix the spelling mistakes"
20+
}, Models.TextEditDavinciV1);
21+
22+
if (completionResult.Successful)
23+
{
24+
Console.WriteLine(completionResult.Choices.FirstOrDefault());
25+
}
26+
else
27+
{
28+
if (completionResult.Error == null)
29+
{
30+
throw new Exception("Unknown Error");
31+
}
32+
33+
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
34+
}
35+
}
36+
catch (Exception e)
37+
{
38+
Console.WriteLine(e);
39+
throw;
40+
}
41+
}
42+
}
43+
}

OpenAI.Playground/TestHelpers/ImageTestHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ public static async Task RunSimpleCreateImageTest(IOpenAIService sdk)
4242
Console.WriteLine(e);
4343
throw;
4444
}
45-
}
46-
45+
}
46+
4747
public static async Task RunSimpleCreateImageEditTest(IOpenAIService sdk)
4848
{
4949
ConsoleExtensions.WriteLine("Image Edit Create Testing is starting:", ConsoleColor.Cyan);
5050
const string maskFileName = "image_edit_mask.png";
5151
const string originalFileName = "image_edit_original.png";
52-
52+
5353
// Images should be in png format with ARGB. I got help from this website to generate sample mask
5454
// https://www.online-image-editor.com/
5555
var maskFile = await File.ReadAllBytesAsync($"SampleData/{maskFileName}");
5656
var originalFile = await File.ReadAllBytesAsync($"SampleData/{originalFileName}");
57-
57+
5858
try
5959
{
6060
ConsoleExtensions.WriteLine("Image Edit Create Test:", ConsoleColor.DarkCyan);
@@ -93,13 +93,13 @@ public static async Task RunSimpleCreateImageEditTest(IOpenAIService sdk)
9393
}
9494
}
9595

96-
public static async Task RunSimpleCreateImageVariationTest(IOpenAIService sdk)
96+
public static async Task RunSimpleCreateImageVariationTest(IOpenAIService sdk)
9797
{
9898
ConsoleExtensions.WriteLine("Image Variation Create Testing is starting:", ConsoleColor.Cyan);
9999
const string originalFileName = "image_edit_original.png";
100-
100+
101101
var originalFile = await File.ReadAllBytesAsync($"SampleData/{originalFileName}");
102-
102+
103103
try
104104
{
105105
ConsoleExtensions.WriteLine("Image Variation Create Test:", ConsoleColor.DarkCyan);

OpenAI.SDK/Interfaces/IEditService.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using OpenAI.GPT3.ObjectModels;
2+
using OpenAI.GPT3.ObjectModels.RequestModels;
3+
using OpenAI.GPT3.ObjectModels.ResponseModels;
4+
5+
namespace OpenAI.GPT3.Interfaces;
6+
7+
/// <summary>
8+
/// Given a prompt and an instruction, the model will return an edited version of the prompt.
9+
/// </summary>
10+
public interface IEditService
11+
{
12+
/// <summary>
13+
/// Creates a new edit for the provided input, instruction, and parameters
14+
/// </summary>
15+
/// <param name="editCreate"></param>
16+
/// <param name="engineId">The ID of the engine to use for this request</param>
17+
/// <returns></returns>
18+
Task<EditCreateResponse> CreateEdit(EditCreateRequest editCreate, string? engineId = null);
19+
20+
/// <summary>
21+
/// Creates a new edit for the provided input, instruction, and parameters
22+
/// </summary>
23+
/// <param name="editCreate"></param>
24+
/// <param name="engineId">The ID of the engine to use for this request</param>
25+
/// <returns></returns>
26+
Task<EditCreateResponse> Edit(EditCreateRequest editCreate, Models.Model engineId)
27+
{
28+
return CreateEdit(editCreate, engineId.EnumToString());
29+
}
30+
}

OpenAI.SDK/Interfaces/IOpenAIService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ public interface IOpenAIService
3232
/// <summary>
3333
/// Given a prompt and/or an input image, the model will generate a new image.
3434
/// </summary>
35-
public IImageService Image { get; }
36-
35+
public IImageService Image { get; }
36+
37+
/// <summary>
38+
/// Creates a new edit for the provided input, instruction, and parameters
39+
/// </summary>
40+
public IEditService Edit { get; }
41+
3742

3843
/// <summary>
3944
/// Set default engine

OpenAI.SDK/Managers/OpenAIEdits.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using OpenAI.GPT3.Extensions;
2+
using OpenAI.GPT3.Interfaces;
3+
using OpenAI.GPT3.ObjectModels.RequestModels;
4+
using OpenAI.GPT3.ObjectModels.ResponseModels;
5+
6+
namespace OpenAI.GPT3.Managers;
7+
8+
public partial class OpenAIService : IEditService
9+
{
10+
public async Task<EditCreateResponse> CreateEdit(EditCreateRequest editCreate, string? engineId = null)
11+
{
12+
if (editCreate.Model != null && engineId != null)
13+
{
14+
throw new ArgumentException("You cannot specify both a model and an engineId");
15+
}
16+
else if (editCreate.Model == null && engineId != null)
17+
{
18+
editCreate.Model = ProcessEngineId(engineId);
19+
}
20+
return await _httpClient.PostAndReadAsAsync<EditCreateResponse>(_endpointProvider.EditCreate(), editCreate);
21+
}
22+
}

OpenAI.SDK/Managers/OpenAIImage.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public async Task<ImageCreateResponse> CreateImage(ImageCreateRequest imageCreat
1818
}
1919

2020
/// <summary>
21-
/// Creates an edited or extended image given an original image and a prompt.
21+
/// Creates an edited or extended image given an original image and a prompt.
2222
/// </summary>
2323
/// <param name="imageEditCreateRequest"></param>
2424
/// <returns></returns>
@@ -27,9 +27,9 @@ public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest im
2727
var multipartContent = new MultipartFormDataContent();
2828
if (imageEditCreateRequest.User != null) multipartContent.Add(new StringContent(imageEditCreateRequest.User), "user");
2929
if (imageEditCreateRequest.ResponseFormat != null) multipartContent.Add(new StringContent(imageEditCreateRequest.ResponseFormat), "response_format");
30-
if (imageEditCreateRequest.Size!= null) multipartContent.Add(new StringContent(imageEditCreateRequest.Size), "size");
31-
if (imageEditCreateRequest.N!= null) multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
32-
30+
if (imageEditCreateRequest.Size != null) multipartContent.Add(new StringContent(imageEditCreateRequest.Size), "size");
31+
if (imageEditCreateRequest.N != null) multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
32+
3333
multipartContent.Add(new StringContent(imageEditCreateRequest.Prompt), "prompt");
3434
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);
3535
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Mask), "mask", imageEditCreateRequest.MaskName);
@@ -38,7 +38,7 @@ public async Task<ImageCreateResponse> CreateImageEdit(ImageEditCreateRequest im
3838
}
3939

4040
/// <summary>
41-
/// Creates a variation of a given image.
41+
/// Creates a variation of a given image.
4242
/// </summary>
4343
/// <param name="imageEditCreateRequest"></param>
4444
/// <returns></returns>
@@ -47,9 +47,9 @@ public async Task<ImageCreateResponse> CreateImageVariation(ImageVariationCreate
4747
var multipartContent = new MultipartFormDataContent();
4848
if (imageEditCreateRequest.User != null) multipartContent.Add(new StringContent(imageEditCreateRequest.User), "user");
4949
if (imageEditCreateRequest.ResponseFormat != null) multipartContent.Add(new StringContent(imageEditCreateRequest.ResponseFormat), "response_format");
50-
if (imageEditCreateRequest.Size!= null) multipartContent.Add(new StringContent(imageEditCreateRequest.Size), "size");
51-
if (imageEditCreateRequest.N!= null) multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
52-
50+
if (imageEditCreateRequest.Size != null) multipartContent.Add(new StringContent(imageEditCreateRequest.Size), "size");
51+
if (imageEditCreateRequest.N != null) multipartContent.Add(new StringContent(imageEditCreateRequest.N.ToString()!), "n");
52+
5353
multipartContent.Add(new ByteArrayContent(imageEditCreateRequest.Image), "image", imageEditCreateRequest.ImageName);
5454

5555
return await _httpClient.PostFileAndReadAsAsync<ImageCreateResponse>(_endpointProvider.ImageVariationCreate(), multipartContent);

OpenAI.SDK/Managers/OpenAIService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public OpenAIService(OpenAiOptions settings, HttpClient? httpClient = null)
5656
public IFineTuneService FineTunes => this;
5757
public IModerationService Moderation => this;
5858
public IImageService Image => this;
59+
public IEditService Edit => this;
5960

6061
public void SetDefaultEngineId(string engineId)
6162
{

OpenAI.SDK/ObjectModels/Models.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum Model
2424
TextDavinciV1,
2525

2626
TextDavinciV2,
27+
TextDavinciV3,
2728

2829
CurieInstructBeta,
2930
DavinciInstructBeta,
@@ -45,6 +46,9 @@ public enum Model
4546
TextSearchCurieQueryV1,
4647
TextSearchDavinciQueryV1,
4748

49+
TextEditDavinciV1,
50+
CodeEditDavinciV1,
51+
4852
CodeSearchAdaCodeV1,
4953
CodeSearchBabbageCodeV1,
5054

@@ -67,7 +71,9 @@ public enum Subject
6771
TextSearchQuery,
6872
CodeSearchCode,
6973
CodeSearchText,
70-
Code
74+
Code,
75+
CodeEdit,
76+
Edit
7177
}
7278

7379
public static string Ada => "ada";
@@ -80,6 +86,7 @@ public enum Subject
8086

8187
public static string TextDavinciV1 => ModelNameBuilder(BaseEngine.Davinci, Subject.Text, "001");
8288
public static string TextDavinciV2 => ModelNameBuilder(BaseEngine.Davinci, Subject.Text, "002");
89+
public static string TextDavinciV3 => ModelNameBuilder(BaseEngine.Davinci, Subject.Text, "003");
8390
public static string TextAdaV1 => ModelNameBuilder(BaseEngine.Ada, Subject.Text, "001");
8491
public static string TextBabbageV1 => ModelNameBuilder(BaseEngine.Babbage, Subject.Text, "001");
8592
public static string TextCurieV1 => ModelNameBuilder(BaseEngine.Curie, Subject.Text, "001");
@@ -104,6 +111,9 @@ public enum Subject
104111
public static string TextSearchCurieQueryV1 => ModelNameBuilder(BaseEngine.Curie, Subject.TextSearchQuery, "001");
105112
public static string TextSearchDavinciQueryV1 => ModelNameBuilder(BaseEngine.Davinci, Subject.TextSearchQuery, "001");
106113

114+
public static string TextEditDavinciV1 => ModelNameBuilder(BaseEngine.Davinci, Subject.Edit, "001");
115+
public static string CodeEditDavinciV1 => ModelNameBuilder(BaseEngine.Davinci, Subject.CodeEdit, "001");
116+
107117
public static string CodeSearchAdaCodeV1 => ModelNameBuilder(BaseEngine.Ada, Subject.CodeSearchCode, "001");
108118
public static string CodeSearchBabbageCodeV1 => ModelNameBuilder(BaseEngine.Babbage, Subject.CodeSearchCode, "001");
109119
public static string CodeSearchAdaTextV1 => ModelNameBuilder(BaseEngine.Ada, Subject.CodeSearchText, "001");
@@ -147,6 +157,7 @@ public static string EnumToString(this Model engine)
147157
Model.DavinciInstructBeta => DavinciInstructBeta,
148158
Model.TextDavinciV1 => TextDavinciV1,
149159
Model.TextDavinciV2 => TextDavinciV2,
160+
Model.TextDavinciV3 => TextDavinciV3,
150161
Model.TextAdaV1 => TextAdaV1,
151162
Model.TextBabbageV1 => TextBabbageV1,
152163
Model.TextCurieV1 => TextCurieV1,
@@ -170,6 +181,8 @@ public static string EnumToString(this Model engine)
170181
Model.CodeSearchBabbageCodeV1 => CodeSearchBabbageCodeV1,
171182
Model.CodeSearchAdaTextV1 => CodeSearchAdaTextV1,
172183
Model.CodeSearchBabbageTextV1 => CodeSearchBabbageTextV1,
184+
Model.TextEditDavinciV1 => TextEditDavinciV1,
185+
Model.CodeEditDavinciV1 => CodeEditDavinciV1,
173186
_ => throw new ArgumentOutOfRangeException(nameof(engine), engine, null)
174187
};
175188
}
@@ -201,6 +214,8 @@ public static string EnumToString(this Subject subject, string baseEngine)
201214
Subject.CodeSearchCode => "code-search-{0}-code",
202215
Subject.CodeSearchText => "code-search-{0}-text",
203216
Subject.Code => "code-{0}",
217+
Subject.CodeEdit => "code-{0}-edit",
218+
Subject.Edit => "text-{0}-edit",
204219
_ => throw new ArgumentOutOfRangeException(nameof(subject), subject, null)
205220
}, baseEngine);
206221
}

OpenAI.SDK/ObjectModels/RequestModels/CompletionCreateRequest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,6 @@ public record CompletionCreateRequest : IModelValidate, IOpenAiModels.ITemperatu
120120
[JsonPropertyName("logit_bias")]
121121
public object? LogitBias { get; set; }
122122

123-
/// <summary>
124-
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
125-
/// </summary>
126-
[JsonPropertyName("user")]
127-
public string? User { get; set; }
128-
129123
/// <summary>
130124
/// Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. For example, if
131125
/// logprobs is 5, the API will return a list of the 5 most likely tokens. The API will always return the logprob of
@@ -153,5 +147,11 @@ public IEnumerable<ValidationResult> Validate()
153147
/// <see cref="https://beta.openai.com/docs/api-reference/completions/create#completions/create-temperature" />
154148
[JsonPropertyName("temperature")]
155149
public float? Temperature { get; set; }
150+
151+
/// <summary>
152+
/// A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
153+
/// </summary>
154+
[JsonPropertyName("user")]
155+
public string? User { get; set; }
156156
}
157157
}

0 commit comments

Comments
 (0)