Skip to content

Commit c62df4f

Browse files
Merge pull request #278 from Avanade/feat/newcartridges
feat: Refactor LiquidChatMessage and update project references
2 parents e8db908 + dc04c87 commit c62df4f

File tree

6 files changed

+126
-9
lines changed

6 files changed

+126
-9
lines changed

src/Liquid.Core/GenAi/Entities/LiquidChatMessage.cs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using Liquid.Core.GenAi.Enums;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics.CodeAnalysis;
25

36
namespace Liquid.Core.GenAi.Entities
47
{
@@ -11,11 +14,80 @@ public class LiquidChatMessage
1114
/// <summary>
1215
/// The chat role associated with this message.
1316
/// </summary>
14-
public string Role { get; set; }
17+
public LiquidMessageRole Role { get; set; }
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="LiquidChatMessage"/> class with the specified role.
21+
/// </summary>
22+
/// <param name="role">The role associated with the chat message. This value cannot be null or empty.</param>
23+
public LiquidChatMessage(LiquidMessageRole role)
24+
{
25+
Role = role;
26+
}
1527

1628
/// <summary>
1729
/// The contents of the message.
1830
/// </summary>
19-
public LiquidChatContent[] Content { get; set; }
31+
public LiquidChatContent[] Content { get; set; } = Array.Empty<LiquidChatContent>();
32+
33+
34+
/// <summary>
35+
/// Adds a text content item to the current collection of chat contents.
36+
/// </summary>
37+
/// <remarks>This method appends the specified text as a new content item to the existing
38+
/// collection. If the collection is initially null, it initializes the collection with the new content
39+
/// item.</remarks>
40+
/// <param name="text">The text content to add. Cannot be null, empty, or consist solely of whitespace.</param>
41+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is null, empty, or consists only of whitespace.</exception>
42+
public void AddContent(string text)
43+
{
44+
if (string.IsNullOrWhiteSpace(text))
45+
{
46+
throw new ArgumentException("Text content cannot be null or empty.", nameof(text));
47+
}
48+
var content = new LiquidChatContent
49+
{
50+
Kind = LiquidContentKind.Text,
51+
Text = text
52+
};
53+
if (Content == null)
54+
{
55+
Content = new[] { content };
56+
}
57+
else
58+
{
59+
var contentList = new List<LiquidChatContent>(Content) { content };
60+
Content = contentList.ToArray();
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Adds an image content to the current collection of chat contents.
66+
/// </summary>
67+
/// <remarks>If the content collection is initially empty, this method initializes it with the new
68+
/// image content. Otherwise, it appends the image content to the existing collection.</remarks>
69+
/// <param name="imageUri">The URI of the image to be added. Cannot be <see langword="null"/>.</param>
70+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="imageUri"/> is <see langword="null"/>.</exception>
71+
public void AddContent(Uri imageUri)
72+
{
73+
if (imageUri == null)
74+
{
75+
throw new ArgumentNullException(nameof(imageUri), "Image URI cannot be null.");
76+
}
77+
var content = new LiquidChatContent
78+
{
79+
Kind = LiquidContentKind.Image,
80+
ImageUri = imageUri
81+
};
82+
if (Content == null)
83+
{
84+
Content = new[] { content };
85+
}
86+
else
87+
{
88+
var contentList = new List<LiquidChatContent>(Content) { content };
89+
Content = contentList.ToArray();
90+
}
91+
}
2092
}
2193
}

src/Liquid.Core/GenAi/Entities/LiquidChatMessages.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,20 @@ public class LiquidChatMessages
1313
/// The collection of context messages associated with a chat completions request.
1414
/// </summary>
1515
public List<LiquidChatMessage> Messages { get; set; } = new List<LiquidChatMessage>();
16+
17+
/// <summary>
18+
/// Adds a message to the collection of chat messages.
19+
/// </summary>
20+
/// <param name="message">The chat message to add. Cannot be <see langword="null"/>.</param>
21+
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="message"/> is <see langword="null"/>.</exception>
22+
public void AddMessage(LiquidChatMessage message)
23+
{
24+
if (message == null)
25+
{
26+
throw new System.ArgumentNullException(nameof(message), "Message cannot be null");
27+
}
28+
Messages.Add(message);
29+
}
30+
1631
}
1732
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Liquid.Core.GenAi.Enums
8+
{
9+
/// <summary>
10+
/// Specifies the role of a participant in a message exchange, such as a user, assistant, or system.
11+
/// </summary>
12+
public enum LiquidMessageRole
13+
{
14+
/// <summary>
15+
/// The user role, typically representing the end user or client.
16+
/// </summary>
17+
User,
18+
/// <summary>
19+
/// The assistant role, typically representing the AI or system responding to the user.
20+
/// </summary>
21+
Assistant,
22+
/// <summary>
23+
/// The system role, typically used for system-level messages or instructions.
24+
/// </summary>
25+
System
26+
}
27+
}

src/Liquid.Core/Liquid.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Copyright>Avanade 2019</Copyright>
1111
<PackageProjectUrl>https://github.com/Avanade/Liquid-Application-Framework</PackageProjectUrl>
1212
<PackageIcon>logo.png</PackageIcon>
13-
<Version>8.0.0</Version>
13+
<Version>8.1.0</Version>
1414
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1515
<ProjectGuid>{C33A89FC-4F4D-4274-8D0F-29456BA8F76B}</ProjectGuid>
1616
<IsPackable>true</IsPackable>

src/Liquid.GenAi.OpenAi/Liquid.GenAi.OpenAi.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232

3333
<ItemGroup>
3434
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
35-
<PackageReference Include="Liquid.Core" Version="8.0.0" />
3635
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
3736
</ItemGroup>
37+
38+
<ItemGroup>
39+
<ProjectReference Include="..\Liquid.Core\Liquid.Core.csproj" />
40+
</ItemGroup>
3841

3942
</Project>

src/Liquid.GenAi.OpenAi/OpenAiAdapter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ private static List<ChatMessage> GetChatMessagesAsync(string content, string pro
150150
private static ChatMessage MapChatRequestMessage(LiquidChatMessage message)
151151
{
152152
ChatMessage? chatRequestMessage = null;
153-
switch (message.Role.ToLower())
153+
switch (message.Role)
154154
{
155-
case "system":
155+
case LiquidMessageRole.System:
156156
chatRequestMessage = new SystemChatMessage(CreateContent(message));
157157
break;
158-
case "assistant":
158+
case LiquidMessageRole.Assistant:
159159
chatRequestMessage = new AssistantChatMessage(CreateContent(message));
160160
break;
161-
case "user":
161+
case LiquidMessageRole.User:
162162
chatRequestMessage = new UserChatMessage(CreateContent(message));
163163
break;
164164
default:

0 commit comments

Comments
 (0)