microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
core/sso-in-channels

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/samples/ExtAIBot/Program.cs

43lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.ClientModel;
5using Azure.AI.OpenAI;
6using ExtAIBot;
7using Microsoft.Extensions.AI;
8using Microsoft.Teams.Apps;
9
10// Wires up the Teams bot application and delegates AI execution to Agent.
11// Handler registration lives in TeamsBotAppHandlers.cs.
12
13WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
14builder.Services.AddTeamsBotApplication();
15
16builder.Services.AddSingleton<IChatClient>(sp =>
17{
18 IConfiguration config = sp.GetRequiredService<IConfiguration>();
19 string endpoint = config["AzureOpenAI:Endpoint"] ?? throw new InvalidOperationException("AzureOpenAI:Endpoint is required.");
20 string apiKey = config["AzureOpenAI:ApiKey"] ?? throw new InvalidOperationException("AzureOpenAI:ApiKey is required.");
21 string modelId = config["AzureOpenAI:ModelId"] ?? throw new InvalidOperationException("AzureOpenAI:ModelId is required.");
22
23 return new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey))
24 .GetChatClient(modelId)
25 .AsIChatClient()
26 .AsBuilder()
27 .UseFunctionInvocation()
28 .Build();
29});
30
31builder.Services.AddSingleton<McpToolSetLifetimeService>();
32builder.Services.AddHostedService(sp => sp.GetRequiredService<McpToolSetLifetimeService>());
33
34builder.Services.AddSingleton<Agent>();
35
36WebApplication webApp = builder.Build();
37
38Agent agent = webApp.Services.GetRequiredService<Agent>();
39ILogger handlerLogger = webApp.Services.GetRequiredService<ILoggerFactory>().CreateLogger("ExtAIBot.TeamsBotAppHandlers");
40
41webApp.UseTeamsBotApplication().RegisterHandlers(agent, handlerLogger);
42
43webApp.Run();
44