Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp.sln
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
17 changes: 17 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/ChatApp.csproj
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>
16 changes: 16 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Configuration;

namespace ChatApp
{
internal class Message
{
[ConfigurationKeyName("role")]
Comment thread
zhenlan marked this conversation as resolved.
public required string Role { get; set; }

[ConfigurationKeyName("content")]
public string? Content { get; set; }
}
}
25 changes: 25 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/ModelConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Configuration;

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; }
}
}
79 changes: 79 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
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 start with "ChatApp:" and have no label.
.Select("ChatApp:*")
// Reload configuration if any selected key-values have changed.
// Use the default refresh interval of 30 seconds. It can be overridden via refreshOptions.SetRefreshInterval.
Comment thread
jimmyca15 marked this conversation as resolved.
.ConfigureRefresh(refreshOptions =>
{
refreshOptions.RegisterAll();
});

refresher = options.GetRefresher();
})
.Build();

// Retrieve the OpenAI connection information from the configuration
Uri openaiEndpoint = new (configuration["ChatApp:AzureOpenAI:Endpoint"]);
string deploymentName = configuration["ChatApp:AzureOpenAI:DeploymentName"];

// Create a chat client
AzureOpenAIClient azureClient = new(openaiEndpoint, 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 modelConfiguration = configuration.GetSection("ChatApp:Model").Get<ModelConfiguration>();
var requestOptions = new ChatCompletionOptions()
{
MaxOutputTokenCount = modelConfiguration.MaxTokens,
Temperature = modelConfiguration.Temperature,
TopP = modelConfiguration.TopP
};

foreach (var message in modelConfiguration.Messages)
{
Console.WriteLine($"{message.Role}: {message.Content}");
}

// Get chat response from AI
var response = await chatClient.CompleteChatAsync(GetChatMessages(modelConfiguration), requestOptions);
System.Console.WriteLine($"AI response: {response.Value.Content[0].Text}");

Console.WriteLine("Press Enter to continue...");
Comment thread
jimmyca15 marked this conversation as resolved.
Console.ReadLine();
}

static IEnumerable<ChatMessage> GetChatMessages(ModelConfiguration modelConfiguration)
{
return modelConfiguration.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))
});
}
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## .NET Samples

### [AI Chat App](./DotNetCore/ChatApp)

This example showcases a .NET console application that retrieves chat responses from Azure OpenAI. It demonstrates how to configure chat completion using AI Configuration from Azure App Configuration, enabling rapid prompt iteration and frequent tuning of model parameters—without requiring application restarts, rebuilds, or redeployments.

### [Azure Functions (Isolated worker model)](./DotNetCore/AzureFunctions/FunctionAppIsolated)

This example showcases a .NET isolated worker model Function App, which operates out-of-process in Azure Functions. It demonstrates how to enable dynamic configuration and utilize feature flags from App Configuration. Additionally, it illustrates how to leverage the App Configuration references feature in Azure Functions to manage trigger parameters within App Configuration.
Expand Down