microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/samples/ExtAIBot/LocalTools.cs
56lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ComponentModel; |
| 5 | using System.Text.Json; |
| 6 | using System.Text.Json.Serialization; |
| 7 | using Microsoft.Extensions.AI; |
| 8 | using Microsoft.Teams.Cards; |
| 9 | |
| 10 | namespace ExtAIBot; |
| 11 | |
| 12 | // Provides local AIFunction definitions that the model can call during a turn. |
| 13 | internal static class LocalTools |
| 14 | { |
| 15 | // Returns a fresh AIFunction each turn; pendingCards is a per-turn accumulator |
| 16 | // captured by closure. |
| 17 | |
| 18 | private static readonly JsonSerializerOptions SerializerOptions = new() |
| 19 | { |
| 20 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 21 | }; |
| 22 | public static AIFunction CreateClarificationCardTool(IList<object> pendingCards, ILogger logger) => |
| 23 | AIFunctionFactory.Create( |
| 24 | ([Description("The clarification question to ask the user.")] string question, |
| 25 | [Description("2–4 candidate interpretations the user can pick between.")] string[] options) => |
| 26 | { |
| 27 | logger.LogInformation("[tool] request_clarification(question={Question}, options=[{Options}])", |
| 28 | question, string.Join(", ", options)); |
| 29 | pendingCards.Add(BuildClarificationCard(question, options)); |
| 30 | return "Clarification card attached."; |
| 31 | }, |
| 32 | "request_clarification", |
| 33 | "Show an Adaptive Card asking the user to clarify their request when needed. " + |
| 34 | "The user picks one option and submits; their choice arrives as the next user turn."); |
| 35 | |
| 36 | private static JsonElement BuildClarificationCard(string question, string[] options) |
| 37 | { |
| 38 | AdaptiveCard card = new AdaptiveCard( |
| 39 | new TextBlock(question) |
| 40 | .WithSize(TextSize.Medium) |
| 41 | .WithWeight(TextWeight.Bolder) |
| 42 | .WithWrap(true), |
| 43 | new ChoiceSetInput([.. options.Select(o => new Choice { Title = o, Value = o })]) |
| 44 | .WithId("clarificationChoice") |
| 45 | .WithIsRequired(true) |
| 46 | .WithErrorMessage("Please pick one option.")) |
| 47 | .WithVersion(Microsoft.Teams.Cards.Version.Version1_6) |
| 48 | .WithActions( |
| 49 | new ExecuteAction() |
| 50 | .WithTitle("Submit") |
| 51 | .WithVerb("clarification") |
| 52 | .WithAssociatedInputs(AssociatedInputs.Auto)); |
| 53 | |
| 54 | return JsonSerializer.SerializeToElement(card, SerializerOptions); |
| 55 | } |
| 56 | } |