diff --git a/OpenAI.Playground/Program.cs b/OpenAI.Playground/Program.cs index d4079d46..9026fe43 100644 --- a/OpenAI.Playground/Program.cs +++ b/OpenAI.Playground/Program.cs @@ -42,10 +42,10 @@ // | / \ / \ | \ /) | ( \ /o\ / ) | (\ / | / \ / \ | // |-----------------------------------------------------------------------| -//await ChatCompletionTestHelper.RunSimpleChatCompletionTest(sdk); +await ChatCompletionTestHelper.RunSimpleChatCompletionTest(sdk); //await ChatCompletionTestHelper.RunSimpleCompletionStreamTest(sdk); //await ChatCompletionTestHelper.RunChatFunctionCallTest(sdk); -await FineTuningJobTestHelper.RunCaseStudyIsTheModelMakingUntrueStatements(sdk); +//await FineTuningJobTestHelper.RunCaseStudyIsTheModelMakingUntrueStatements(sdk); // Whisper //await AudioTestHelper.RunSimpleAudioCreateTranscriptionTest(sdk); //await AudioTestHelper.RunSimpleAudioCreateTranslationTest(sdk); @@ -55,7 +55,7 @@ //await ImageTestHelper.RunSimpleCreateImageTest(sdk); //await ImageTestHelper.RunSimpleCreateImageEditTest(sdk); //await ImageTestHelper.RunSimpleCreateImageVariationTest(sdk); -//await ModerationTestHelper.CreateModerationTest(sdk); +await ModerationTestHelper.CreateModerationTest(sdk); //await CompletionTestHelper.RunSimpleCompletionTest(sdk); //await CompletionTestHelper.RunSimpleCompletionTest2(sdk); //await CompletionTestHelper.RunSimpleCompletionTest3(sdk); diff --git a/OpenAI.SDK/EndpointProviders/AzureOpenAiEndpointProvider.cs b/OpenAI.SDK/EndpointProviders/AzureOpenAiEndpointProvider.cs index 44ffff64..012aaf27 100644 --- a/OpenAI.SDK/EndpointProviders/AzureOpenAiEndpointProvider.cs +++ b/OpenAI.SDK/EndpointProviders/AzureOpenAiEndpointProvider.cs @@ -17,7 +17,7 @@ public AzureOpenAiEndpointProvider(string apiVersion, string deploymentId) _deploymentId = deploymentId; } - private string Prefix => $"/{ApiPrefix}/{DeploymentsPrefix}/{WebUtility.UrlEncode(_deploymentId)}"; + private string Prefix => $"{ApiPrefix}/{DeploymentsPrefix}/{WebUtility.UrlEncode(_deploymentId)}"; private string QueryString => $"?api-version={_apiVersion}"; @@ -58,12 +58,12 @@ public string FilesUpload() public string FileRetrieve(string fileId) { - return $"/{Prefix}/files/{fileId}{QueryString}"; + return $"{Prefix}/files/{fileId}{QueryString}"; } public string FileRetrieveContent(string fileId) { - return $"/{Prefix}/files/{fileId}/content{QueryString}"; + return $"{Prefix}/files/{fileId}/content{QueryString}"; } public string FineTuneCreate() @@ -103,7 +103,7 @@ public string FineTuningJobCreate() public string FineTuningJobList(FineTuningJobListRequest? fineTuningJobListRequest) { - var url = $"/{Prefix}/fine_tuning/jobs"; + var url = $"{Prefix}/fine_tuning/jobs"; if (fineTuningJobListRequest != null) { var queryParams = new List(); diff --git a/OpenAI.SDK/EndpointProviders/OpenAiEndpointProvider.cs b/OpenAI.SDK/EndpointProviders/OpenAiEndpointProvider.cs index d1dfc682..3a85aec3 100644 --- a/OpenAI.SDK/EndpointProviders/OpenAiEndpointProvider.cs +++ b/OpenAI.SDK/EndpointProviders/OpenAiEndpointProvider.cs @@ -14,42 +14,42 @@ public OpenAiEndpointProvider(string apiVersion) public string ModelRetrieve(string model) { - return $"/{_apiVersion}/models/{model}"; + return $"{_apiVersion}/models/{model}"; } public string FileDelete(string fileId) { - return $"/{_apiVersion}/files/{fileId}"; + return $"{_apiVersion}/files/{fileId}"; } public string CompletionCreate() { - return $"/{_apiVersion}/completions"; + return $"{_apiVersion}/completions"; } public string ChatCompletionCreate() { - return $"/{_apiVersion}/chat/completions"; + return $"{_apiVersion}/chat/completions"; } public string AudioCreateTranscription() { - return $"/{_apiVersion}/audio/transcriptions"; + return $"{_apiVersion}/audio/transcriptions"; } public string AudioCreateTranslation() { - return $"/{_apiVersion}/audio/translations"; + return $"{_apiVersion}/audio/translations"; } public string EditCreate() { - return $"/{_apiVersion}/edits"; + return $"{_apiVersion}/edits"; } public string ModelsList() { - return $"/{_apiVersion}/models"; + return $"{_apiVersion}/models"; } public string FilesList() @@ -64,52 +64,52 @@ public string FilesUpload() public string FileRetrieve(string fileId) { - return $"/{_apiVersion}/files/{fileId}"; + return $"{_apiVersion}/files/{fileId}"; } public string FileRetrieveContent(string fileId) { - return $"/{_apiVersion}/files/{fileId}/content"; + return $"{_apiVersion}/files/{fileId}/content"; } public string FineTuneCreate() { - return $"/{_apiVersion}/fine-tunes"; + return $"{_apiVersion}/fine-tunes"; } public string FineTuneList() { - return $"/{_apiVersion}/fine-tunes"; + return $"{_apiVersion}/fine-tunes"; } public string FineTuneRetrieve(string fineTuneId) { - return $"/{_apiVersion}/fine-tunes/{fineTuneId}"; + return $"{_apiVersion}/fine-tunes/{fineTuneId}"; } public string FineTuneCancel(string fineTuneId) { - return $"/{_apiVersion}/fine-tunes/{fineTuneId}/cancel"; + return $"{_apiVersion}/fine-tunes/{fineTuneId}/cancel"; } public string FineTuneListEvents(string fineTuneId) { - return $"/{_apiVersion}/fine-tunes/{fineTuneId}/events"; + return $"{_apiVersion}/fine-tunes/{fineTuneId}/events"; } public string FineTuneDelete(string fineTuneId) { - return $"/{_apiVersion}/models/{fineTuneId}"; + return $"{_apiVersion}/models/{fineTuneId}"; } public string FineTuningJobCreate() { - return $"/{_apiVersion}/fine_tuning/jobs"; + return $"{_apiVersion}/fine_tuning/jobs"; } public string FineTuningJobList(FineTuningJobListRequest? fineTuningJobListRequest) { - var url = $"/{_apiVersion}/fine_tuning/jobs"; + var url = $"{_apiVersion}/fine_tuning/jobs"; if (fineTuningJobListRequest != null) { var queryParams = new List(); @@ -126,51 +126,51 @@ public string FineTuningJobList(FineTuningJobListRequest? fineTuningJobListReque public string FineTuningJobRetrieve(string fineTuningJobId) { - return $"/{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}"; + return $"{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}"; } public string FineTuningJobCancel(string fineTuningJobId) { - return $"/{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}/cancel"; + return $"{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}/cancel"; } public string FineTuningJobListEvents(string fineTuningJobId) { - return $"/{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}/events"; + return $"{_apiVersion}/fine_tuning/jobs/{fineTuningJobId}/events"; } public string ModelsDelete(string modelId) { - return $"/{_apiVersion}/models/{modelId}"; + return $"{_apiVersion}/models/{modelId}"; } public string EmbeddingCreate() { - return $"/{_apiVersion}/embeddings"; + return $"{_apiVersion}/embeddings"; } public string ModerationCreate() { - return $"/{_apiVersion}/moderations"; + return $"{_apiVersion}/moderations"; } public string ImageCreate() { - return $"/{_apiVersion}/images/generations"; + return $"{_apiVersion}/images/generations"; } public string ImageEditCreate() { - return $"/{_apiVersion}/images/edits"; + return $"{_apiVersion}/images/edits"; } public string ImageVariationCreate() { - return $"/{_apiVersion}/images/variations"; + return $"{_apiVersion}/images/variations"; } private string Files() { - return $"/{_apiVersion}/files"; + return $"{_apiVersion}/files"; } } \ No newline at end of file diff --git a/OpenAI.SDK/Extensions/HttpclientExtensions.cs b/OpenAI.SDK/Extensions/HttpclientExtensions.cs index ecaa5379..d1e7aa26 100644 --- a/OpenAI.SDK/Extensions/HttpclientExtensions.cs +++ b/OpenAI.SDK/Extensions/HttpclientExtensions.cs @@ -13,6 +13,7 @@ public static async Task PostAndReadAsAsync(this HttpClien { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } @@ -47,6 +48,7 @@ private static HttpResponseMessage SendRequestPreNet6(HttpClient client, HttpReq { var responseTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); var response = responseTask.GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); return response; } @@ -62,18 +64,21 @@ private static HttpRequestMessage CreatePostEventStreamRequest(string uri, HttpC public static async Task PostFileAndReadAsAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) { var response = await client.PostAsync(uri, content, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } public static async Task PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) { var response = await client.PostAsync(uri, content, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(cancellationToken) ?? throw new InvalidOperationException(); } public static async Task DeleteAndReadAsAsync(this HttpClient client, string uri, CancellationToken cancellationToken = default) { var response = await client.DeleteAsync(uri, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new InvalidOperationException(); } @@ -99,6 +104,7 @@ public static async Task ReadAsByteArrayAsync(this HttpContent content, public static async Task GetStreamAsync(this HttpClient client, string requestUri, CancellationToken cancellationToken) { var response = await client.GetAsync(requestUri, cancellationToken); + response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStreamAsync(cancellationToken); } diff --git a/OpenAI.SDK/ObjectModels/ResponseModels/CreateModerationResponse.cs b/OpenAI.SDK/ObjectModels/ResponseModels/CreateModerationResponse.cs index 3cbbd975..d997dca1 100644 --- a/OpenAI.SDK/ObjectModels/ResponseModels/CreateModerationResponse.cs +++ b/OpenAI.SDK/ObjectModels/ResponseModels/CreateModerationResponse.cs @@ -27,7 +27,16 @@ public record Categories [JsonPropertyName("hate/threatening")] public bool HateThreatening { get; set; } + [JsonPropertyName("harassment")] public bool Harassment { get; set; } + + [JsonPropertyName("harassment/threatening")] + public bool HarassmentThreatening { get; set; } + [JsonPropertyName("self-harm")] public bool SelfHarm { get; set; } + [JsonPropertyName("self-harm/intent")] public bool SelfHarmIntent { get; set; } + + [JsonPropertyName("self-harm/instructions")] + public bool SelfHarmInstructions { get; set; } [JsonPropertyName("sexual")] public bool Sexual { get; set; } @@ -44,7 +53,16 @@ public record CategoryScores [JsonPropertyName("hate/threatening")] public float HateThreatening { get; set; } + [JsonPropertyName("harassment")] public float Harassment { get; set; } + + [JsonPropertyName("harassment/threatening")] + public float HarassmentThreatening { get; set; } + [JsonPropertyName("self-harm")] public float SelfHarm { get; set; } + [JsonPropertyName("self-harm/intent")] public float SelfHarmIntent { get; set; } + + [JsonPropertyName("self-harm/instructions")] + public float SelfHarmInstructions { get; set; } [JsonPropertyName("sexual")] public float Sexual { get; set; } diff --git a/OpenAI.SDK/OpenAI.csproj b/OpenAI.SDK/OpenAI.csproj index 0cd760c9..7d669a1c 100644 --- a/OpenAI.SDK/OpenAI.csproj +++ b/OpenAI.SDK/OpenAI.csproj @@ -11,7 +11,7 @@ OpenAI-Betalgo.png true OpenAI SDK by Betalgo - 7.2.0 + 7.3.0 Tolga Kayhan, Betalgo Betalgo Up Ltd. OpenAI ChatGPT, Whisper, GPT-4 and DALL·E dotnet SDK diff --git a/OpenAI.Utilities/OpenAI.Utilities.csproj b/OpenAI.Utilities/OpenAI.Utilities.csproj index aa3a43d1..6d2648d7 100644 --- a/OpenAI.Utilities/OpenAI.Utilities.csproj +++ b/OpenAI.Utilities/OpenAI.Utilities.csproj @@ -41,7 +41,7 @@ - + diff --git a/Readme.md b/Readme.md index f7617f30..6f58a40e 100644 --- a/Readme.md +++ b/Readme.md @@ -13,7 +13,8 @@ Dotnet SDK for OpenAI Chat GPT, Whisper, GPT-4 ,GPT-3 and DALL·E #### This library used be to known as `Betalgo.OpenAI.GPT3`, now it has a new package Id `Betalgo.OpenAI`. ## Checkout the wiki page: -https://github.com/betalgo/openai/wiki +https://github.com/betalgo/openai/wiki +or [![Static Badge](https://img.shields.io/badge/API%20Docs-RobiniaDocs-43bc00?logo=readme&logoColor=white)](https://www.robiniadocs.com/d/betalgo-openai/api/OpenAI.ObjectModels.RequestModels.ChatMessage.html) ## Checkout new ***experimantal*** utilities library: [![Betalgo.OpenAI.Utilities](https://img.shields.io/nuget/v/Betalgo.OpenAI.Utilities?style=for-the-badge)](https://www.nuget.org/packages/Betalgo.OpenAI.Utilities/) ``` @@ -223,6 +224,11 @@ I will always be using the latest libraries, and future releases will frequently I am incredibly busy. If I forgot your name, please accept my apologies and let me know so I can add it to the list. ## Changelog +### Version 7.3.0 +- Updated Moderation categories as reported by @dmki. +- **Breaking Changes:** + - Introduced the use of `EnsureStatusCode()` after making requests.Please adjust your code accordingly for handling failure cases. Thanks to @miroljub1995 for reporting. + - Previously, we used to override paths in the base domain, but this behavior has now changed. If you were using `abc.com/mypath` as the base domain, we used to ignore `/mypath`. This will no longer be the case, and the code will now respect `/mypath`. Thanks to @Hzw576816 for reporting. ### 7.2.0 - Added Chatgpt Finetununig support thanks to @aghimir3 - Default Azure Openai version increased thanks to @mac8005 @@ -234,23 +240,3 @@ I am incredibly busy. If I forgot your name, please accept my apologies and let - This release was a bit late and took longer than expected due to a couple of reasons. The future was quite big, and I couldn't cover all possibilities. However, I believe I have covered most of the function definitions (with some details missing). Additionally, I added an option to build it manually. If you don't know what I mean, you don't need to worry. I plan to cover the rest of the function definition in the next release. Until then, you can discover this by playing in the playground or in the source code. This version also support using other libraries to export your function definition. - We now have support for functions! Big cheers to @rzubek for completing most of this feature. - Additionally, we have made bug fixes and improvements. Thanks to @choshinyoung, @yt3trees, @WeihanLi, @N0ker, and all the bug reporters. (Apologies if I missed any names. Please let me know if I missed your name and you have a commit.) -### 7.1.2-beta -- Bugfix https://github.com/betalgo/openai/pull/302 -- Added support for Function role https://github.com/betalgo/openai/issues/303 -### 7.1.0-beta -- Function Calling: We're releasing this version to bring in a new feature that lets you call functions faster. But remember, this version might not be perfectly stable and we might change it a lot later. A big shout-out to @rzubek for helping us add this feature. Although I liked his work, I didn't have enough time to look into it thoroughly. Still, the tests I did showed it was working, so I decided to add his feature to our code. This lets everyone use it now. Even though I'm busy moving houses and didn't have much time, seeing @rzubek's help made things a lot easier for me. -- Support for New Models: This update also includes support for new models that OpenAI recently launched. I've also changed the naming style to match OpenAI's. Model names will no longer start with 'chat'; instead, they'll start with 'gpt_3_5' and so on. -### 7.0.0 -- The code now supports .NET 7.0. Big cheers to @BroMarduk for making this happen. -- The library now automatically disposes of the Httpclient when it's created by the constructor. This feature is thanks to @BroMarduk. -- New support has been added for using more than one instance at the same time. Check out this [link](https://github.com/betalgo/openai/wiki/Working-with-Multiple-Instances) for more details. Thanks to @remixtedi for bringing this to my attention. -- A lot of small improvements have been done by @BroMarduk. -- **Breaking Changes** 😢 - - I've removed 'GPT3' from the namespace, so you might need to modify some aspects of your project. But don't worry, it's pretty simple! For instance, instead of writing `using OpenAI.GPT3.Interfaces`, you'll now write `using OpenAI.Interfaces`. - - The order of the OpenAI constructor parameters has changed. It now takes 'options' first, then 'httpclient'. - ```csharp - //Before - var openAiService = new OpenAIService(httpClient, options); - //Now - var openAiService = new OpenAIService(options, httpClient); - ```