From c7a1fb4f2c3154be13429ffbbf15ed95394bfa9d Mon Sep 17 00:00:00 2001 From: jefim Date: Fri, 5 Jan 2024 10:57:59 +0200 Subject: [PATCH] Moved options/input properties around --- Frends.OpenAI.CallChatGPT/CHANGELOG.md | 5 ++ .../BasicPropertyTests.cs | 27 +++++++--- .../Definitions/Input.cs | 51 +++---------------- .../Definitions/Options.cs | 45 ++++++++++++++-- .../Frends.OpenAI.CallChatGPT.cs | 16 ++++-- 5 files changed, 84 insertions(+), 60 deletions(-) diff --git a/Frends.OpenAI.CallChatGPT/CHANGELOG.md b/Frends.OpenAI.CallChatGPT/CHANGELOG.md index f53ea60..dab1391 100644 --- a/Frends.OpenAI.CallChatGPT/CHANGELOG.md +++ b/Frends.OpenAI.CallChatGPT/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [1.0.1] - 2024-01-05 +### Changed +- Moved some input parameters to options +- Moved API key from options to input + ## [1.0.0] - 2023-12-28 ### Changed - Initial implementation diff --git a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.Tests/BasicPropertyTests.cs b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.Tests/BasicPropertyTests.cs index 7575856..3019a00 100644 --- a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.Tests/BasicPropertyTests.cs +++ b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.Tests/BasicPropertyTests.cs @@ -43,18 +43,29 @@ public void InputProperties_SetAndGetCorrectly() // Act input.Model = "gpt-3.5-turbo"; input.Messages = messages; - input.MaxTokens = 100; - input.N = 3; - input.Seed = 12345; - input.User = "test-user"; // Assert Assert.AreEqual("gpt-3.5-turbo", input.Model); CollectionAssert.AreEqual(messages, input.Messages); - Assert.AreEqual(100, input.MaxTokens); - Assert.AreEqual(3, input.N); - Assert.AreEqual(12345, input.Seed); - Assert.AreEqual("test-user", input.User); + } + + [Test] + public void OptionsProperties_SetAndGetCorrectly() + { + // Arrange + var options = new Options(); + + // Act + options.MaxTokens = 100; + options.N = 3; + options.Seed = 12345; + options.User = "test-user"; + + // Assert + Assert.AreEqual(100, options.MaxTokens); + Assert.AreEqual(3, options.N); + Assert.AreEqual(12345, options.Seed); + Assert.AreEqual("test-user", options.User); } [Test] diff --git a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Input.cs b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Input.cs index 18858e4..e173d5c 100644 --- a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Input.cs +++ b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Input.cs @@ -17,6 +17,14 @@ public class Input [DefaultValue("gpt-3.5-turbo")] public string Model { get; set; } + /// + /// Chat GPT API key. + /// + /// 1234567qwerty + [DisplayFormat(DataFormatString = "Text")] + [PasswordPropertyText] + public string ApiKey { get; set; } + /// /// Array of messages to send to Chat GPT. /// @@ -27,49 +35,6 @@ public class Input /// ] /// public InputMessage[] Messages { get; set; } - - /// - /// The maximum number of tokens that can be generated in the chat - /// completion. Set to 0 for unlimited tokens. - /// The total length of input tokens and generated tokens is still limited - /// by the model's context length. - /// - /// 100 - [DefaultValue(null)] - public int? MaxTokens { get; set; } - - /// - /// How many chat completion choices to generate for each input message. - /// Note that you will be charged based on the number of generated tokens - /// across all of the choices. Keep n as 1 to minimize costs. - /// - /// - /// This property name comes directly from ChatGPT API, thus is called - /// just N and not somethings else. We want to keep the task as close to - /// the API as possible. - /// - /// 3 - [DefaultValue(1)] - public int N { get; set; } = 1; - - /// - /// If specified, ChatGPT system will make a best effort to sample - /// deterministically, such that repeated requests with the same seed and - /// parameters should return the same result. Determinism is not guaranteed, - /// and you should refer to the system_fingerprint response parameter to - /// monitor changes in the backend. - /// Set to null for non-deterministic results. - /// - /// 12345 - [DefaultValue(null)] - public int? Seed { get; set; } - - /// - /// A unique identifier representing your end-user, which can help OpenAI to - /// monitor and detect abuse. - /// - /// User12345 - public string User { get; set; } } /// diff --git a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Options.cs b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Options.cs index df98233..936d69f 100644 --- a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Options.cs +++ b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Definitions/Options.cs @@ -9,12 +9,47 @@ public class Options { /// - /// Chat GPT API key. + /// The maximum number of tokens that can be generated in the chat + /// completion. Set to 0 for unlimited tokens. + /// The total length of input tokens and generated tokens is still limited + /// by the model's context length. /// - /// 1234567qwerty - [DisplayFormat(DataFormatString = "Text")] - [PasswordPropertyText] - public string ApiKey { get; set; } + /// 100 + [DefaultValue(null)] + public int? MaxTokens { get; set; } + + /// + /// How many chat completion choices to generate for each input message. + /// Note that you will be charged based on the number of generated tokens + /// across all of the choices. Keep n as 1 to minimize costs. + /// + /// + /// This property name comes directly from ChatGPT API, thus is called + /// just N and not somethings else. We want to keep the task as close to + /// the API as possible. + /// + /// 3 + [DefaultValue(1)] + public int N { get; set; } = 1; + + /// + /// If specified, ChatGPT system will make a best effort to sample + /// deterministically, such that repeated requests with the same seed and + /// parameters should return the same result. Determinism is not guaranteed, + /// and you should refer to the system_fingerprint response parameter to + /// monitor changes in the backend. + /// Set to null for non-deterministic results. + /// + /// 12345 + [DefaultValue(null)] + public int? Seed { get; set; } + + /// + /// A unique identifier representing your end-user, which can help OpenAI to + /// monitor and detect abuse. + /// + /// User12345 + public string User { get; set; } /// /// Whether to throw an exception if the API returns an error. Otherwise diff --git a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.cs b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.cs index c6bed5f..dc10a3a 100644 --- a/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.cs +++ b/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT/Frends.OpenAI.CallChatGPT.cs @@ -45,9 +45,17 @@ public static async Task CallChatGPT( [PropertyTab] Options options, CancellationToken cancellationToken) { - using var client = CreateClient(options); + using var client = CreateClient(input); var request = new RestRequest("v1/chat/completions"); - request.AddJsonBody(input); + request.AddJsonBody(new + { + input.Messages, + input.Model, + options.MaxTokens, + options.N, + options.Seed, + options.User, + }); var response = await client.ExecutePostAsync(request, cancellationToken); return response.IsSuccessful @@ -69,9 +77,9 @@ private static Result HandleErrorResponse(Options options, RestResponse