microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/samples/AFBot/Program.cs
41lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ClientModel; |
| 5 | using AFBot; |
| 6 | using Azure.AI.OpenAI; |
| 7 | using Azure.Monitor.OpenTelemetry.AspNetCore; |
| 8 | using Microsoft.Agents.AI; |
| 9 | using Microsoft.Bot.Core; |
| 10 | using Microsoft.Bot.Core.Hosting; |
| 11 | using Microsoft.Bot.Core.Schema; |
| 12 | using OpenAI; |
| 13 | |
| 14 | WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 15 | |
| 16 | WebApplicationBuilder webAppBuilder = WebApplication.CreateSlimBuilder(args); |
| 17 | webAppBuilder.Services.AddOpenTelemetry().UseAzureMonitor(); |
| 18 | webAppBuilder.Services.AddBotApplication<BotApplication>(); |
| 19 | WebApplication webApp = webAppBuilder.Build(); |
| 20 | BotApplication botApp = webApp.UseBotApplication<BotApplication>(); |
| 21 | |
| 22 | AzureOpenAIClient azureClient = new( |
| 23 | new Uri("https://ridofoundry.cognitiveservices.azure.com/"), |
| 24 | new ApiKeyCredential(Environment.GetEnvironmentVariable("AZURE_OpenAI_KEY")!)); |
| 25 | |
| 26 | ChatClientAgent agent = azureClient.GetChatClient("gpt-5-nano").CreateAIAgent( |
| 27 | instructions: "You are an expert acronym maker, made an acronym made up from the first three characters of the user's message. " + |
| 28 | "Some examples: OMW on my way, BTW by the way, TVM thanks very much, and so on." + |
| 29 | "Always respond with the three complete words only, and include a related emoji at the end.", |
| 30 | name: "AcronymMaker"); |
| 31 | |
| 32 | botApp.Use(new DropTypingMiddleware()); |
| 33 | botApp.OnActivity = async (activity, cancellationToken) => |
| 34 | { |
| 35 | await botApp.SendTypingActivityAsync(activity, cancellationToken); |
| 36 | AgentRunResponse res = await agent.RunAsync(activity?.Text ?? "OMW"); |
| 37 | CoreActivity reply = activity!.CreateReplyMessageActivity(res.Text); |
| 38 | await botApp.SendActivityAsync(reply, cancellationToken); |
| 39 | }; |
| 40 | |
| 41 | webApp.Run(); |