FunctionCallingService

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public enum WeatherUnit
{
    Celsius,
    Fahrenheit,
}

[GenerateJsonSchema(Strict = false)]
public interface IFunctionCallingService
{
    [Description("Get the user's current location")]
    public Task<string> GetCurrentLocation(
        CancellationToken cancellationToken = default);

    [Description("Get the current weather in a given location")]
    public Task<string> GetCurrentWeatherAsync(
        [Description("The city and state, e.g. Boston, MA")]
        string location,
        [Description("The temperature unit to use. Infer this from the specified location.")]
        WeatherUnit unit = WeatherUnit.Celsius,
        CancellationToken cancellationToken = default);
}

public class FunctionCallingService : IFunctionCallingService
{
    public Task<string> GetCurrentLocation(
        CancellationToken cancellationToken = default)
    {
        // Call the location API here.
        return Task.FromResult("San Francisco");
    }

    public Task<string> GetCurrentWeatherAsync(
        string location,
        WeatherUnit unit = WeatherUnit.Celsius,
        CancellationToken cancellationToken = default)
    {
        // Call the weather API here.
        return Task.FromResult($"31 {unit:G}");
    }
}