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