March 6th, 2025

Effortlessly Integrate xAI’s Grok with Semantic Kernel

Grok generated image based on the blog content

For Semantic Kernel users, integrating xAI’s Grok API using the OpenAI connector is a breeze thanks to its compatibility with OpenAI’s API format.

This tutorial focuses on setting up Grok in your Semantic Kernel projects with minimal fuss, using C# and Python examples.

Why Grok?

Grok, built by xAI, is a powerful AI model, offers a 128k context window and function-calling support, making it a solid choice for complex tasks in Semantic Kernel.

With an API compatible with OpenAI, announced in November 2023 and now available via API access, with models like “grok-beta” available for developers and soon the new flagship “grok-3”.

This compatibility allows the OpenAI connector in Semantic Kernel to interface with Grok by adjusting the base URL and API key, leveraging Semantic Kernel’s existing infrastructure.

Pricing

The xAI API offers access to Grok models with pricing based on token usage, as detailed in the xAI Models documentation.

This page lists available models, their pricing per million tokens, and additional capabilities like context length and multimodal support. Below is the current pricing for Grok models in the us-east-1 cluster as of March 2025:

Model Input (per 1M tokens) Output (per 1M tokens)
grok-2-vision-1212 $2.00 (Text/Image) $10.00
grok-2-1212 $2.00 (Text) $10.00
grok-vision-beta $5.00 (Text/Image) $15.00
grok-beta $5.00 (Text) $15.00

Obtaining a Grok API Key

To begin, users must obtain a Grok API key from xAI’s platform:

  • Visit xAI’s console to sign up or log in.
  • Navigate to the “API Keys” section, create a new key, and note it down.
  • The base URL for API calls is https://api.x.ai/v1.

Setting Up in .NET (C#)

Semantic Kernel’s OpenAI connector, specifically the OpenAIChatCompletionService class, can be configured to connect to Grok. The constructor allows specification of the API key and base URL, enabling compatibility with Grok’s API. Here’s how to set it up:

using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

#pragma warning disable SKEXP0010

// Initialize the OpenAI chat completion service with the grok-beta model.
var chatService = new OpenAIChatCompletionService(
    modelId: "grok-beta",  // Grok API model
    apiKey: "your_grok_api_key",  // Your Grok API key from xAI
    endpoint: new Uri("https://api.x.ai/v1")  // Grok API endpoint
);

// Create a new chat history and add a user message to prompt the model.
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Why is the sky blue in one sentence?");

// Configure settings for the chat completion request.
var settings = new OpenAIPromptExecutionSettings { MaxTokens = 100 };

// Send the chat completion request to Grok
var reply = await chatService.GetChatMessageContentAsync(chatHistory, settings);
Console.WriteLine("Grok reply: " + reply);

Setting Up in Python

In Python, leverage OpenAIChatCompletion from semantic_kernel.connectors.ai.open_ai with an async setup. The constructor allows specification of the API key and base URL, enabling compatibility with Grok’s API. Here’s how to set it up:

import asyncio

from openai import AsyncOpenAI
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAIChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory

async def main():
    # Initialize the OpenAI chat completion service with the grok-beta model.
    chat_service = OpenAIChatCompletion(
        ai_model_id="grok-beta",
        async_client=AsyncOpenAI(
            api_key="your_grok_api_key",
            base_url="https://api.x.ai/v1",
        ),
    )

    # Create a new chat history and add a user message to prompt the model.
    chat_history = ChatHistory()
    chat_history.add_system_message("You are a helpful assistant.")
    chat_history.add_user_message("Why is the sky blue in one sentence?")

    # Configure settings for the chat completion request.
    settings = OpenAIChatPromptExecutionSettings(max_tokens=100)

    # Get the model's response
    response = await chat_service.get_chat_message_content(chat_history, settings)
    print("Grok reply:", response)

# Run the async main function
if __name__ == "__main__":
    asyncio.run(main())

We invite you to dive into Grok models within your Semantic Kernel workflows. With their impressive 128k context window, support for function calling, and compatibility with xAI’s innovative API, Grok brings a fresh perspective to your AI toolkit. Semantic Kernel smooths out the integration process, making it easy to swap in Grok and explore its potential. Try it out and discover how it can enhance your projects—whether you’re crafting intelligent agents, tackling intricate queries, or streamlining multi-step AI processes. Enjoy experimenting!

References

Ready to explore more? Check out these key resources to fuel your Grok and Semantic Kernel journey:

  • More about SK Chat Completion: Get the full scoop on chat services here.
  • xAI Console: Grab your Grok API key and get started here.
  • xAI Models & Pricing: Curious about costs and options? Peek here.
  • xAI Blog Highlights: Stay in the loop with the latest from xAI here.
  • Grok Performance Unveiled: Dig into benchmark wins for Grok-2 and Grok-3.

Author

0 comments