microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Samples/Samples.AI/Handlers/FunctionCallingHandler.cs
100lines · modeblame
19e4df96Aamir Jawaid9 months ago | 1 | using System.Text.Json; |
b6c66549Alex Acebo8 months ago | 2 | |
19e4df96Aamir Jawaid9 months ago | 3 | using Microsoft.Teams.AI.Annotations; |
| 4 | using Microsoft.Teams.AI.Models.OpenAI; | |
| 5 | using Microsoft.Teams.AI.Prompts; | |
| 6 | using Microsoft.Teams.AI.Templates; | |
| 7 | using Microsoft.Teams.Api.Activities; | |
| 8 | using Microsoft.Teams.Apps; | |
| 9 | | |
| 10 | namespace Samples.AI.Handlers; | |
| 11 | | |
| 12 | public static class FunctionCallingHandler | |
| 13 | { | |
| 14 | /// <summary> | |
| 15 | /// Handle Pokemon search using PokeAPI | |
| 16 | /// </summary> | |
| 17 | public static async Task<string> PokemonSearchFunction([Param("pokemon_name")] string pokemonName) | |
| 18 | { | |
| 19 | Console.WriteLine($"[FUNCTION] pokemon_search called with pokemon_name='{pokemonName}'"); | |
| 20 | | |
| 21 | try | |
| 22 | { | |
| 23 | using var client = new HttpClient(); | |
| 24 | Console.WriteLine($"[FUNCTION] Fetching Pokemon data from PokeAPI for '{pokemonName}'..."); | |
| 25 | | |
| 26 | var response = await client.GetAsync($"https://pokeapi.co/api/v2/pokemon/{pokemonName.ToLower()}"); | |
| 27 | | |
| 28 | if (!response.IsSuccessStatusCode) | |
| 29 | { | |
| 30 | Console.WriteLine($"[FUNCTION] Pokemon '{pokemonName}' not found (status: {response.StatusCode})"); | |
| 31 | return $"Pokemon '{pokemonName}' not found"; | |
| 32 | } | |
| 33 | else | |
| 34 | { | |
| 35 | Console.WriteLine($"[FUNCTION] Successfully retrieved data for Pokemon '{pokemonName}'"); | |
| 36 | } | |
| 37 | | |
| 38 | var json = await response.Content.ReadAsStringAsync(); | |
| 39 | var data = JsonDocument.Parse(json); | |
| 40 | var root = data.RootElement; | |
| 41 | | |
| 42 | var name = root.GetProperty("name").GetString(); | |
| 43 | var height = root.GetProperty("height").GetInt32(); | |
| 44 | var weight = root.GetProperty("weight").GetInt32(); | |
| 45 | var types = root.GetProperty("types") | |
| 46 | .EnumerateArray() | |
| 47 | .Select(t => t.GetProperty("type").GetProperty("name").GetString()) | |
| 48 | .ToList(); | |
| 49 | | |
| 50 | var result = $"Pokemon {name}: height={height}, weight={weight}, types={string.Join(", ", types)}"; | |
| 51 | Console.WriteLine($"[FUNCTION] Successfully retrieved Pokemon data: {result}"); | |
| 52 | | |
| 53 | return result; | |
| 54 | } | |
| 55 | catch (Exception ex) | |
| 56 | { | |
| 57 | Console.WriteLine($"[FUNCTION] Error searching for Pokemon: {ex.Message}"); | |
| 58 | return $"Error searching for Pokemon: {ex.Message}"; | |
| 59 | } | |
| 60 | } | |
| 61 | | |
| 62 | /// <summary> | |
| 63 | /// Handle single function calling - Pokemon search | |
| 64 | /// </summary> | |
2a3ae203Rido6 months ago | 65 | public static async Task HandlePokemonSearch(OpenAIChatModel model, IContext<MessageActivity> context, CancellationToken cancellationToken = default) |
19e4df96Aamir Jawaid9 months ago | 66 | { |
| 67 | Console.WriteLine($"[HANDLER] Pokemon search handler invoked with text: '{context.Activity.Text}'"); | |
| 68 | | |
| 69 | var prompt = new OpenAIChatPrompt(model, new ChatPromptOptions | |
| 70 | { | |
| 71 | Instructions = new StringTemplate("You are a helpful assistant that can look up Pokemon for the user.") | |
| 72 | }); | |
| 73 | | |
| 74 | // Register the pokemon search function | |
| 75 | prompt.Function( | |
| 76 | "pokemon_search", | |
| 77 | "Search for pokemon information including height, weight, and types", | |
| 78 | PokemonSearchFunction | |
| 79 | ); | |
| 80 | | |
| 81 | Console.WriteLine("[HANDLER] Registered pokemon_search function, sending prompt to AI..."); | |
2a3ae203Rido6 months ago | 82 | var result = await prompt.Send(context.Activity.Text, cancellationToken); |
19e4df96Aamir Jawaid9 months ago | 83 | |
| 84 | if (result.Content != null) | |
| 85 | { | |
| 86 | Console.WriteLine($"[HANDLER] AI response received: {result.Content}"); | |
| 87 | var message = new MessageActivity | |
| 88 | { | |
| 89 | Text = result.Content, | |
| 90 | }.AddAIGenerated(); | |
2a3ae203Rido6 months ago | 91 | await context.Send(message, cancellationToken); |
19e4df96Aamir Jawaid9 months ago | 92 | } |
| 93 | else | |
| 94 | { | |
| 95 | Console.WriteLine("[HANDLER] No content received from AI"); | |
2a3ae203Rido6 months ago | 96 | await context.Reply("Sorry I could not find that pokemon", cancellationToken); |
19e4df96Aamir Jawaid9 months ago | 97 | } |
| 98 | } | |
| 99 | | |
b6c66549Alex Acebo8 months ago | 100 | } |