microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.6

Branches

Tags

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

Clone

HTTPS

Download ZIP

Samples/Samples.AI/Handlers/FunctionCallingHandler.cs

100lines · modeblame

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