Skip to content

Commit 6e0ca11

Browse files
committed
Fix ChatMessageRole to be singletons, pull ChatMessage to seperate file
1 parent c81ac58 commit 6e0ca11

File tree

4 files changed

+63
-49
lines changed

4 files changed

+63
-49
lines changed

OpenAI_API/Chat/ChatMessage.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace OpenAI_API.Chat
7+
{
8+
/// <summary>
9+
/// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content"
10+
/// </summary>
11+
public class ChatMessage
12+
{
13+
/// <summary>
14+
/// Creates an empty <see cref="ChatMessage"/>, with <see cref="Role"/> defaulting to <see cref="ChatMessageRole.User"/>
15+
/// </summary>
16+
public ChatMessage()
17+
{
18+
this.Role = ChatMessageRole.User;
19+
}
20+
21+
/// <summary>
22+
/// Constructor for a new Chat Message
23+
/// </summary>
24+
/// <param name="role">The role of the message, which can be "system", "assistant" or "user"</param>
25+
/// <param name="content">The text to send in the message</param>
26+
public ChatMessage(ChatMessageRole role, string content)
27+
{
28+
this.Role = role;
29+
this.Content = content;
30+
}
31+
32+
[JsonProperty("role")]
33+
internal string rawRole { get; set; }
34+
35+
/// <summary>
36+
/// The role of the message, which can be "system", "assistant" or "user"
37+
/// </summary>
38+
[JsonIgnore]
39+
public ChatMessageRole Role
40+
{
41+
get
42+
{
43+
return ChatMessageRole.FromString(rawRole);
44+
}
45+
set
46+
{
47+
rawRole = value.ToString();
48+
}
49+
}
50+
51+
/// <summary>
52+
/// The content of the message
53+
/// </summary>
54+
[JsonProperty("content")]
55+
public string Content { get; set; }
56+
}
57+
}

OpenAI_API/Chat/ChatMessageRole.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OpenAI_API.Chat
1212
public class ChatMessageRole : IEquatable<ChatMessageRole>
1313
{
1414
/// <summary>
15-
/// Contructor is prvate to force usage of strongly typed values
15+
/// Contructor is private to force usage of strongly typed values
1616
/// </summary>
1717
/// <param name="value"></param>
1818
private ChatMessageRole(string value) { Value = value; }
@@ -37,20 +37,20 @@ public static ChatMessageRole FromString(string roleName)
3737
}
3838
}
3939

40-
private string Value { get; set; }
40+
private string Value { get; }
4141

4242
/// <summary>
4343
/// The system message helps set the behavior of the assistant.
4444
/// </summary>
45-
public static ChatMessageRole System { get { return new ChatMessageRole("system"); } }
45+
public static ChatMessageRole System { get; } = new ChatMessageRole("system");
4646
/// <summary>
4747
/// 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.
4848
/// </summary>
49-
public static ChatMessageRole User { get { return new ChatMessageRole("user"); } }
49+
public static ChatMessageRole User { get; } = new ChatMessageRole("user");
5050
/// <summary>
5151
/// The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior.
5252
/// </summary>
53-
public static ChatMessageRole Assistant { get { return new ChatMessageRole("assistant"); } }
53+
public static ChatMessageRole Assistant { get; } = new ChatMessageRole("assistant");
5454

5555
/// <summary>
5656
/// Gets the string value for this role to pass to the API

OpenAI_API/Chat/ChatResult.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -81,50 +81,6 @@ public override string ToString()
8181
}
8282
}
8383

84-
/// <summary>
85-
/// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content"
86-
/// </summary>
87-
public class ChatMessage
88-
{
89-
public ChatMessage() { }
90-
91-
/// <summary>
92-
/// Constructor for a new Chat Message
93-
/// </summary>
94-
/// <param name="role">The role of the message, which can be "system", "assistant" or "user"</param>
95-
/// <param name="content">The text to send in the message</param>
96-
public ChatMessage(ChatMessageRole role, string content)
97-
{
98-
this.Role = role;
99-
this.Content = content;
100-
}
101-
102-
[JsonProperty("role")]
103-
internal string rawRole { get; set; }
104-
105-
/// <summary>
106-
/// The role of the message, which can be "system", "assistant" or "user"
107-
/// </summary>
108-
[JsonIgnore]
109-
public ChatMessageRole Role
110-
{
111-
get
112-
{
113-
return ChatMessageRole.FromString(rawRole);
114-
}
115-
set
116-
{
117-
rawRole = value.ToString();
118-
}
119-
}
120-
121-
/// <summary>
122-
/// The content of the message
123-
/// </summary>
124-
[JsonProperty("content")]
125-
public string Content { get; set; }
126-
}
127-
12884
/// <summary>
12985
/// How many tokens were used in this chat message.
13086
/// </summary>

OpenAI_Tests/ChatEndpointTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void SimpleCompletion()
7979
Assert.NotZero(results.Choices.Count);
8080
Assert.AreEqual(ChatMessageRole.Assistant, results.Choices[0].Message.Role);
8181
Assert.That(results.Choices.All(c => c.Message.Role.Equals(ChatMessageRole.Assistant)));
82+
Assert.That(results.Choices.All(c => c.Message.Role == ChatMessageRole.Assistant));
8283
Assert.That(results.Choices.All(c => c.Message.Content.Length > 1));
8384
Assert.IsNotEmpty(results.ToString());
8485
}

0 commit comments

Comments
 (0)