-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathHostedService.cs
179 lines (154 loc) · 7.16 KB
/
HostedService.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Reflection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.ChatCompletion;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using NetCoreAudio;
namespace ConversationalSpeaker
{
/// <summary>
/// A hosted service providing the primary conversation loop for Semantic Kernel with OpenAI ChatGPT.
/// </summary>
internal class HostedService : IHostedService, IDisposable
{
private readonly ILogger<HostedService> _logger;
// Semantic Kernel chat support
private readonly IKernel _semanticKernel;
private readonly IDictionary<string, ISKFunction> _speechSkill;
private readonly AzCognitiveServicesWakeWordListener _wakeWordListener;
private readonly IChatCompletion _chatCompletion;
private readonly OpenAIChatHistory _chatHistory;
private readonly ChatRequestSettings _chatRequestSettings;
private Task _executeTask;
private readonly CancellationTokenSource _cancelToken = new();
// Notification sound support
private readonly string _notificationSoundFilePath;
private readonly Player _player;
/// <summary>
/// Constructor
/// </summary>
public HostedService(
AzCognitiveServicesWakeWordListener wakeWordListener,
IKernel semanticKernel,
AzCognitiveServicesSpeechSkill speechSkill,
IOptions<OpenAiServiceOptions> openAIOptions,
IOptions<AzureOpenAiServiceOptions> azureOpenAIOptions,
IOptions<GeneralOptions> generalOptions,
ILogger<HostedService> logger)
{
_logger = logger;
_semanticKernel = semanticKernel;
//
// To switch from using OpenAI to Azure OpenAI, comment out the "OpenAI" section below
// and uncomment the subsequent "Azure OpenAI" section. Make sure you have updated
// the `/src/configuration.json` with your Azure OpenAI settings as well.
//
// OpenAI BEGIN
_chatRequestSettings = new ChatRequestSettings()
{
MaxTokens = openAIOptions.Value.MaxTokens,
Temperature = openAIOptions.Value.Temperature,
FrequencyPenalty = openAIOptions.Value.FrequencyPenalty,
PresencePenalty = openAIOptions.Value.PresencePenalty,
TopP = openAIOptions.Value.TopP,
StopSequences = new string[] { "\n\n" }
};
_semanticKernel.Config.AddOpenAIChatCompletionService(
openAIOptions.Value.Model, openAIOptions.Value.Key, alsoAsTextCompletion: true, logger: _logger);
// OpenAI END
// Azure OpenAI BEGIN
//_chatRequestSettings = new ChatRequestSettings()
//{
// MaxTokens = azureOpenAIOptions.Value.MaxTokens,
// Temperature = azureOpenAIOptions.Value.Temperature,
// FrequencyPenalty = azureOpenAIOptions.Value.FrequencyPenalty,
// PresencePenalty = azureOpenAIOptions.Value.PresencePenalty,
// TopP = azureOpenAIOptions.Value.TopP,
// StopSequences = new string[] { "\n\n" }
//};
//_semanticKernel.Config.AddAzureChatCompletionService(
// azureOpenAIOptions.Value.Deployment, azureOpenAIOptions.Value.Endpoint, azureOpenAIOptions.Value.Key, alsoAsTextCompletion: true, logger: _logger);
// Azure OpenAI END
_wakeWordListener = wakeWordListener;
_chatCompletion = _semanticKernel.GetService<IChatCompletion>();
_chatHistory = (OpenAIChatHistory)_chatCompletion.CreateNewChat(generalOptions.Value.SystemPrompt);
_speechSkill = _semanticKernel.ImportSkill(speechSkill);
_notificationSoundFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Handlers", "bing.mp3");
_player = new Player();
}
/// <summary>
/// Start the service.
/// </summary>
public Task StartAsync(CancellationToken cancellationToken)
{
_executeTask = ExecuteAsync(_cancelToken.Token);
return Task.CompletedTask;
}
/// <summary>
/// Primary service logic loop.
/// </summary>
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// Play a notification to let the user know we have started listening for the wake phrase.
await _player.Play(_notificationSoundFilePath);
// Wait for wake word or phrase
if (!await _wakeWordListener.WaitForWakeWordAsync(cancellationToken))
{
continue;
}
await _player.Play(_notificationSoundFilePath);
// Say hello on startup
await _semanticKernel.RunAsync("Hello!", _speechSkill["Speak"]);
// Start listening
while (!cancellationToken.IsCancellationRequested)
{
// Listen to the user
SKContext context = await _semanticKernel.RunAsync(_speechSkill["Listen"]);
string userSpoke = context.Result;
// Get a reply from the AI and add it to the chat history.
string reply = string.Empty;
try
{
_chatHistory.AddUserMessage(userSpoke);
reply = await _chatCompletion.GenerateMessageAsync(_chatHistory, _chatRequestSettings);
// Add the interaction to the chat history.
_chatHistory.AddAssistantMessage(reply);
}
catch (AIException aiex)
{
_logger.LogError($"OpenAI returned an error. {aiex.ErrorCode}: {aiex.Message}");
reply = "OpenAI returned an error. Please try again.";
}
// Speak the AI's reply
await _semanticKernel.RunAsync(reply, _speechSkill["Speak"]);
// If the user said "Goodbye" - stop listening and wait for the wake work again.
if (userSpoke.StartsWith("goodbye", StringComparison.InvariantCultureIgnoreCase))
{
break;
}
}
}
}
/// <summary>
/// Stop a running service.
/// </summary>
public Task StopAsync(CancellationToken cancellationToken)
{
_cancelToken.Cancel();
return Task.CompletedTask;
}
/// <inheritdoc/>
public virtual void Dispose()
{
_cancelToken.Dispose();
_wakeWordListener.Dispose();
}
}
}