From 6e0ca11395bceb030cd84a4ae5acba66990c7254 Mon Sep 17 00:00:00 2001 From: Roger Date: Wed, 8 Mar 2023 16:33:57 -0800 Subject: [PATCH] Fix ChatMessageRole to be singletons, pull ChatMessage to seperate file --- OpenAI_API/Chat/ChatMessage.cs | 57 ++++++++++++++++++++++++++++++ OpenAI_API/Chat/ChatMessageRole.cs | 10 +++--- OpenAI_API/Chat/ChatResult.cs | 44 ----------------------- OpenAI_Tests/ChatEndpointTests.cs | 1 + 4 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 OpenAI_API/Chat/ChatMessage.cs diff --git a/OpenAI_API/Chat/ChatMessage.cs b/OpenAI_API/Chat/ChatMessage.cs new file mode 100644 index 0000000..0a68243 --- /dev/null +++ b/OpenAI_API/Chat/ChatMessage.cs @@ -0,0 +1,57 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenAI_API.Chat +{ + /// + /// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content" + /// + public class ChatMessage + { + /// + /// Creates an empty , with defaulting to + /// + public ChatMessage() + { + this.Role = ChatMessageRole.User; + } + + /// + /// Constructor for a new Chat Message + /// + /// The role of the message, which can be "system", "assistant" or "user" + /// The text to send in the message + public ChatMessage(ChatMessageRole role, string content) + { + this.Role = role; + this.Content = content; + } + + [JsonProperty("role")] + internal string rawRole { get; set; } + + /// + /// The role of the message, which can be "system", "assistant" or "user" + /// + [JsonIgnore] + public ChatMessageRole Role + { + get + { + return ChatMessageRole.FromString(rawRole); + } + set + { + rawRole = value.ToString(); + } + } + + /// + /// The content of the message + /// + [JsonProperty("content")] + public string Content { get; set; } + } +} diff --git a/OpenAI_API/Chat/ChatMessageRole.cs b/OpenAI_API/Chat/ChatMessageRole.cs index 055d094..3d4ed99 100644 --- a/OpenAI_API/Chat/ChatMessageRole.cs +++ b/OpenAI_API/Chat/ChatMessageRole.cs @@ -12,7 +12,7 @@ namespace OpenAI_API.Chat public class ChatMessageRole : IEquatable { /// - /// Contructor is prvate to force usage of strongly typed values + /// Contructor is private to force usage of strongly typed values /// /// private ChatMessageRole(string value) { Value = value; } @@ -37,20 +37,20 @@ public static ChatMessageRole FromString(string roleName) } } - private string Value { get; set; } + private string Value { get; } /// /// The system message helps set the behavior of the assistant. /// - public static ChatMessageRole System { get { return new ChatMessageRole("system"); } } + public static ChatMessageRole System { get; } = new ChatMessageRole("system"); /// /// The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction. /// - public static ChatMessageRole User { get { return new ChatMessageRole("user"); } } + public static ChatMessageRole User { get; } = new ChatMessageRole("user"); /// /// The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior. /// - public static ChatMessageRole Assistant { get { return new ChatMessageRole("assistant"); } } + public static ChatMessageRole Assistant { get; } = new ChatMessageRole("assistant"); /// /// Gets the string value for this role to pass to the API diff --git a/OpenAI_API/Chat/ChatResult.cs b/OpenAI_API/Chat/ChatResult.cs index 0deb91c..9138d19 100644 --- a/OpenAI_API/Chat/ChatResult.cs +++ b/OpenAI_API/Chat/ChatResult.cs @@ -81,50 +81,6 @@ public override string ToString() } } - /// - /// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content" - /// - public class ChatMessage - { - public ChatMessage() { } - - /// - /// Constructor for a new Chat Message - /// - /// The role of the message, which can be "system", "assistant" or "user" - /// The text to send in the message - public ChatMessage(ChatMessageRole role, string content) - { - this.Role = role; - this.Content = content; - } - - [JsonProperty("role")] - internal string rawRole { get; set; } - - /// - /// The role of the message, which can be "system", "assistant" or "user" - /// - [JsonIgnore] - public ChatMessageRole Role - { - get - { - return ChatMessageRole.FromString(rawRole); - } - set - { - rawRole = value.ToString(); - } - } - - /// - /// The content of the message - /// - [JsonProperty("content")] - public string Content { get; set; } - } - /// /// How many tokens were used in this chat message. /// diff --git a/OpenAI_Tests/ChatEndpointTests.cs b/OpenAI_Tests/ChatEndpointTests.cs index 60714fa..174a7f9 100644 --- a/OpenAI_Tests/ChatEndpointTests.cs +++ b/OpenAI_Tests/ChatEndpointTests.cs @@ -79,6 +79,7 @@ public void SimpleCompletion() Assert.NotZero(results.Choices.Count); Assert.AreEqual(ChatMessageRole.Assistant, results.Choices[0].Message.Role); Assert.That(results.Choices.All(c => c.Message.Role.Equals(ChatMessageRole.Assistant))); + Assert.That(results.Choices.All(c => c.Message.Role == ChatMessageRole.Assistant)); Assert.That(results.Choices.All(c => c.Message.Content.Length > 1)); Assert.IsNotEmpty(results.ToString()); }