Skip to content

Instantly share code, notes, and snippets.

@arafattehsin
Created March 8, 2024 00:47
Show Gist options
  • Save arafattehsin/00d9388fb727030fc3214729a6f8158e to your computer and use it in GitHub Desktop.
Save arafattehsin/00d9388fb727030fc3214729a6f8158e to your computer and use it in GitHub Desktop.
Custom Copilot (Vanilla)
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using static System.Environment;
namespace CustomCopilot
{
internal class Program
{
static async Task Main(string[] args)
{
// Create a kernel with the Azure OpenAI chat completion service
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion("gpt-35-turbo-16k",
GetEnvironmentVariable("AOI_ENDPOINT_SWDN")!,
GetEnvironmentVariable("AOI_KEY_SWDN")!);
// Build the kernel
var kernel = builder.Build();
// Create chat history
ChatHistory history = [];
history.AddSystemMessage(@"You're a virtual assistant that helps people find information.");
// Get chat completion service
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Start the conversation
while (true)
{
// Get user input
Console.ForegroundColor = ConsoleColor.White;
Console.Write("User > ");
history.AddUserMessage(Console.ReadLine()!);
// Enable auto function calling
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{
MaxTokens = 200
};
// Get the response from the AI
var response = chatCompletionService.GetStreamingChatMessageContentsAsync(
history,
executionSettings: openAIPromptExecutionSettings,
kernel: kernel);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("\nAssistant > ");
string combinedResponse = string.Empty;
await foreach (var message in response)
{
//Write the response to the console
Console.Write(message);
combinedResponse += message;
}
Console.WriteLine();
// Add the message from the agent to the chat history
history.AddAssistantMessage(combinedResponse);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment