microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/ExtAIBot/Program.cs
37lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ClientModel; |
| 5 | using Azure.AI.OpenAI; |
| 6 | using ExtAIBot; |
| 7 | using Microsoft.Extensions.AI; |
| 8 | using Microsoft.Teams.Apps; |
| 9 | |
| 10 | // Wires up the Teams bot application. Handler registration lives in ExtAIBotApp. |
| 11 | |
| 12 | WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args); |
| 13 | builder.Services.AddTeamsBotApplication<ExtAIBotApp>(); |
| 14 | |
| 15 | builder.Services.AddSingleton<IChatClient>(sp => |
| 16 | { |
| 17 | IConfiguration config = sp.GetRequiredService<IConfiguration>(); |
| 18 | string endpoint = config["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("AzureOpenAI:Endpoint is required."); |
| 19 | string apiKey = config["AzureOpenAI:ApiKey"] ?? throw new InvalidOperationException("AzureOpenAI:ApiKey is required."); |
| 20 | string deployment = config["AzureOpenAI:Deployment"] ?? throw new InvalidOperationException("AzureOpenAI:Deployment is required."); |
| 21 | |
| 22 | return new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)) |
| 23 | .GetChatClient(deployment) |
| 24 | .AsIChatClient() |
| 25 | .AsBuilder() |
| 26 | .UseFunctionInvocation() |
| 27 | .Build(); |
| 28 | }); |
| 29 | |
| 30 | builder.Services.AddSingleton<McpToolSetLifetimeService>(); |
| 31 | builder.Services.AddHostedService(sp => sp.GetRequiredService<McpToolSetLifetimeService>()); |
| 32 | |
| 33 | builder.Services.AddSingleton<Agent>(); |
| 34 | |
| 35 | WebApplication webApp = builder.Build(); |
| 36 | webApp.UseTeamsBotApplication<ExtAIBotApp>(); |
| 37 | webApp.Run(); |
| 38 | |