-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from matthewbolanos/main
Update starters for v1.0.0 RC1
- Loading branch information
Showing
31 changed files
with
375 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.AI.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.AI.OpenAI; | ||
|
||
/// <summary> | ||
/// This is the main application service. | ||
/// This takes console input, then sends it to the configured AI service, and then prints the response. | ||
/// All conversation history is maintained in the chat history. | ||
/// </summary> | ||
internal class ConsoleChat : IHostedService | ||
{ | ||
private readonly Kernel _kernel; | ||
private readonly IHostApplicationLifetime _lifeTime; | ||
|
||
public ConsoleChat(Kernel kernel, IHostApplicationLifetime lifeTime) | ||
{ | ||
this._kernel = kernel; | ||
this._lifeTime = lifeTime; | ||
} | ||
|
||
/// <summary> | ||
/// Start the service. | ||
/// </summary> | ||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
Task.Run(() => this.ExecuteAsync(cancellationToken), cancellationToken); | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Stop a running service. | ||
/// </summary> | ||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
/// <summary> | ||
/// The main execution loop. It will use any of the available plugins to perform actions | ||
/// </summary> | ||
private async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
ChatHistory chatMessages = []; | ||
IChatCompletionService chatCompletionService = this._kernel.GetService<IChatCompletionService>(); | ||
|
||
// Loop till we are cancelled | ||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
// Get user input | ||
System.Console.Write("User > "); | ||
chatMessages.AddUserMessage(Console.ReadLine()!); | ||
|
||
// Get the chat completions | ||
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() | ||
{ | ||
FunctionCallBehavior = FunctionCallBehavior.AutoInvokeKernelFunctions | ||
}; | ||
IAsyncEnumerable<StreamingChatMessageContent> result = | ||
chatCompletionService.GetStreamingChatMessageContentsAsync( | ||
chatMessages, | ||
executionSettings: openAIPromptExecutionSettings, | ||
kernel: this._kernel, | ||
cancellationToken: cancellationToken); | ||
|
||
// Print the chat completions | ||
ChatMessageContent? chatMessageContent = null; | ||
await foreach (var content in result) | ||
{ | ||
if (content.Role.HasValue) | ||
{ | ||
System.Console.Write("Assistant > "); | ||
chatMessageContent = new( | ||
content.Role ?? AuthorRole.Assistant, | ||
content.ModelId!, | ||
content.Content!, | ||
content.InnerContent, | ||
content.Encoding, | ||
content.Metadata | ||
); | ||
} | ||
System.Console.Write(content.Content); | ||
chatMessageContent!.Content += content.Content; | ||
} | ||
System.Console.WriteLine(); | ||
chatMessages.AddMessage(chatMessageContent!); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sk-csharp-console-chat/config/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
|
||
internal static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a chat completion service to the list. It can be either an OpenAI or Azure OpenAI backend service. | ||
/// </summary> | ||
/// <param name="kernelBuilder"></param> | ||
/// <param name="kernelSettings"></param> | ||
/// <exception cref="ArgumentException"></exception> | ||
internal static IServiceCollection AddChatCompletionService(this IServiceCollection serviceCollection, KernelSettings kernelSettings) | ||
{ | ||
switch (kernelSettings.ServiceType.ToUpperInvariant()) | ||
{ | ||
case ServiceTypes.AzureOpenAI: | ||
serviceCollection = serviceCollection.AddAzureOpenAIChatCompletion(kernelSettings.DeploymentId, kernelSettings.ModelId, endpoint: kernelSettings.Endpoint, apiKey: kernelSettings.ApiKey, serviceId: kernelSettings.ServiceId); | ||
break; | ||
|
||
case ServiceTypes.OpenAI: | ||
serviceCollection = serviceCollection.AddOpenAIChatCompletion(modelId: kernelSettings.ModelId, apiKey: kernelSettings.ApiKey, orgId: kernelSettings.OrgId, serviceId: kernelSettings.ServiceId); | ||
break; | ||
|
||
default: | ||
throw new ArgumentException($"Invalid service type value: {kernelSettings.ServiceType}"); | ||
} | ||
|
||
return serviceCollection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"serviceType": "AzureOpenAI", | ||
"serviceId": "gpt-35-turbo", | ||
"deploymentOrModelId": "gpt-35-turbo", | ||
"deploymentId": "gpt-35-turbo", | ||
"modelId": "gpt-35-turbo", | ||
"endpoint": "https:// ... your endpoint ... .openai.azure.com/", | ||
"apiKey": "... your Azure OpenAI key ...", | ||
"systemPrompt": "You are a friendly, intelligent, and curious assistant who is good at conversation." | ||
"apiKey": "... your Azure OpenAI key ..." | ||
} |
5 changes: 2 additions & 3 deletions
5
sk-csharp-console-chat/config/appsettings.json.openai-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"serviceType": "OpenAI", | ||
"serviceId": "gpt-3.5-turbo", | ||
"deploymentOrModelId": "gpt-3.5-turbo", | ||
"modelId": "gpt-3.5-turbo", | ||
"apiKey": "... your OpenAI key ...", | ||
"orgId": "", | ||
"systemPrompt": "You are a friendly, intelligent, and curious assistant who is good at conversation." | ||
"orgId": "" | ||
} |
Oops, something went wrong.