-
Notifications
You must be signed in to change notification settings - Fork 79
Add a .NET ChatApp example for AI configuration #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,25 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.14.36119.2 d17.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatApp", "ChatApp\ChatApp.csproj", "{E7CB6AEC-C73D-4C9F-9A28-D728B47CF9EE}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {E7CB6AEC-C73D-4C9F-9A28-D728B47CF9EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E7CB6AEC-C73D-4C9F-9A28-D728B47CF9EE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E7CB6AEC-C73D-4C9F-9A28-D728B47CF9EE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E7CB6AEC-C73D-4C9F-9A28-D728B47CF9EE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {4228A6BE-DF14-40A7-8447-85FAF6249828} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or 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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" /> | ||
| <PackageReference Include="Azure.Identity" Version="1.14.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,13 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| namespace ChatApp | ||
| { | ||
| internal class Message | ||
| { | ||
| [ConfigurationKeyName("role")] | ||
| public string Role { get; set; } | ||
|
|
||
| [ConfigurationKeyName("content")] | ||
| public string Content { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or 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,32 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using OpenAI.Chat; | ||
|
|
||
| namespace ChatApp | ||
| { | ||
| internal class ModelConfiguration | ||
| { | ||
| [ConfigurationKeyName("model")] | ||
| public string Model { get; set; } | ||
|
|
||
| [ConfigurationKeyName("messages")] | ||
| public List<Message> Messages { get; set; } | ||
|
|
||
| [ConfigurationKeyName("max_tokens")] | ||
| public int MaxTokens { get; set; } | ||
|
|
||
| [ConfigurationKeyName("temperature")] | ||
| public float Temperature { get; set; } | ||
|
|
||
| [ConfigurationKeyName("top_p")] | ||
| public float TopP { get; set; } | ||
|
|
||
| public IEnumerable<ChatMessage> ChatMessages => | ||
|
zhenlan marked this conversation as resolved.
Outdated
|
||
| Messages.Select<Message, ChatMessage>(message => message.Role switch | ||
| { | ||
| "system" => ChatMessage.CreateSystemMessage(message.Content), | ||
| "user" => ChatMessage.CreateUserMessage(message.Content), | ||
| "assistant" => ChatMessage.CreateAssistantMessage(message.Content), | ||
| _ => throw new ArgumentException($"Unknown role: {message.Role}", nameof(message.Role)) | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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,66 @@ | ||
| using Azure.AI.OpenAI; | ||
| using Azure.Core; | ||
| using Azure.Identity; | ||
| using ChatApp; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Configuration.AzureAppConfiguration; | ||
| using OpenAI.Chat; | ||
|
|
||
| TokenCredential credential = new DefaultAzureCredential(); | ||
| IConfigurationRefresher _refresher = null; | ||
|
|
||
| // Load configuration from Azure App Configuration | ||
| IConfiguration configuration = new ConfigurationBuilder() | ||
| .AddAzureAppConfiguration(options => | ||
| { | ||
| Uri endpoint = new(Environment.GetEnvironmentVariable("AZURE_APPCONFIG_ENDPOINT") ?? | ||
| throw new InvalidOperationException("The environment variable 'AZURE_APPCONFIG_ENDPOINT' is not set or is empty.")); | ||
| options.Connect(endpoint, credential) | ||
| // Load all keys that have a specific prefix and no label. | ||
|
zhenlan marked this conversation as resolved.
Outdated
|
||
| .Select("ChatLLM:*") | ||
| // Reload configuration if any selected key-values have changed. | ||
| // Use the default refresh interval of 30 seconds. It can be overridden via refreshOptions.SetRefreshInterval. | ||
|
jimmyca15 marked this conversation as resolved.
|
||
| .ConfigureRefresh(refreshOptions => | ||
| { | ||
| refreshOptions.RegisterAll() | ||
| .SetRefreshInterval(TimeSpan.FromSeconds(5)); | ||
| }); | ||
|
|
||
| _refresher = options.GetRefresher(); | ||
| }) | ||
| .Build(); | ||
|
|
||
| // Retrieve the model connection information from the configuration | ||
| Uri chatEndpoint = new (configuration["ChatLLM:Endpoint"]); | ||
| string deploymentName = configuration["ChatLLM:DeploymentName"]; | ||
|
|
||
| // Create a chat client | ||
| AzureOpenAIClient azureClient = new(chatEndpoint, credential); | ||
| ChatClient chatClient = azureClient.GetChatClient(deploymentName); | ||
|
|
||
| while (true) | ||
| { | ||
| // Refresh the configuration from Azure App Configuration | ||
| await _refresher.TryRefreshAsync(); | ||
|
|
||
| // Configure chat completion with AI configuration | ||
| var modelConfig = configuration.GetSection("ChatLLM:Model").Get<ModelConfiguration>(); | ||
|
zhenlan marked this conversation as resolved.
Outdated
|
||
| var requestOptions = new ChatCompletionOptions() | ||
| { | ||
| MaxOutputTokenCount = modelConfig.MaxTokens, | ||
| Temperature = modelConfig.Temperature, | ||
| TopP = modelConfig.TopP | ||
| }; | ||
|
zhenlan marked this conversation as resolved.
|
||
|
|
||
| foreach (var message in modelConfig.Messages) | ||
| { | ||
| Console.WriteLine($"{message.Role}: {message.Content}"); | ||
| } | ||
|
|
||
| // Get chat response from AI | ||
| var response = chatClient.CompleteChat(modelConfig.ChatMessages, requestOptions); | ||
|
zhenlan marked this conversation as resolved.
Outdated
|
||
| System.Console.WriteLine($"AI response: {response.Value.Content[0].Text}"); | ||
|
|
||
| Console.WriteLine("Press Enter to continue..."); | ||
|
jimmyca15 marked this conversation as resolved.
|
||
| Console.ReadLine(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.