forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptResultExtensions.cs
45 lines (38 loc) · 1.47 KB
/
PromptResultExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using ModelContextProtocol.Protocol.Types;
namespace MCPClient;
/// <summary>
/// Extension methods for <see cref="GetPromptResult"/>.
/// </summary>
internal static class PromptResultExtensions
{
/// <summary>
/// Converts a <see cref="GetPromptResult"/> to a <see cref="ChatHistory"/>.
/// </summary>
/// <param name="result">The prompt result to convert.</param>
/// <returns>The corresponding <see cref="ChatHistory"/>.</returns>
public static ChatHistory ToChatHistory(this GetPromptResult result)
{
ChatHistory chatHistory = [];
foreach (PromptMessage message in result.Messages)
{
ChatMessageContentItemCollection items = [];
switch (message.Content.Type)
{
case "text":
items.Add(new TextContent(message.Content.Text));
break;
case "image":
items.Add(new ImageContent(Convert.FromBase64String(message.Content.Data!), message.Content.MimeType));
break;
default:
throw new InvalidOperationException($"Unexpected message content type '{message.Content.Type}'");
}
chatHistory.Add(new ChatMessageContent(message.Role.ToAuthorRole(), items));
}
return chatHistory;
}
}