Skip to content

Instantly share code, notes, and snippets.

@medhatelmasry
Created October 22, 2025 23:30
Show Gist options
  • Select an option

  • Save medhatelmasry/9bdd10b194eb730154723bfec058648b to your computer and use it in GitHub Desktop.

Select an option

Save medhatelmasry/9bdd10b194eb730154723bfec058648b to your computer and use it in GitHub Desktop.
AI Toolkit Sematic Kernel Client
using System.Text;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
var model = "mistral-7b-v02-int4-cpu";
var baseUrl = "http://localhost:5272/v1/";
var apikey = "unused";
// Create a chat completion service
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId: model, apiKey: apikey, endpoint: new Uri(baseUrl))
.Build();
var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddSystemMessage("You are a useful chatbot. Always reply in a funny way with short answers.");
var settings = new OpenAIPromptExecutionSettings
{
MaxTokens = 500,
Temperature = 1,
};
while (true)
{
Console.Write("\nUser: ");
var userInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(userInput)) break;
history.AddUserMessage(userInput);
var responseBuilder = new StringBuilder();
Console.Write("\nAI: ");
await foreach (var message in chat.GetStreamingChatMessageContentsAsync(userInput, settings, kernel))
{
responseBuilder.Append(message);
Console.Write(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment