-
-
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 #36 from betalgo/feature/edits
support for Edit API and clean up and bug fixes
- Loading branch information
Showing
26 changed files
with
271 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using OpenAI.GPT3.Interfaces; | ||
using OpenAI.GPT3.ObjectModels; | ||
using OpenAI.GPT3.ObjectModels.RequestModels; | ||
|
||
namespace OpenAI.Playground.TestHelpers | ||
{ | ||
internal static class EditTestHelper | ||
{ | ||
public static async Task RunSimpleEditCreateTest(IOpenAIService sdk) | ||
{ | ||
ConsoleExtensions.WriteLine("Edit Create Testing is starting:", ConsoleColor.Cyan); | ||
|
||
try | ||
{ | ||
ConsoleExtensions.WriteLine("Edit Create Test:", ConsoleColor.DarkCyan); | ||
var completionResult = await sdk.Edit.CreateEdit(new EditCreateRequest() | ||
{ | ||
Input = "What day of the wek is it?", | ||
Instruction = "Fix the spelling mistakes" | ||
}, Models.TextEditDavinciV1); | ||
|
||
if (completionResult.Successful) | ||
{ | ||
Console.WriteLine(completionResult.Choices.FirstOrDefault()); | ||
} | ||
else | ||
{ | ||
if (completionResult.Error == null) | ||
{ | ||
throw new Exception("Unknown Error"); | ||
} | ||
|
||
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}"); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using OpenAI.GPT3.ObjectModels; | ||
using OpenAI.GPT3.ObjectModels.RequestModels; | ||
using OpenAI.GPT3.ObjectModels.ResponseModels; | ||
|
||
namespace OpenAI.GPT3.Interfaces; | ||
|
||
/// <summary> | ||
/// Given a prompt and an instruction, the model will return an edited version of the prompt. | ||
/// </summary> | ||
public interface IEditService | ||
{ | ||
/// <summary> | ||
/// Creates a new edit for the provided input, instruction, and parameters | ||
/// </summary> | ||
/// <param name="editCreate"></param> | ||
/// <param name="engineId">The ID of the engine to use for this request</param> | ||
/// <returns></returns> | ||
Task<EditCreateResponse> CreateEdit(EditCreateRequest editCreate, string? engineId = null); | ||
|
||
/// <summary> | ||
/// Creates a new edit for the provided input, instruction, and parameters | ||
/// </summary> | ||
/// <param name="editCreate"></param> | ||
/// <param name="engineId">The ID of the engine to use for this request</param> | ||
/// <returns></returns> | ||
Task<EditCreateResponse> Edit(EditCreateRequest editCreate, Models.Model engineId) | ||
{ | ||
return CreateEdit(editCreate, engineId.EnumToString()); | ||
} | ||
} |
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,22 @@ | ||
using OpenAI.GPT3.Extensions; | ||
using OpenAI.GPT3.Interfaces; | ||
using OpenAI.GPT3.ObjectModels.RequestModels; | ||
using OpenAI.GPT3.ObjectModels.ResponseModels; | ||
|
||
namespace OpenAI.GPT3.Managers; | ||
|
||
public partial class OpenAIService : IEditService | ||
{ | ||
public async Task<EditCreateResponse> CreateEdit(EditCreateRequest editCreate, string? engineId = null) | ||
{ | ||
if (editCreate.Model != null && engineId != null) | ||
{ | ||
throw new ArgumentException("You cannot specify both a model and an engineId"); | ||
} | ||
else if (editCreate.Model == null && engineId != null) | ||
{ | ||
editCreate.Model = ProcessEngineId(engineId); | ||
} | ||
return await _httpClient.PostAndReadAsAsync<EditCreateResponse>(_endpointProvider.EditCreate(), editCreate); | ||
} | ||
} |
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
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
64 changes: 64 additions & 0 deletions
64
OpenAI.SDK/ObjectModels/RequestModels/EditCreateRequest.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,64 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json.Serialization; | ||
using OpenAI.GPT3.Interfaces; | ||
using OpenAI.GPT3.ObjectModels.SharedModels; | ||
|
||
namespace OpenAI.GPT3.ObjectModels.RequestModels | ||
{ | ||
//TODO add model validation | ||
//TODO check what is string or array for prompt,.. | ||
/// <summary> | ||
/// Create Edit Request Model | ||
/// </summary> | ||
public record EditCreateRequest : IModelValidate, IOpenAiModels.ITemperature, IOpenAiModels.IModel | ||
{ | ||
/// <summary> | ||
/// The input text to use as a starting point for the edit. | ||
/// </summary> | ||
[JsonPropertyName("input")] | ||
public string? Input { get; set; } | ||
|
||
/// <summary> | ||
/// The instruction that tells the model how to edit the prompt. | ||
/// </summary> | ||
[JsonPropertyName("instruction")] | ||
public string Instruction { get; set; } | ||
|
||
/// <summary> | ||
/// Defaults to 1 | ||
/// How many completions to generate for each prompt. | ||
/// Note: Because this parameter generates many completions, it can quickly consume your token quota.Use carefully and | ||
/// ensure that you have reasonable settings for max_tokens and stop. | ||
/// </summary> | ||
[JsonPropertyName("n")] | ||
public int? N { get; set; } | ||
|
||
/// <summary> | ||
/// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the | ||
/// tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are | ||
/// considered. | ||
/// We generally recommend altering this or temperature but not both. | ||
/// </summary> | ||
[JsonPropertyName("top_p")] | ||
public float? TopP { get; set; } | ||
|
||
|
||
[JsonPropertyName("model")] public string? Model { get; set; } | ||
|
||
public IEnumerable<ValidationResult> Validate() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// What | ||
/// <a href="https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277">sampling temperature</a> | ||
/// to use. Higher values means the model will take more risks. Try 0.9 for more creative | ||
/// applications, and 0 (argmax sampling) for ones with a well-defined answer. | ||
/// We generally recommend altering this or top_p but not both. | ||
/// </summary> | ||
/// <see cref="https://beta.openai.com/docs/api-reference/completions/create#completions/create-temperature" /> | ||
[JsonPropertyName("temperature")] | ||
public float? Temperature { get; set; } | ||
} | ||
} |
Oops, something went wrong.