Skip to content

Instantly share code, notes, and snippets.

@jongalloway
Created October 12, 2023 19:53
Show Gist options
  • Save jongalloway/58ccb880559aad3a36fccb0657e4ba84 to your computer and use it in GitHub Desktop.
Save jongalloway/58ccb880559aad3a36fccb0657e4ba84 to your computer and use it in GitHub Desktop.
Simple AI Chat
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Memory;
using System.Text;
string aoaiEndpoint = Environment.GetEnvironmentVariable("AZUREOPENAI_ENDPOINT")!;
string aoaiApiKey = Environment.GetEnvironmentVariable("AZUREOPENAI_API_KEY")!;
string gptModelDeploymentName = Environment.GetEnvironmentVariable("AZUREOPENAI_GPT_NAME")!;
string adaTextDeploymentName = Environment.GetEnvironmentVariable("AZUREOPENAI_TEXT_EMBEDDING_NAME")!;
// Initialize the kernel
IKernel kernel = Kernel.Builder
.WithAzureChatCompletionService(gptModelDeploymentName, aoaiEndpoint, aoaiApiKey)
.WithAzureTextEmbeddingGenerationService(adaTextDeploymentName, aoaiEndpoint, aoaiApiKey)
.Build();
var logger = kernel.LoggerFactory.CreateLogger<IKernel>();
// Download a document and create embeddings for it
ISemanticTextMemory memory = kernel.Memory;
IList<string> collections = await memory.GetCollectionsAsync();
string collectionName = "orleansdiscords";
if (collections.Contains(collectionName))
{
logger.LogInformation("Found database");
}
else
{
// load in your data here
logger.LogInformation("Generated database");
}
// Create a new chat
IChatCompletion ai = kernel.GetService<IChatCompletion>();
ChatHistory chat = ai.CreateNewChat("You are an AI assistant that helps people find information.");
StringBuilder builder = new();
// Q&A loop
while (true)
{
Console.Write("Question: ");
string question = Console.ReadLine()!;
builder.Clear();
await foreach (var result in memory.SearchAsync(collectionName, question, limit: 3))
builder.AppendLine(result.Metadata.Text);
int contextToRemove = -1;
if (builder.Length != 0)
{
builder.Insert(0, "Here's some additional information: ");
contextToRemove = chat.Count;
chat.AddUserMessage(builder.ToString());
}
chat.AddUserMessage(question);
builder.Clear();
await foreach (string message in ai.GenerateMessageStreamAsync(chat))
{
Console.Write(message);
builder.Append(message);
}
Console.WriteLine();
chat.AddAssistantMessage(builder.ToString());
if (contextToRemove >= 0) chat.RemoveAt(contextToRemove);
Console.WriteLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment