forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptDefinition.cs
111 lines (99 loc) · 3.95 KB
/
PromptDefinition.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Server;
namespace MCPServer.Prompts;
/// <summary>
/// Represents a prompt definition.
/// </summary>
internal sealed class PromptDefinition
{
/// <summary>
/// Gets or sets the prompt.
/// </summary>
public required Prompt Prompt { get; init; }
/// <summary>
/// Gets or sets the handler for the prompt.
/// </summary>
public required Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>> Handler { get; init; }
/// <summary>
/// Gets this prompt definition.
/// </summary>
/// <param name="jsonPrompt">The JSON prompt template.</param>
/// <param name="kernel">An instance of the kernel to render the prompt.</param>
/// <returns>The prompt definition.</returns>
public static PromptDefinition Create(string jsonPrompt, Kernel kernel)
{
PromptTemplateConfig promptTemplateConfig = PromptTemplateConfig.FromJson(jsonPrompt);
return new PromptDefinition()
{
Prompt = GetPrompt(promptTemplateConfig),
Handler = (context, cancellationToken) =>
{
IPromptTemplate promptTemplate = new HandlebarsPromptTemplateFactory().Create(promptTemplateConfig);
return GetPromptHandlerAsync(context, promptTemplateConfig, promptTemplate, kernel, cancellationToken);
}
};
}
/// <summary>
/// Creates an MCP prompt from SK prompt template.
/// </summary>
/// <param name="promptTemplateConfig">The prompt template configuration.</param>
/// <returns>The MCP prompt.</returns>
private static Prompt GetPrompt(PromptTemplateConfig promptTemplateConfig)
{
// Create the MCP prompt arguments
List<PromptArgument>? arguments = null;
foreach (var inputVariable in promptTemplateConfig.InputVariables)
{
(arguments ??= []).Add(new()
{
Name = inputVariable.Name,
Description = inputVariable.Description,
Required = inputVariable.IsRequired
});
}
// Create the MCP prompt
return new Prompt
{
Name = promptTemplateConfig.Name!,
Description = promptTemplateConfig.Description,
Arguments = arguments
};
}
/// <summary>
/// Handles the prompt request by rendering the prompt.
/// </summary>
/// <param name="context">The prompt request context.</param>
/// <param name="promptTemplateConfig">The prompt template configuration.</param>
/// <param name="promptTemplate">The prompt template.</param>
/// <param name="kernel">The kernel to render the prompt.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The prompt.</returns>
private static async Task<GetPromptResult> GetPromptHandlerAsync(RequestContext<GetPromptRequestParams> context, PromptTemplateConfig promptTemplateConfig, IPromptTemplate promptTemplate, Kernel kernel, CancellationToken cancellationToken)
{
// Render the prompt
string renderedPrompt = await promptTemplate.RenderAsync(
kernel: kernel,
arguments: context.Params?.Arguments is { } args ? new KernelArguments(args!) : null,
cancellationToken: cancellationToken);
// Create prompt result
return new GetPromptResult()
{
Description = promptTemplateConfig.Description,
Messages =
[
new PromptMessage()
{
Content = new Content()
{
Type = "text",
Text = renderedPrompt
},
Role = Role.User
}
]
};
}
}