- Google GenerativeAI (Gemini) π
- Introduction π
- Usage π‘
- Quick Start π
- Chat Mode π¬
- Streaming π¦
- Multimodal Capabilities with Overloaded
GenerateContentAsync
Methods π¨π΅ - Easy JSON Handling π οΈ
- Gemini Tools and Function Calling π§°
- Semantic Search Retrieval (RAG) with Google AQA π
- Coming Soon: π
- Credits π
- Explore the Wiki π
- API Reference π
Unofficial C# .Net SDK based on Google GenerativeAI (Gemini Pro) REST APIs.
This new version is a complete rewrite of the previous SDK, designed to improve performance, flexibility, and ease of
use. It seamlessly integrates with LangChain.net, providing easy methods for
JSON-based interactions and function calling with Google Gemini models.
Highlights of this release include:
- Complete Rewrite β The SDK has been entirely rebuilt for improved reliability and maintainability.
- LangChain.net Support π β Enables you to directly use this SDK within LangChain.net workflows.
- Enhanced JSON Mode π οΈ β Includes straightforward methods to handle Google Geminiβs JSON mode.
- Function Calling with Code Generator π§βπ» β Simplifies function calling by providing a source generator that creates argument classes and extension methods automatically.
- Multi-Modal Functionality π¨π΅ β Provides methods to easily incorporate text, images, and other data for multimodal operations with Google Gemini.
- Vertex AI Support π₯οΈ β Introducing direct support for Vertex AI, including multiple authentication methods such as OAuth, Service Account, and ADC (Application Default Credentials).
- New Packages π¦ β Modularizes features to help you tailor the SDK to your needs:
By merging the best of the old version with these new capabilities, the SDK provides a smoother developer experience and a wide range of features to leverage Google Gemini.
Use this library to access Google Gemini (Generative AI) models easily. You can start by installing the NuGet package and obtaining the necessary API key from your Google account.
Below are two common ways to initialize and use the SDK. For a full list of supported approaches, please refer to our Wiki Page
-
Obtain an API Key π
Visit Google AI Studio and generate your API key. -
Install the NuGet Package π¦
You can install the package via NuGet Package Manager:Install-Package Google_GenerativeAI
Or using the .NET CLI:
dotnet add package Google_GenerativeAI
-
Initialize GoogleAI βοΈ
Provide the API key when creating an instance of the GoogleAI class:var googleAI = new GoogleAI("Your_API_Key");
-
Obtain a GenerativeModel π€
Create a generative model using a model name (for example, "models/gemini-1.5-flash"):var model = googleAI.CreateGenerativeModel("models/gemini-1.5-flash");
-
Generate Content βοΈ
Call the GenerateContentAsync method to get a response:var response = await model.GenerateContentAsync("How is the weather today?"); Console.WriteLine(response.Text());
-
Full Code at a Glance πΌοΈ
var apiKey = "YOUR_GOOGLE_API_KEY"; var googleAI = new GoogleAI(apiKey); var googleModel = googleAI.CreateGenerativeModel("models/gemini-1.5-flash"); var googleResponse = await googleModel.GenerateContentAsync("How is the weather today?"); Console.WriteLine("Google AI Response:"); Console.WriteLine(googleResponse.Text()); Console.WriteLine();
-
Install the Google Cloud SDK (CLI) π
By default, Vertex AI uses Application Default Credentials (ADC). Follow Googleβs official instructions to install and set up the Google Cloud CLI. -
Initialize VertexAI βοΈ
Once the SDK is set up locally, create an instance of the VertexAI class:var vertexAI = new VertexAI();
-
Obtain a GenerativeModel π€
Just like with GoogleAI, choose a model name and create the generative model:var vertexModel = vertexAI.CreateGenerativeModel("models/gemini-1.5-flash");
-
Generate Content βοΈ
Use the GenerateContentAsync method to produce text:var response = await vertexModel.GenerateContentAsync("Hello from Vertex AI!"); Console.WriteLine(response.Text());
-
Full code at a Glance π
var vertexAI = new VertexAI(); //usage Google Cloud CLI's ADC to get the Access token var vertexModel = vertexAI.CreateGenerativeModel("models/gemini-1.5-flash"); var vertexResponse = await vertexModel.GenerateContentAsync("Hello from Vertex AI!"); Console.WriteLine("Vertex AI Response:"); Console.WriteLine(vertexResponse.Text());
For multi-turn, conversational use cases, you can start a chat session by calling the StartChat
method on an instance
of GenerativeModel
. You can use any of the previously mentioned initialization methods (environment variables, direct
constructor, configuration files, ADC, service accounts, etc.) to set up credentials for your AI service first. Then you
would:
- Create a
GenerativeModel
instance (e.g., viagoogleAI.CreateGenerativeModel(...)
orvertexAI.CreateGenerativeModel(...)
). - Call
StartChat()
on the generated model to initialize a conversation. - Use
GenerateContentAsync(...)
to exchange messages in the conversation.
Below is an example using the model name "gemini-1.5-flash":
// Example: Starting a chat session with a Google AI GenerativeModel
// 1) Initialize your AI instance (GoogleAI) with credentials or environment variables
var googleAI = new GoogleAI("YOUR_GOOGLE_API_KEY");
// 2) Create a GenerativeModel using the model name "gemini-1.5-flash"
var generativeModel = googleAI.CreateGenerativeModel("models/gemini-1.5-flash");
// 3) Start a chat session from the GenerativeModel
var chatSession = generativeModel.StartChat();
// 4) Send and receive messages
var firstResponse = await chatSession.GenerateContentAsync("Welcome to the Gemini 1.5 Flash chat!");
Console.WriteLine("First response: " + firstResponse.Text());
// Continue the conversation
var secondResponse = await chatSession.GenerateContentAsync("How can you help me with my AI development?");
Console.WriteLine("Second response: " + secondResponse.Text());
The same approach applies if youβre using Vertex AI:
// Example: Starting a chat session with a Vertex AI GenerativeModel
// 1) Initialize your AI instance (VertexAI) using one of the available authentication methods
var vertexAI = new VertexAI();
// 2) Create a GenerativeModel using "gemini-1.5-flash"
var generativeModel = vertexAI.CreateGenerativeModel("models/gemini-1.5-flash");
// 3) Start a chat
var chatSession = generativeModel.StartChat();
// 4) Send a chat message and read the response
var response = await chatSession.GenerateContentAsync("Hello from Vertex AI Chat using Gemini 1.5 Flash!");
Console.WriteLine(response.Text());
Each conversation preserves the context from previous messages, making it ideal for multi-turn or multi-step reasoning tasks. For more details, please check our Wiki.
The GenerativeAI SDK supports streaming responses, allowing you to receive and process parts of the model's output as they become available, rather than waiting for the entire response to be generated. This is particularly useful for long-running generation tasks or for creating more responsive user interfaces.
StreamContentAsync()
: Use this method for streaming text responses. It returns anIAsyncEnumerable<GenerateContentResponse>
, which you can iterate over usingawait foreach
.
Example (StreamContentAsync()
):
using GenerativeAI;
// ... (Assume model is already initialized) ...
var prompt = "Write a long story about a cat.";
await foreach (var chunk in model.StreamContentAsync(prompt))
{
Console.Write(chunk.Text); // Print each chunk as it arrives
}
Console.WriteLine(); // Newline after the complete response
Google Gemini models can work with more than just text β they can handle images, audio, and videos too! This opens up a lot of possibilities for developers. The GenerativeAI SDK makes it super easy to use these features.
Below are several examples showcasing how to incorporate files into your AI prompts:
- Directly providing a local file path.
- Referencing a remote file with its MIME type.
- Creating a request object to add multiple files (local or remote).
If you have a file available locally, simply pass in the file path:
// Generate content from a local file (e.g., an image)
var response = await geminiModel.GenerateContentAsync(
"Describe the details in this uploaded image",
@"C:\path\to\local\image.jpg"
);
Console.WriteLine(response.Text());
When your file is hosted remotely, provide the file URI and its corresponding MIME type:
// Generate content from a remote file (e.g., a PDF)
var response = await geminiModel.GenerateContentAsync(
"Summarize the information in this PDF document",
"https://example.com/path/to/sample.pdf",
"application/pdf"
);
Console.WriteLine(response.Text());
For granular control, you can create a GenerateContentRequest
, set a prompt, and attach one or more files (local or
remote) before calling GenerateContentAsync
:
// Create a request with a text prompt
var request = new GenerateContentRequest();
request.AddText("Describe what's in this document");
// Attach a local file
request.AddInlineFile(@"C:\files\example.png");
// Attach a remote file with its MIME type
request.AddRemoteFile("https://example.com/path/to/sample.pdf", "application/pdf");
// Generate the content with attached files
var response = await geminiModel.GenerateContentAsync(request);
Console.WriteLine(response.Text());
With these overloads and request-based approaches, you can seamlessly integrate additional file-based context into your prompts, enabling richer answers and unlocking more advanced AI-driven workflows.
The GenerativeAI SDK makes it simple to work with JSON data from Gemini. You have several ways some of those are:
1. Automatic JSON Handling:
-
Use
GenerateObjectAsync<T>
to directly get the deserialized object:var myObject = await model.GenerateObjectAsync<SampleJsonClass>(request);
-
Use
GenerateContentAsync
and thenToObject<T>
to deserialize the response:var response = await model.GenerateContentAsync<SampleJsonClass>(request); var myObject = response.ToObject<SampleJsonClass>();
-
Request: Use the
UseJsonMode<T>
extension method when creating yourGenerateContentRequest
. This tells the SDK to expect a JSON response of the specified type.var request = new GenerateContentRequest(); request.UseJsonMode<SampleJsonClass>(); request.AddText("Give me a really good response.");
2. Manual JSON Parsing:
-
Request: Create a standard
GenerateContentRequest
.var request = new GenerateContentRequest(); request.AddText("Give me some JSON.");
or
var request = new GenerateContentRequest(); request.GenerationConfig = new GenerationConfig() { ResponseMimeType = "application/json", ResponseSchema = new SampleJsonClass() } request.AddText("Give me a really good response.");
-
Response: Use
ExtractJsonBlocks()
to get the raw JSON blocks from the response, and then useToObject<T>
to deserialize them.var response = await model.GenerateContentAsync(request); var jsonBlocks = response.ExtractJsonBlocks(); var myObjects = jsonBlocks.Select(block => block.ToObject<SampleJsonClass>());
These options give you flexibility in how you handle JSON data with the GenerativeAI SDK.
Read the wiki for more options.
The GenerativeAI SDK provides built-in tools to enhance Gemini's capabilities, including Google Search, Google Search Retrieval, and Code Execution. These tools allow Gemini to interact with the outside world and perform actions beyond generating text.
1. Inbuilt Tools (GoogleSearch, GoogleSearchRetrieval, and Code Execution):
You can easily enable or disable these tools by setting the corresponding properties on the GenerativeModel
:
UseGoogleSearch
: Enables or disables the Google Search tool.UseGrounding
: Enables or disables the Google Search Retrieval tool (often used for grounding responses in factual information).UseCodeExecutionTool
: Enables or disables the Code Execution tool.
// Example: Enabling Google Search and Code Execution
var model = new GenerativeModel(apiKey: "YOUR_API_KEY");
model.UseGoogleSearch = true;
model.UseCodeExecutionTool = true;
// Example: Disabling all inbuilt tools.
var model = new GenerativeModel(apiKey: "YOUR_API_KEY");
model.UseGoogleSearch = false;
model.UseGrounding = false;
model.UseCodeExecutionTool = false;
2. Function Calling π§
Function calling lets you integrate custom functionality with Gemini by defining functions it can call. This requires
the GenerativeAI.Tools
package.
-
Setup:
- Define an interface for your functions, using the
[GenerateJsonSchema()]
attribute. - Implement the interface.
- Create
tools
andcalls
usingAsTools()
andAsCalls()
. - Create a
GenericFunctionTool
instance. - Add the tool to your
GenerativeModel
withAddFunctionTool()
.
- Define an interface for your functions, using the
-
FunctionCallingBehaviour
: Customize behavior (e.g., auto-calling, error handling) using theGenerativeModel
'sFunctionCallingBehaviour
property:FunctionEnabled
(default:true
): Enables/disables function calling.AutoCallFunction
(default:true
): Gemini automatically calls functions.AutoReplyFunction
(default:true
): Gemini automatically generates responses after function calls.AutoHandleBadFunctionCalls
(default:false
): Attempts to handle errors from incorrect calls
// Install-Package GenerativeAI.Tools
using GenerativeAI;
using GenerativeAI.Tools;
[GenerateJsonSchema()]
public interface IWeatherFunctions // Simplified Interface
{
[Description("Get the current weather")]
Weather GetCurrentWeather(string location);
}
public class WeatherService : IWeatherFunctions
{ // ... (Implementation - see full example in wiki) ...
public Weather GetCurrentWeather(string location)
=> new Weather
{
Location = location,
Temperature = 30.0,
Unit = Unit.Celsius,
Description = "Sunny",
};
}
// --- Usage ---
var service = new WeatherService();
var tools = service.AsTools();
var calls = service.AsCalls();
var tool = new GenericFunctionTool(tools, calls);
var model = new GenerativeModel(apiKey: "YOUR_API_KEY");
model.AddFunctionTool(tool);
//Example for FunctionCallingBehaviour
model.FunctionCallingBehaviour = new FunctionCallingBehaviour { AutoCallFunction = false }; // Example
var result = await model.GenerateContentAsync("Weather in SF?");
Console.WriteLine(result.Text);
For more details and options, see the wiki.
The Google_GenerativeAI
library makes implementing Retrieval-Augmented Generation (RAG) incredibly easy. RAG combines the strengths of Large Language Models (LLMs) with the precision of information retrieval. Instead of relying solely on the LLM's pre-trained knowledge, a RAG system first retrieves relevant information from a knowledge base (a "corpus" of documents) and then uses that information to augment the LLM's response. This allows the LLM to generate more accurate, factual, and context-aware answers.
This library leverages Google's Attributed Question Answering (AQA) model, which is specifically designed for semantic search and question answering. AQA excels at understanding the intent behind a question and finding the most relevant passages within a corpus to answer it. Key features include:
- Semantic Understanding: AQA goes beyond simple keyword matching. It understands the meaning of the query and the documents.
- Attribution: AQA provides an "Answerable Probability" score, indicating its confidence in the retrieved answer.
- Easy Integration: The
Google_GenerativeAI
library provides a simple API to create corpora, add documents, and perform semantic searches.
For a step-by-step tutorial on implementing Semantic Search Retrieval with Google AQA, see the wiki page.
The following features are planned for future releases of the GenerativeAI SDK:
- Semantic Search Retrieval (RAG π): Use Gemini as a Retrieval-Augmented Generation (RAG) system, allowing it to incorporate information from external sources into its responses. (Released on 20th Feb, 2025)
- Image Generation π¨: Generate images with imagen from text prompts, expanding Gemini's capabilities beyond text and code.
- Multimodal Live APIποΈ: Bidirectional Multimodal Live Chat with Gemini 2.0 Flash
- Model Tuning ποΈ: Customize Gemini models to better suit your specific needs and data.
Thanks to HavenDV for LangChain.net SDK
Dive deeper into the GenerativeAI SDK! The wiki is your comprehensive resource for:
- Detailed Guides π: Step-by-step tutorials on various features and use cases.
- Advanced Usage π οΈ: Learn about advanced configuration options, error handling, and best practices.
- Complete Code Examples π»: Find ready-to-run code snippets and larger project examples.
We encourage you to explore the wiki to unlock the full potential of the GenerativeAI SDK! π
Feel free to open an issue or submit a pull request if you encounter any problems or want to propose improvements! Your feedback helps us continue to refine and expand this SDK.