Skip to content

Commit e4874bb

Browse files
.Net: Extend chat message parsing and add relevant unit tests (microsoft#4602)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Splitted PR microsoft#4536 Allow passing image content in prompt text. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Added new method named 'ParseChatNode' for interpreting images and text content within chat messages. Added unit tests for ChatPromptParser. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Dmytro Struk <[email protected]>
1 parent 3fadf9a commit e4874bb

2 files changed

Lines changed: 191 additions & 8 deletions

File tree

‎dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatPromptParser.cs‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
67

78
namespace Microsoft.SemanticKernel.ChatCompletion;
89

@@ -13,6 +14,8 @@ internal static class ChatPromptParser
1314
{
1415
private const string MessageTagName = "message";
1516
private const string RoleAttributeName = "role";
17+
private const string ImageTagName = "image";
18+
private const string TextTagName = "text";
1619

1720
/// <summary>
1821
/// Parses a prompt for an XML representation of a <see cref="ChatHistory"/>.
@@ -48,29 +51,69 @@ private static bool TryParse(List<PromptNode> nodes, [NotNullWhen(true)] out Cha
4851
{
4952
chatHistory = null;
5053

51-
foreach (var node in nodes)
54+
foreach (var node in nodes.Where(IsValidChatMessage))
5255
{
53-
if (IsValidChatMessage(node))
54-
{
55-
var role = node.Attributes[RoleAttributeName];
56-
var content = node.Content!;
56+
(chatHistory ??= new()).Add(ParseChatNode(node));
57+
}
58+
59+
return chatHistory is not null;
60+
}
5761

58-
(chatHistory ??= new()).AddMessage(new AuthorRole(role), content);
62+
/// <summary>
63+
/// Parses a chat node and constructs a <see cref="ChatMessageContent"/> object.
64+
/// </summary>
65+
/// <param name="node">The prompt node to parse.</param>
66+
/// <returns><see cref="ChatMessageContent"/> object.</returns>
67+
private static ChatMessageContent ParseChatNode(PromptNode node)
68+
{
69+
ChatMessageContentItemCollection items = new();
70+
foreach (var childNode in node.ChildNodes.Where(childNode => childNode.Content is not null))
71+
{
72+
if (childNode.TagName.Equals(ImageTagName, StringComparison.OrdinalIgnoreCase))
73+
{
74+
items.Add(new ImageContent(new Uri(childNode.Content!)));
75+
}
76+
else if (childNode.TagName.Equals(TextTagName, StringComparison.OrdinalIgnoreCase))
77+
{
78+
items.Add(new TextContent(childNode.Content));
5979
}
6080
}
6181

62-
return chatHistory is not null;
82+
if (items.Count == 1 && items[0] is TextContent textContent)
83+
{
84+
node.Content = textContent.Text;
85+
items.Clear();
86+
}
87+
88+
var authorRole = new AuthorRole(node.Attributes[RoleAttributeName]);
89+
90+
return items.Count > 0
91+
? new ChatMessageContent(authorRole, items)
92+
: new ChatMessageContent(authorRole, node.Content);
6393
}
6494

6595
/// <summary>
6696
/// Checks if <see cref="PromptNode"/> is valid chat message.
6797
/// </summary>
6898
/// <param name="node">Instance of <see cref="PromptNode"/>.</param>
99+
/// <remarks>
100+
/// A valid chat message is a node with the following structure:<br/>
101+
/// TagName = "message"<br/>
102+
/// Attributes = { "role" : "..." }<br/>
103+
/// optional one or more child nodes <image>...</image><br/>
104+
/// content not null or single child node <text>...</text>
105+
/// </remarks>
69106
private static bool IsValidChatMessage(PromptNode node)
70107
{
71108
return
72109
node.TagName.Equals(MessageTagName, StringComparison.OrdinalIgnoreCase) &&
73110
node.Attributes.ContainsKey(RoleAttributeName) &&
74-
node.Content is not null;
111+
IsValidChildNodes(node);
112+
}
113+
114+
private static bool IsValidChildNodes(PromptNode node)
115+
{
116+
var textTagsCount = node.ChildNodes.Count(n => n.TagName.Equals(TextTagName, StringComparison.OrdinalIgnoreCase));
117+
return textTagsCount == 1 || (textTagsCount == 0 && node.Content is not null);
75118
}
76119
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using Microsoft.SemanticKernel;
5+
using Microsoft.SemanticKernel.ChatCompletion;
6+
using Xunit;
7+
8+
namespace SemanticKernel.UnitTests.Prompt;
9+
10+
public sealed class ChatPromptParserTests
11+
{
12+
[Theory]
13+
[InlineData("This is plain prompt")]
14+
[InlineData("<message This is invalid chat prompt>")]
15+
[InlineData("<message role='user'><text>This is invalid</text><text>chat prompt</text></message>")]
16+
public void ItReturnsNullChatHistoryWhenPromptIsPlainTextOrInvalid(string prompt)
17+
{
18+
// Act
19+
var result = ChatPromptParser.TryParse(prompt, out var chatHistory);
20+
21+
// Assert
22+
Assert.False(result);
23+
Assert.Null(chatHistory);
24+
}
25+
26+
[Fact]
27+
public void ItReturnsChatHistoryWithValidRolesWhenPromptIsValid()
28+
{
29+
// Arrange
30+
string prompt = GetSimpleValidPrompt();
31+
32+
// Act
33+
bool result = ChatPromptParser.TryParse(prompt, out var chatHistory);
34+
35+
// Assert
36+
Assert.True(result);
37+
Assert.NotNull(chatHistory);
38+
39+
Assert.Collection(chatHistory,
40+
c => c.Role = AuthorRole.System,
41+
c => c.Role = AuthorRole.User,
42+
c => c.Role = AuthorRole.Assistant,
43+
c => c.Role = AuthorRole.System,
44+
c => c.Role = AuthorRole.User);
45+
}
46+
47+
[Fact]
48+
public void ItReturnsChatHistoryWithValidContentWhenSimplePrompt()
49+
{
50+
// Arrange
51+
string prompt = GetSimpleValidPrompt();
52+
53+
// Act
54+
bool result = ChatPromptParser.TryParse(prompt, out var chatHistory);
55+
56+
// Assert
57+
Assert.True(result);
58+
Assert.NotNull(chatHistory);
59+
60+
Assert.Collection(chatHistory,
61+
c => Assert.Equal("Test with role in double quotes and content in new line.", c.Content),
62+
c => Assert.Equal("Test with role in single quotes and content in the same line.", c.Content),
63+
c => Assert.Equal("""
64+
Test with multiline content.
65+
Second line.
66+
""", c.Content),
67+
c => Assert.Equal("Test line with tab.", c.Content),
68+
c => Assert.Equal("Hello, I'm a user.", c.Content));
69+
}
70+
71+
[Fact]
72+
public void ItReturnsChatHistoryWithValidContentItemsWhenNestedPrompt()
73+
{
74+
// Arrange
75+
string prompt = GetNestedItemsValidPrompt();
76+
77+
// Act
78+
bool result = ChatPromptParser.TryParse(prompt, out var chatHistory);
79+
80+
// Assert
81+
Assert.True(result);
82+
Assert.NotNull(chatHistory);
83+
84+
Assert.Collection(chatHistory,
85+
c => Assert.Equal("Hi how are you?", c.Content),
86+
c => Assert.Equal("""
87+
Test with multiline content.
88+
Second line.
89+
""", c.Content),
90+
c => Assert.True(((TextContent)c.Items![0]).Text!.Equals("explain image", StringComparison.Ordinal)
91+
&& ((ImageContent)c.Items![1]).Uri!.AbsoluteUri == "https://fake-link-to-image/"));
92+
}
93+
94+
private static string GetSimpleValidPrompt()
95+
{
96+
return
97+
"""
98+
99+
<message role="system">
100+
Test with role in double quotes and content in new line.
101+
</message>
102+
103+
<message role='user'>Test with role in single quotes and content in the same line.</message>
104+
105+
<message role="assistant">
106+
Test with multiline content.
107+
Second line.
108+
</message>
109+
110+
<message role='system'>
111+
Test line with tab.
112+
</message>
113+
114+
<message role='user'>
115+
Hello, I'm a user.
116+
</message>
117+
118+
""";
119+
}
120+
121+
private static string GetNestedItemsValidPrompt()
122+
{
123+
return
124+
"""
125+
126+
<message role='user'><text>Hi how are you?</text></message>
127+
128+
<message role="assistant">
129+
Test with multiline content.
130+
Second line.
131+
</message>
132+
133+
<message role='user'>
134+
<text>explain image</text>
135+
<image>https://fake-link-to-image/</image>
136+
</message>
137+
138+
""";
139+
}
140+
}

0 commit comments

Comments
 (0)