⚠ Deprecated: Legacy
Suggested alternative: tryAGI.OpenAI
C# Source Generator which allows you to define functions natively through a C# interface, and also provides extensions that make it easier to call this interface later
$ dotnet add package OpenAI.FunctionsC# Source Generator which allows you to define functions natively through a C# interface,
and also provides extensions that make it easier to call this interface later.
In addition to easy function implementation and readability,
it generates argument helper Args classes, extension methods to easily pass a function description,
and extension methods to simply call a function via json and return json.
Currently only System.Text.Json is supported.
NOTE: When installing via NuGet just replace
<IncludeAssets>runtimewith<IncludeAssets>compilefor it to work properly.
public enum Unit
{
Celsius,
Fahrenheit,
}
public class Weather
{
public string Location { get; set; } = string.Empty;
public double Temperature { get; set; }
public Unit Unit { get; set; }
public string Description { get; set; } = string.Empty;
}
[OpenAiFunctions]
public interface IWeatherFunctions
{
[Description("Get the current weather in a given location")]
public Task<Weather> GetCurrentWeatherAsync(
[Description("The city and state, e.g. San Francisco, CA")] string location,
Unit unit = Unit.Celsius,
CancellationToken cancellationToken = default);
}
public class WeatherService : IWeatherFunctions
{
public Task<Weather> GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default)
{
return Task.FromResult(new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
});
}
}
// Integration on the example of OpenAI-DotNet:
var api = new OpenAIClient(new OpenAIAuthentication(apiKey));
var service = new WeatherService();
var getCurrentWeather = service.GetCurrentWeatherAsParametersJsonNode();
var functions = new List<Function>
{
new (getCurrentWeather.Name, getCurrentWeather.Description, getCurrentWeather.Node)
};
var chatRequest = new ChatRequest(messages, functions: functions, functionCall: "auto", model: "gpt-3.5-turbo-0613");
// ...
var json = service.CallGetCurrentWeather(result.FirstChoice.Message.Function.Arguments.ToString());
Priority place for bugs: https://github.com/tryAGI/OpenAI.Functions/issues
Priority place for ideas and general questions: https://github.com/OpenAI.Functions/LangChain/discussions
Discord: https://discord.gg/Ca2xhfBf3v