Introduction
When you’re building a Large Language Model (LLMs) application, you typically start with the SDK of the LLM you’re trying to talk to. However, at some point, it might make sense to start using a higher level framework. This is especially true if you rely on multiple LLMs from different vendors. Instead of learning and using SDKs from multiple vendors, you can learn a higher level framework and use that to orchestrate your calls to multiple LLMs. These frameworks also have useful abstractions beyond simple LLM calls that accelerate LLM application development.
LangChain is probably the most popular LLM framework out there. It’s mainly focused on Python with JavaScript and Java flavours (via LangChain4j). More LLM frameworks popping up nowadays and Semantic Kernel is one of them and the topic of this blog post.
What is Semantic Kernel?
Semantic Kernel is an open-source development kit from Microsoft. Its goal is let you easily build AI agents and integrate the latest AI models into your applications, similar to LangChain. It currently supports C#, Python, and Java.
At this point, you might be wondering: What’s the difference between LangChain and Semantic Kernel?. They’re similar but different in details. I’ll point you to this excellent blog post from Jane Huang who deep dives into differences: A comparative overview of LangChain, Semantic Kernel, AutoGen and more.
In the rest of the blog post, I will show you how to get a chat application working with Semantic Kernel and Gemini using C#.
Gemini on Google AI and Vertex AI
Gemini is the LLM from Google and currently, you can access it via Google AI or Vertex AI of Google Cloud. You can use Semantic Kernel to access both.
For Gemini on Google AI, you need an API key. You can create an API key in Google AI Studio.
For Gemini on Vertex AI, you need a Google Cloud project with Vertex AI service enabled.
Make a note of the project id. You also need a bearer key for authentication.
You can get that with gcloud
:
gcloud auth print-access-token
Let’s first take a look at how to talk to Gemini on Google AI.
Install Semantic Kernel and Google connector
Create a C# console application:
dotnet new console -o HelloWorldGeminiGoogleAi
Add Semantic Kernel to your console app:
dotnet add package Microsoft.SemanticKernel
You also need the Google connector for Gemini model:
dotnet add package Microsoft.SemanticKernel.Connectors.Google --prerelease
Build the application for Gemini on Google AI
We’re now ready to build the application.
First, some imports, choosing the model we want to use and reading the API key from an env variable:
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Google;
public class Program
{
const string ModelId = "gemini-1.5-flash";
static readonly string ApiKey = Environment.GetEnvironmentVariable("GEMINI_API_KEY") ?? throw new ArgumentNullException("GEMINI_API_KEY environment variable is not set.");
Next, create a kernel with Google AI’s Gemini chat completion.
The Google chat completion connector is currently experimental. To use it, you will need to add #pragma warning disable SKEXP0070:
static async Task Main()
{
// Create a kernel with Google AI's Gemini chat completion
#pragma warning disable SKEXP0070
var builder = Kernel.CreateBuilder().AddGoogleAIGeminiChatCompletion(ModelId, ApiKey);
Build the kernel and initialize settings for Gemini:
// Build the kernel
Kernel kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Settings
GeminiPromptExecutionSettings settings = new()
{
Temperature = 0.8,
MaxTokens = 8192
};
Create a chat history and use it a loop for a conversation:
// Create a history store the conversation
var history = new ChatHistory();
// Initiate a back-and-forth chat
string? userInput;
while (true)
{
// Collect user input
Console.Write("User > ");
userInput = Console.ReadLine();
if (userInput == null)
{
break;
}
// Add user input
history.AddUserMessage(userInput);
// Get the response from the AI
var result = await chatCompletionService.GetChatMessageContentAsync(
history,
executionSettings: settings,
kernel: kernel);
// Print the results
Console.WriteLine("Assistant > " + result);
// Add the message from the agent to the chat history
history.AddMessage(result.Role, result.Content ?? string.Empty);
}
You can see the full Program.cs.
Run the application against Gemini on Google AI
Set your API key:
export GEMINI_API_KEY=your-api-key
Run the app:
dotnet run
You can have a chat with Gemini on Google AI now:
User > Hello
Assistant > Hello! How can I help you today?
User > How are you?
Assistant > I'm doing well, thank you for asking! As a large language model, I don't have
feelings like humans do, but I'm here and ready to help you with any questions or tasks you
might have. What about you? How are you doing today?
Build the application for Gemini on Vertex AI
Building the application for Gemini on Vertex AI is almost identical with slight differences.
One difference is, you need to read the location, project id, and bearer key from env variables:
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Google;
public class Program
{
const string Location = "us-central1";
const string ModelId = "gemini-1.5-flash";
static readonly string ProjectId = Environment.GetEnvironmentVariable("PROJECT_ID") ?? throw new ArgumentNullException("PROJECT_ID environment variable is not set.");
static readonly string BearerKey = Environment.GetEnvironmentVariable("BEARER_KEY") ?? throw new ArgumentNullException("BEARER_KEY environment variable is not set.");
The other difference is, you need to create a kernel with Vertex AI’s Gemini chat completion:
static async Task Main()
{
// Create a kernel with Vertex AI's Gemini chat completion
#pragma warning disable SKEXP0070
var builder = Kernel.CreateBuilder().AddVertexAIGeminiChatCompletion(ModelId, BearerKey, Location, ProjectId);
You can see the full Program.cs.
Run the application against Gemini on Vertex AI
Set your Google Cloud project id and bearer key:
export PROJECT_ID=your-google-cloud-project-id
export BEARER_KEY=your-bearer-key-for-auth
Run the app:
dotnet run
You can have a chat with Gemini on Vertex AI now:
User > Hello
Assistant > Hello! 👋 What can I do for you today? 😊
User > How are you?
Assistant > I'm doing well, thank you for asking! 😊 As a large language model, I don't have
feelings or experiences like humans do, but I'm always here and ready to assist you with any
questions or tasks you might have.
What about you? How are you doing today?
Conclusion
In this blog post, I showed you how to use Semantic Kernel against Gemini on Google AI and Vertex AI in C#. If you want to learn more, here are some resources:
- Sample: HelloWorld Semantic Kernel with Gemini on Google AI.
- Sample: HelloWorld Semantic Kernel with Gemini on Vertex AI.
- Blog post: A comparative overview of LangChain, Semantic Kernel, AutoGen and more.
- Docs: Getting started with Semantic Kernel.
- Docs: Chat completion tutorial - C# and Google Gemini