var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddTransient<IWeatherService, WeatherService>();
builder.Services.AddHttpContextAccessor();
var apiKey = builder.Configuration["ApiKey"]
?? throw new InvalidOperationException("Configuration value 'ApiKey' is missing.");
var postgresConnectionString = builder.Configuration["ConnectionString"]
?? throw new InvalidOperationException("Configuration value 'ConnectionString' is missing.");
var openAIClient = new ChatClient(
model: "gpt-5-mini",
credential: new ApiKeyCredential(apiKey),
options: new OpenAIClientOptions()
{
Endpoint = new Uri("myendpoint")
}
).AsIChatClient();
var serviceProvider = builder.Services.BuildServiceProvider();
var weatherTool = serviceProvider?.GetRequiredService<IWeatherService>();
//builder.Services.AddChatClient(openAIClient);
//var agentBuilder = builder.AddAIAgent("Alex", "You are a helpful assistant.", openAIClient)
// .WithAITools(AIFunctionFactory.Create(weatherTool.Get, name: "get_weather")) ;
var codeStyleSkill = new AgentInlineSkill(
name: "code-style",
description: "Coding style guidelines and conventions for the team",
instructions: """
Use this skill when answering questions about coding style, conventions, or best practices for the team.
1. Read the style-guide resource for the full set of rules.
2. Answer based on those rules, quoting the relevant guideline where helpful.
""")
.AddResource(
"style-guide",
"""
# Team Coding Style Guide
- Use 4-space indentation (no tabs)
- Maximum line length: 120 characters
- Use type annotations on all public methods
""");
var botInfoSkill = new AgentInlineSkill(
name: "bot-info",
description: "Basic agent configuration info used to test skill functionality",
instructions: """
Use this skill when the user asks about the bot's name, version, or environment.
1. Read the data from the 'bot-config' resource.
2. Answer accurately based on that resource.
""")
.AddResource(
"bot-config",
"""
Agent Name: TestBot-01
Version: 1.0.0
Environment: Staging
""");
var skillsProvider = new AgentSkillsProvider(codeStyleSkill, botInfoSkill);
var agentBuilder =builder.AddAIAgent("Alex", (sp, key) =>
{
return openAIClient.AsAIAgent(new ChatClientAgentOptions()
{
Name = "Alex",
ChatOptions = new ChatOptions()
{
Instructions = "You are a helpful assistant.",
Tools = new List<AITool>()
{
AIFunctionFactory.Create(weatherTool.Get, name: "get_weather")
}
},
AIContextProviders = [skillsProvider],
// ChatHistoryProvider = new DatabaseChatHistoryProvider() <--- Please add example for this
})
.AsBuilder()
.UseToolApproval(new ToolApprovalAgentOptions
{
AutoApprovalRules = [AgentSkillsProvider.AllToolsAutoApprovalRule],
})
.Build()
;
});
builder.Services.AddOpenAIResponses();
builder.Services.AddOpenAIConversations();
builder.Services.AddOpenAIChatCompletions();
//if (builder.Environment.IsDevelopment())
//{
builder.AddDevUI(options =>
{
options.AllowRemoteAccess = true;
});
//}
var app = builder.Build();
app.MapOpenAIResponses();
app.MapOpenAIConversations();
app.MapOpenAIChatCompletions(agentBuilder);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.MapDevUI();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Description
Dear all,
I am looking to build an agent that is accessible through a .NET API. I also want to store the user conversation in a database by conversation id, but I have not been able to find a working example for this (tested with DevUI).
Any help would be greatly appreciated.
Thank you.
Regards
Code Sample
Language/SDK
.NET