-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathCreativeWriterSession.cs
More file actions
136 lines (120 loc) · 5.97 KB
/
CreativeWriterSession.cs
File metadata and controls
136 lines (120 loc) · 5.97 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using ChatApp.ServiceDefaults.Contracts;
using ChatApp.WebApi.Model;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Chat;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using System.Text;
using Microsoft.SemanticKernel.PromptTemplates.Liquid;
using Microsoft.SemanticKernel.Prompty;
namespace ChatApp.WebApi.Agents;
public class CreativeWriterSession(Kernel kernel, Kernel bingKernel, Kernel vectorSearchKernel)
{
private const string ResearcherName = "Researcher";
private const string MarketingName = "Marketing";
private const string WriterName = "Writer";
private const string EditorName = "Editor";
internal async IAsyncEnumerable<AIChatCompletionDelta> ProcessStreamingRequest(CreateWriterRequest createWriterRequest)
{
ChatCompletionAgent researcherAgent = new(ReadPromptyFileForTemplateConfig("./Agents/Prompts/researcher.prompty"), new LiquidPromptTemplateFactory())
{
Name = ResearcherName,
Kernel = bingKernel,
Arguments = CreateFunctionChoiceAutoBehavior(),
LoggerFactory = bingKernel.LoggerFactory
};
ChatCompletionAgent marketingAgent = new(ReadPromptyFileForTemplateConfig("./Agents/Prompts/marketing.prompty"), new LiquidPromptTemplateFactory())
{
Name = MarketingName,
Kernel = vectorSearchKernel,
Arguments = CreateFunctionChoiceAutoBehavior(),
LoggerFactory = vectorSearchKernel.LoggerFactory
};
StringBuilder sbResearchResults = new();
await foreach (ChatMessageContent response in researcherAgent.InvokeAsync([], new() { { "research_context", createWriterRequest.Research } }))
{
sbResearchResults.AppendLine(response.Content);
yield return new AIChatCompletionDelta(Delta: new AIChatMessageDelta
{
Role = AIChatRole.Assistant,
Context = new AIChatAgentInfo(ResearcherName),
Content = response.Content,
});
}
StringBuilder sbProductResults = new();
await foreach (ChatMessageContent response in marketingAgent.InvokeAsync([], new() { { "product_context", createWriterRequest.Products } }))
{
sbProductResults.AppendLine(response.Content);
yield return new AIChatCompletionDelta(Delta: new AIChatMessageDelta
{
Role = AIChatRole.Assistant,
Context = new AIChatAgentInfo(MarketingName),
Content = response.Content,
});
}
ChatCompletionAgent writerAgent = new(ReadPromptyFileForTemplateConfig("./Agents/Prompts/writer.prompty"), new LiquidPromptTemplateFactory())
{
Name = WriterName,
Kernel = kernel,
Arguments = [],
LoggerFactory = kernel.LoggerFactory
};
ChatCompletionAgent editorAgent = new(ReadPromptyFileForTemplateConfig("./Agents/Prompts/editor.prompty"), new LiquidPromptTemplateFactory())
{
Name = EditorName,
Kernel = kernel,
LoggerFactory = kernel.LoggerFactory
};
writerAgent.Arguments["research_context"] = createWriterRequest.Research;
writerAgent.Arguments["research_results"] = sbResearchResults.ToString();
writerAgent.Arguments["product_context"] = createWriterRequest.Products;
writerAgent.Arguments["product_results"] = sbProductResults.ToString();
writerAgent.Arguments["assignment"] = createWriterRequest.Writing;
AgentGroupChat chat = new(writerAgent, editorAgent)
{
LoggerFactory = kernel.LoggerFactory,
ExecutionSettings = new AgentGroupChatSettings
{
SelectionStrategy = new SequentialSelectionStrategy() { InitialAgent = writerAgent },
TerminationStrategy = new NoFeedbackLeftTerminationStrategy()
}
};
await foreach (ChatMessageContent response in chat.InvokeAsync())
{
yield return new AIChatCompletionDelta(Delta: new AIChatMessageDelta
{
Role = AIChatRole.Assistant,
Context = new AIChatAgentInfo(response.AuthorName ?? ""),
Content = response.Content,
});
}
}
private static PromptTemplateConfig ReadFileForPromptTemplateConfig(string fileName)
{
string yaml = File.ReadAllText(fileName);
return KernelFunctionYaml.ToPromptTemplateConfig(yaml);
}
private static PromptTemplateConfig ReadPromptyFileForTemplateConfig(string fileName)
{
string prompty = File.ReadAllText(fileName);
#pragma warning disable SKEXP0040 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
return KernelFunctionPrompty.ToPromptTemplateConfig(prompty);
#pragma warning restore SKEXP0040 // Type is for evaluation purposes only and is subject to change or removal in future updates.
}
private static KernelArguments CreateFunctionChoiceAutoBehavior()
{
return new KernelArguments(new AzureOpenAIPromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Required() });
}
private sealed class NoFeedbackLeftTerminationStrategy : TerminationStrategy
{
// Terminate when the final message contains the term "Article accepted, no further rework necessary." - all done
protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken)
{
if (agent.Name != EditorName)
return Task.FromResult(false);
return Task.FromResult(history[history.Count - 1].Content?.Contains("Article accepted", StringComparison.OrdinalIgnoreCase) ?? false);
}
}
}