Skip to content

Commit

Permalink
Fix ChatMessageRole to be singletons, pull ChatMessage to seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
OkGoDoIt committed Mar 9, 2023
1 parent c81ac58 commit 6e0ca11
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 49 deletions.
57 changes: 57 additions & 0 deletions OpenAI_API/Chat/ChatMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenAI_API.Chat
{
/// <summary>
/// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content"
/// </summary>
public class ChatMessage
{
/// <summary>
/// Creates an empty <see cref="ChatMessage"/>, with <see cref="Role"/> defaulting to <see cref="ChatMessageRole.User"/>
/// </summary>
public ChatMessage()
{
this.Role = ChatMessageRole.User;
}

/// <summary>
/// Constructor for a new Chat Message
/// </summary>
/// <param name="role">The role of the message, which can be "system", "assistant" or "user"</param>
/// <param name="content">The text to send in the message</param>
public ChatMessage(ChatMessageRole role, string content)
{
this.Role = role;
this.Content = content;
}

[JsonProperty("role")]
internal string rawRole { get; set; }

/// <summary>
/// The role of the message, which can be "system", "assistant" or "user"
/// </summary>
[JsonIgnore]
public ChatMessageRole Role
{
get
{
return ChatMessageRole.FromString(rawRole);
}
set
{
rawRole = value.ToString();
}
}

/// <summary>
/// The content of the message
/// </summary>
[JsonProperty("content")]
public string Content { get; set; }
}
}
10 changes: 5 additions & 5 deletions OpenAI_API/Chat/ChatMessageRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenAI_API.Chat
public class ChatMessageRole : IEquatable<ChatMessageRole>
{
/// <summary>
/// Contructor is prvate to force usage of strongly typed values
/// Contructor is private to force usage of strongly typed values
/// </summary>
/// <param name="value"></param>
private ChatMessageRole(string value) { Value = value; }
Expand All @@ -37,20 +37,20 @@ public static ChatMessageRole FromString(string roleName)
}
}

private string Value { get; set; }
private string Value { get; }

/// <summary>
/// The system message helps set the behavior of the assistant.
/// </summary>
public static ChatMessageRole System { get { return new ChatMessageRole("system"); } }
public static ChatMessageRole System { get; } = new ChatMessageRole("system");
/// <summary>
/// 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.
/// </summary>
public static ChatMessageRole User { get { return new ChatMessageRole("user"); } }
public static ChatMessageRole User { get; } = new ChatMessageRole("user");
/// <summary>
/// The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior.
/// </summary>
public static ChatMessageRole Assistant { get { return new ChatMessageRole("assistant"); } }
public static ChatMessageRole Assistant { get; } = new ChatMessageRole("assistant");

/// <summary>
/// Gets the string value for this role to pass to the API
Expand Down
44 changes: 0 additions & 44 deletions OpenAI_API/Chat/ChatResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,50 +81,6 @@ public override string ToString()
}
}

/// <summary>
/// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content"
/// </summary>
public class ChatMessage
{
public ChatMessage() { }

/// <summary>
/// Constructor for a new Chat Message
/// </summary>
/// <param name="role">The role of the message, which can be "system", "assistant" or "user"</param>
/// <param name="content">The text to send in the message</param>
public ChatMessage(ChatMessageRole role, string content)
{
this.Role = role;
this.Content = content;
}

[JsonProperty("role")]
internal string rawRole { get; set; }

/// <summary>
/// The role of the message, which can be "system", "assistant" or "user"
/// </summary>
[JsonIgnore]
public ChatMessageRole Role
{
get
{
return ChatMessageRole.FromString(rawRole);
}
set
{
rawRole = value.ToString();
}
}

/// <summary>
/// The content of the message
/// </summary>
[JsonProperty("content")]
public string Content { get; set; }
}

/// <summary>
/// How many tokens were used in this chat message.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions OpenAI_Tests/ChatEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 6e0ca11

Please sign in to comment.