Skip to content

.Net: Read the agent and OpenAI model prosp from configuration #10806

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
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
1 change: 1 addition & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ProcessFrameworkWithAspire", "ProcessFrameworkWithAspire", "{3F260A77-B6C9-97FD-1304-4B34DA936CF4}"
ProjectSection(SolutionItems) = preProject
samples\Demos\ProcessFrameworkWithAspire\README.md = samples\Demos\ProcessFrameworkWithAspire\README.md
samples\Demos\Hosting\Agent\README.md = samples\Demos\Hosting\Agent\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessFramework.Aspire.AppHost", "samples\Demos\ProcessFrameworkWithAspire\ProcessFramework.Aspire\ProcessFramework.Aspire.AppHost\ProcessFramework.Aspire.AppHost.csproj", "{2756FED3-ABC1-4F58-932E-5DD05A5EE066}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel.DataAnnotations;

namespace ChatWithAgent.ApiService.Config;

/// <summary>
/// Agent configuration.
/// </summary>
public sealed class AgentConfig
{
/// <summary>
/// Configuration section name.
/// </summary>
public const string ConfigSectionName = "Agent";

/// <summary>
/// Gets or sets the name of the agent.
/// </summary>
[Required]
public string Name { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the description of the agent.
/// </summary>
[Required]
public string Description { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the instructions for the agent.
/// </summary>
[Required]
public string Instructions { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

using ChatWithAgent.Configuration;
using Microsoft.Extensions.Configuration;

namespace ChatWithAgent.ApiService.Config;

/// <summary>
/// Service configuration.
/// </summary>
public sealed class ServiceConfig
{
private readonly AgentConfig _agentConfig = new();
private readonly HostConfig _hostConfig;

/// <summary>
/// Initializes a new instance of the <see cref="ServiceConfig"/> class.
/// </summary>
/// <param name="configurationManager">The configuration manager.</param>
public ServiceConfig(ConfigurationManager configurationManager)
{
configurationManager
.GetSection(AgentConfig.ConfigSectionName)
.Bind(this._agentConfig);

this._hostConfig = new HostConfig(configurationManager);
}

/// <summary>
/// Agent configuration.
/// </summary>
public AgentConfig Agent => this._agentConfig;

/// <summary>
/// Host configuration.
/// </summary>
public HostConfig Host => this._hostConfig;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ internal static void AddAzureOpenAIServices(this WebApplicationBuilder builder,
// Add AzureOpenAI chat completion service.
builder.Services.AddAzureOpenAIChatCompletion(hostConfig.AzureOpenAIChat.DeploymentName);
}

/// <summary>
/// Adds OpenAI services to the web application.
/// </summary>
/// <param name="builder">The web application builder.</param>
/// <param name="hostConfig">The host configuration.</param>
internal static void AddOpenAIServices(this WebApplicationBuilder builder, HostConfig hostConfig)
{
// Add OpenAI client.
builder.AddOpenAIClient(OpenAIChatConfig.ConnectionStringName);

// Add AzureOpenAI chat completion service.
builder.Services.AddOpenAIChatCompletion(hostConfig.OpenAIChat.ModelName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using ChatWithAgent.ApiService.Config;
using ChatWithAgent.ApiService.Extensions;
using ChatWithAgent.Configuration;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -32,11 +33,11 @@ public static void Main(string[] args)
// Add services to the container.
builder.Services.AddProblemDetails();

// Load the host configuration.
var hostConfig = new HostConfig(builder.Configuration);
// Load the service configuration.
var config = new ServiceConfig(builder.Configuration);

// Add Kernel and required AI services.
AddKernelAndServices(builder, hostConfig);
AddKernelAndAgent(builder, config);

var app = builder.Build();

Expand All @@ -50,21 +51,27 @@ public static void Main(string[] args)
app.Run();
}

private static void AddKernelAndServices(WebApplicationBuilder builder, HostConfig hostConfig)
private static void AddKernelAndAgent(WebApplicationBuilder builder, ServiceConfig config)
{
// Add Kernel.
var kernelBuilder = builder.Services.AddKernel();

switch (hostConfig.AIChatService)
switch (config.Host.AIChatService)
{
case AzureOpenAIChatConfig.ConfigSectionName:
{
builder.AddAzureOpenAIServices(hostConfig);
builder.AddAzureOpenAIServices(config.Host);
break;
}

case OpenAIChatConfig.ConfigSectionName:
{
builder.AddOpenAIServices(config.Host);
break;
}

default:
throw new NotSupportedException($"AI service '{hostConfig.AIChatService}' is not supported.");
throw new NotSupportedException($"AI service '{config.Host.AIChatService}' is not supported.");
}

// Add chat completion agent.
Expand All @@ -73,7 +80,9 @@ private static void AddKernelAndServices(WebApplicationBuilder builder, HostConf
return new ChatCompletionAgent()
{
Kernel = sp.GetRequiredService<Kernel>(),
Instructions = "TBD"
Name = config.Agent.Name,
Description = config.Agent.Description,
Instructions = config.Agent.Instructions
};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Agent": {
"Name": "AnalysisMaster",
"Description": "A highly capable agent designed to perform comprehensive analysis on various data sets and topics. It utilizes advanced algorithms and methodologies to provide accurate insights and recommendations.",
"Instructions": "1. Receive the data or topic to be analyzed. 2. Identify the type and scope of analysis required. 3. Apply appropriate analytical techniques and tools. 4. Generate detailed reports and visualizations. 5. Provide clear and actionable insights based on the analysis. 6. Ensure accuracy and reliability of the results. 7. Be ready to explain the analysis process and findings to non-experts if needed."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> bu
break;
}

case OpenAIChatConfig.ConfigSectionName:
{
// Add OpenAI chat model name to environment variables so that Api Service can access it.
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{OpenAIChatConfig.ConfigSectionName}__{nameof(config.OpenAIChat.ModelName)}", config.OpenAIChat.ModelName);
break;
}

default:
throw new NotSupportedException($"AI service '{config.AIChatService}' is not supported.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static IResourceBuilder<IResourceWithConnectionString> AddAIServices(IDistribute
return builder.AddAzureOpenAI(config);
}

case OpenAIChatConfig.ConfigSectionName:
{
// Use an existing Azure OpenAI service via connection string
return builder.AddConnectionString(OpenAIChatConfig.ConnectionStringName);
}

default:
throw new NotSupportedException($"AI service '{config.AIChatService}' is not supported.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"DeploymentName": "gpt-4o-mini",
"ModelName": "gpt-4o-mini",
"ModelVersion": "2024-07-18"
},
"OpenAIChat": {
"ModelName": "gpt-4o-mini"
}
},
"AIChatService": "AzureOpenAIChat"
}
"AIChatService": "OpenAIChat"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public sealed class HostConfig
/// </summary>
public const string AIServicesSectionName = "AIServices";

private readonly AzureOpenAIChatConfig _azureOpenAIChat = new();
private readonly ConfigurationManager _configurationManager;

private readonly AzureOpenAIChatConfig _azureOpenAIChatConfig = new();

private readonly OpenAIChatConfig _openAIChatConfig = new();

/// <summary>
/// Initializes a new instance of the <see cref="HostConfig"/> class.
Expand All @@ -25,9 +29,14 @@ public HostConfig(ConfigurationManager configurationManager)
{
configurationManager
.GetSection($"{AIServicesSectionName}:{AzureOpenAIChatConfig.ConfigSectionName}")
.Bind(this._azureOpenAIChat);
.Bind(this._azureOpenAIChatConfig);
configurationManager
.GetSection($"{AIServicesSectionName}:{OpenAIChatConfig.ConfigSectionName}")
.Bind(this._openAIChatConfig);
configurationManager
.Bind(this);

this._configurationManager = configurationManager;
}

/// <summary>
Expand All @@ -39,5 +48,10 @@ public HostConfig(ConfigurationManager configurationManager)
/// <summary>
/// The Azure OpenAI chat configuration.
/// </summary>
public AzureOpenAIChatConfig AzureOpenAIChat => this._azureOpenAIChat;
public AzureOpenAIChatConfig AzureOpenAIChat => this._azureOpenAIChatConfig;

/// <summary>
/// The OpenAI chat configuration.
/// </summary>
public OpenAIChatConfig OpenAIChat => this._openAIChatConfig;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel.DataAnnotations;

namespace ChatWithAgent.Configuration;

/// <summary>
/// OpenAI chat configuration.
/// </summary>
public sealed class OpenAIChatConfig
{
/// <summary>
/// Configuration section name.
/// </summary>
public const string ConfigSectionName = "OpenAIChat";

/// <summary>
/// The name of the connection string of the OpenAI chat service.
/// </summary>
public const string ConnectionStringName = ConfigSectionName;

/// <summary>
/// The name of the chat model.
/// </summary>
[Required]
public string ModelName { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
{
messages.Add(new Message { Sender = "User", Text = userMessage });

var prompt = userMessage;

userMessage = string.Empty;

bool agentMessageAdded = false;

var agentMessage = new Message { Sender = "Agent", Text = string.Empty };

await foreach (var update in AgentCompletionsApi.CompleteStreamingAsync(userMessage, CancellationToken.None))
await foreach (var update in AgentCompletionsApi.CompleteStreamingAsync(prompt, CancellationToken.None))
{
if (!agentMessageAdded)
{
Expand Down
Loading
Loading