The article explains how install and run Google Generative AI (Gemini) in .NET interactive notebook.
Generative AI can help us create new content. Generative AI builds on existing technologies, like large language models (LLMs) which are trained on large amounts of text and learn to predict the next word in a sentence (source).
You can find a lot of Google Generative AI examples on the Internet done in the Python + Google Colab environment, but it’s difficult to find the same examples in the C# + .NET Interactive environment, so I decided to make such examples for the .NET fans.
To learn how to use the .NET Interactive Notebook in Visual Studio Code, follow the link.
How To Get API KEY for Gemini
The Gemini API gives you access to Gemini models created by Google DeepMind. To obtain an API key, enter in a browser’s address https://aistudio.google.com/
. You expect to see this window:
Click on the menu item Get API key
on the left sidebar. A new interactive window will appear:
Click on the button Create API key
. The system will create an API key and display a result window where you can copy the generated key by clicking on the truncated key text and then on the copy symbol:
Use API keys in a secure manner. Avoid sharing or embedding them in code that is accessible to the public. My recommendation is to store your API key as a PC environment variable and retrieve it in code using an appropriate function.
How To Install Google Generative AI SDK
For demonstration purposes, I decided to use the NuGet package Google_GenerativeAI (Gemini). It is an unofficial C# SDK based on Google Generative AI (Gemini Pro) REST APIs.
First of all, install the NuGet package to use Gemini SDK in the .NET Interactive notebook by entering this code line in the notebook:
#r "nuget:Google_GenerativeAI"
You can expect to get the response:
Installed Packages
Google_GenerativeAI, 0.1.20
I decided to make a useful prompt for Gemini from our everyday life. I think everyone heard about blue vitriol and quicklime, which are materials to make a Bordeaux mixture that is used in vineyards, fruit farms, and gardens to prevent infestations. But we must know what will happen if we saturate them in the water. Who knows?
Let us ask about it Gemini. Enter in the notebook the code below, and we will get the answer.
Enter this code to use appropriate libraries and run it:
using System;
using System.IO;
using GenerativeAI;
using GenerativeAI.Models;
using GenerativeAI.Types;
Then enter the final code block:
string apiKey = Environment.OSVersion.Platform == PlatformID.Win32NT
? Environment.GetEnvironmentVariable("GEM_API_KEY", EnvironmentVariableTarget.User)
: null;
var model = new GenerativeModel(apiKey);
var chat = model.StartChat(new StartChatParams());
var result = await chat.SendMessageAsync("Write copperas formula");
Console.WriteLine("Copperas formula\r\n");
Console.WriteLine(result);
var result2 = await chat.SendMessageAsync("Write lime formula");
Console.WriteLine("Lime formula\r\n");
Console.WriteLine(result2);
var result3 = await chat.SendMessageAsync("What happens if you saturate blue vitriol with quicklime in the water?");
Console.WriteLine("Result of saturating blue vitriol and quicklime in water will be:\r\n");
Console.WriteLine(result3);
Run the code to get this answer:
Copperas formula
FeSO₄·7H₂O
Lime formula
CaO
Lime is also known as calcium oxide. The chemical formula for calcium oxide is CaO.
Result of saturating blue vitriol and quicklime in water will be:
**Reaction:**
When blue vitriol (copper sulfate pentahydrate, CuSO₄·5H₂O) is saturated with quicklime (calcium oxide, CaO) in water, the following reaction occurs:
```
CuSO₄·5H₂O(aq) + CaO(s) → Cu(OH)₂(s) + CaSO₄(aq)
```
**Observations:**
* A light blue precipitate of copper hydroxide [Cu(OH)₂] forms.
* The solution turns colorless due to the formation of calcium sulfate [CaSO₄], which is soluble in water.
**Explanation:**
...
**Note:**
* The reaction is exothermic, meaning that it releases heat.
* The precipitate of copper hydroxide is gelatinous and can be filtered out of the solution.
The mixture releases heat, so we can be careful with it.
Was this helpful?
1 / 0