Google_GenerativeAI.Tools
2.0.7
See the version list below for details.
dotnet add package Google_GenerativeAI.Tools --version 2.0.7
NuGet\Install-Package Google_GenerativeAI.Tools -Version 2.0.7
<PackageReference Include="Google_GenerativeAI.Tools" Version="2.0.7" />
paket add Google_GenerativeAI.Tools --version 2.0.7
#r "nuget: Google_GenerativeAI.Tools, 2.0.7"
// Install Google_GenerativeAI.Tools as a Cake Addin #addin nuget:?package=Google_GenerativeAI.Tools&version=2.0.7 // Install Google_GenerativeAI.Tools as a Cake Tool #tool nuget:?package=Google_GenerativeAI.Tools&version=2.0.7
Google_GenerativeAI.Tools
Google_GenerativeAI.Tools
is a library that provides concrete implementations of function tools for use with the Google Generative AI SDK (specifically the unofficial C# SDK Google_GenerativeAI). It simplifies the process of integrating function calling capabilities (similar to OpenAI's function calling) into your Gemini-powered applications. This project extends the functionality of the IFunctionTool
interface defined in the main SDK.
Key Features
- Easy Function Tool Creation: Quickly create function tools from existing C# classes and methods using the provided extension methods.
- Automatic Schema Generation: Leverages the
[GenerateJsonSchema()]
attribute (from theGoogle_GenerativeAI
SDK) to automatically generate the required JSON schema for your functions. - Synchronous and Asynchronous Support: Handles both synchronous and asynchronous methods.
- Seamless Integration: Works directly with the
GenerativeModel
class from theGoogle_GenerativeAI
SDK. - Enum Support: Correctly handles enum parameters in your function definitions.
Getting Started
1. Installation
Install the Google_GenerativeAI.Tools
NuGet package:
dotnet add package Google_GenerativeAI.Tools
2. Define Your Functions
Create a C# interface and class that defines the functions you want to expose to the Gemini model. Use the [Description]
attribute to provide descriptions for the functions and parameters. Use the [GenerateJsonSchema()]
attribute on the interface.
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using GenerativeAI; // From the main Google_GenerativeAI SDK
using GenerativeAI.Tools; // From this library
public enum Unit
{
Celsius,
Fahrenheit,
Imperial
}
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;
}
[GenerateJsonSchema()]
public interface IWeatherFunctions
{
[Description("Get the current weather in a given location")]
public Weather GetCurrentWeather(
[Description("The city and state, e.g. San Francisco, CA")]
string location,
Unit unit = Unit.Celsius);
[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 Weather GetCurrentWeather(string location, Unit unit = Unit.Celsius)
{
// In a real application, you'd fetch the weather data here.
return new Weather
{
Location = location,
Temperature = 30.0,
Unit = unit,
Description = "Sunny",
};
}
public Task<Weather> GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius,
CancellationToken cancellationToken = default)
{
// In a real application, you'd fetch the weather data asynchronously here.
return Task.FromResult(new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
});
}
}
3. Create the Function Tool
Use the extension methods provided by GenerativeAI.Tools to create a GenericFunctionTool
instance.
WeatherService service = new WeatherService();
var tools = service.AsTools(); // Creates the Tool instances.
var calls = service.AsCalls(); // Creates the delegate map for function calls.
var tool = new GenericFunctionTool(tools, calls); // Combines them into a usable tool.
4. Integrate with the GenerativeModel
Create a GenerativeModel
instance and add the function tool.
// Assuming you have a method to get your API key.
string apiKey = GetTestGooglePlatform();
var model = new GenerativeModel(apiKey, GoogleAIModels.DefaultGeminiModel);
model.AddFunctionTool(tool);
5. Make Requests
You can now use tool.GenerateToolResponseAsync
or tool.GenerateToolResponse
for getting response from the tool.
var request = new GenerateContentRequest();
request.AddText("What is the weather in san francisco today?");
var response = await model.GenerateToolResponseAsync(request);
var result = await model.GenerateContentAsync(response.Text());
Or you can simply send a prompt to the model.
var result = await model.GenerateContentAsync("What is the weather in San Francisco today?");
Console.WriteLine(result.Text());
Dependencies
- Google_GenerativeAI (Unofficial C# Google Generative AI SDK)
Contributing
Contributions are welcome! Please submit pull requests or open issues to discuss proposed changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
.NETStandard 2.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
net5.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
net6.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
net7.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
net8.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
-
net9.0
- CSharpToJsonSchema (>= 3.10.1)
- Google_GenerativeAI (>= 2.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.