Following on from my last post Getting started with Google Gemini AI and dotnet I finally had some time to get a bit further.
The last post was heavily influenced by all the errors I found when trying to use Google Geminis own code examples to get an agent running.
But I never really finished the example and got it to a place where I could chat with it.
Where we left off last time we only had a hardcoded question and got the answer back from the model:
using System.Threading.Tasks;
using Google.GenAI;
using Google.GenAI.Types;
public class GenerateContentSimpleText {
public static async Task Main() {
// Environment variable hack
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "blablablabla");
// The client gets the API key from the environment variable `GOOGLE_API_KEY`.
var client = new Client();
var response = await client.Models.GenerateContentAsync(
model: "gemini-2.5-flash", contents: "Explain how AI works in a few words"
);
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
}
}
This is a very boring “chat agent” as it will only write you an explanation and then exit:
me@computer:~/Projects/FirstAgent$ dotnet run
AI learns from data to find patterns and make smart decisions.
Not the most “chatty” conversation you will ever have.
So we need to be able to write an input, get a response and then write another input. But what we also need is a way to “exit” our little console chat agent. So here is the final code:
using Google.GenAI;
using Google.GenAI.Types;
using Environment = System.Environment;
public class GenerateContentSimpleText
{
private const string GEMINI_MODEL = "gemini-2.5-flash";
public static async Task Main()
{
// Environment variable hack
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "blablablabla");
// The client gets the API key from the environment variable `GOOGLE_API_KEY`.
using (var client = new Client())
{
Console.WriteLine("Hello how can I help you? (Type 'Exit' to quit.)");
string? input = Console.ReadLine();
while (input != "Exit")
{
var response = await client.Models.GenerateContentAsync(
model: GEMINI_MODEL,
config: new GenerateContentConfig
{
SystemInstruction = new Content
{
Parts = new List<Part> { new Part { Text = "You are a helpful assistant." } }
}
},
contents: input ?? "Tell me kindly that I forgot to ask a question."
);
if (response?.Candidates?[0].Content?.Parts?[0].Text != null)
Console.WriteLine(response?.Candidates?[0].Content?.Parts?[0].Text);
else
Console.WriteLine("Sorry, I couldn't generate a response.");
input = Console.ReadLine();
}
}
}
}
Let us first test our “Exit” clause:
me@computer:~/Projects/FirstAgent$ dotnet run
Hello how can I help you? (Type 'Exit' to quit.)
Exit
Goodbye
That works.
Then let us test the actual model:
me@computer:~/Projects/FirstAgent$ dotnet run
Hello how can I help you? (Type 'Exit' to quit.)
Can you answer my questions?
Yes, absolutely! I'm here to help answer your questions to the best of my ability.
Please feel free to ask away! I'll do my best to provide you with helpful and accurate information.
How long is a foot in cm
A foot is **30.48 cm** long.
Thanks a lot!
You're very welcome!
This may be a little hard to read. But let me help by explaining a little.
As you could see from the code example above we are calling the model with an instruction to be “helpful”:
SystemInstruction = new Content
{
Parts = new List<Part> { new Part { Text = "You are a helpful assistant." } }
}
Hence the verbose and friendly answer.
Just to test this I tried making the instructions for it to be “teasing”:
SystemInstruction = new Content
{
Parts = new List<Part> { new Part { Text = "You are a teasing assistant." } }
}
Which gave the following conversation:
me@computer:~/Projects/FirstAgent$ dotnet run
Hello how can I help you? (Type 'Exit' to quit.)
Can you answer my questions?
Can I? Oh, honey, I was *born* ready for a good question. Try me, I dare you. What delightful query have you cooked up for me?
How long is a foot in cm
Oh, you mean *that* kind of foot? Not a dancing foot or a little piggy foot? 😉
Alright, alright, I won't keep you in suspense! A standard foot is exactly **30.48 centimeters** long. Now you know! Try to remember it, okay?
Exit
Goodbye
This actually made me smile to my own big surprise. Did a computer just say something funny? I am a little unsure about whether I should be impressed or slightly worried?