Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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>
13 changes: 13 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/Message.cs
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")]
Comment thread
zhenlan marked this conversation as resolved.
public string Role { get; set; }

[ConfigurationKeyName("content")]
public string Content { get; set; }
}
}
32 changes: 32 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/ModelConfiguration.cs
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 =>
Comment thread
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))
});
}
}
66 changes: 66 additions & 0 deletions examples/DotNetCore/ChatApp/ChatApp/Program.cs
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.
Comment thread
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.
Comment thread
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>();
Comment thread
zhenlan marked this conversation as resolved.
Outdated
var requestOptions = new ChatCompletionOptions()
{
MaxOutputTokenCount = modelConfig.MaxTokens,
Temperature = modelConfig.Temperature,
TopP = modelConfig.TopP
};
Comment thread
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);
Comment thread
zhenlan marked this conversation as resolved.
Outdated
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();
}