mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
"KernelSyntaxExamples" is the only outstanding project under /samples that still uses project references to access the kernel libraries. This change consolidates all the building, linked dotnet SDK code in SK-dotnet.sln into a single top-level folder. This will simplify build jobs, automatic code owner assignment, and other dev ops tasks. Coming next: other projects in samples will get removed from the dotnet/SK-dotnet.sln solution, and a new solution with dedicated builds created for the /samples folder. TBD: - Language-specific code "samples" will move into a similar subfolder for each language. - The current "/samples" folder (at the root) will likely become something like "apps", with the demo apps promoted to the top-level folder for better discoverability. - notebooks may also move to this folder in the future. Co-authored-by: name <email>
123 lines
3.8 KiB
C#
123 lines
3.8 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SemanticKernel.AI.TextCompletion;
|
|
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.TextCompletion;
|
|
using RepoUtils;
|
|
|
|
/**
|
|
* The following example shows how to use Semantic Kernel with Multiple Results Text Completion as streaming
|
|
*/
|
|
// ReSharper disable once InconsistentNaming
|
|
public static class Example37_MultiStreamingCompletion
|
|
{
|
|
private static readonly object s_lockObject = new();
|
|
|
|
public static async Task RunAsync()
|
|
{
|
|
await AzureOpenAIMultiTextCompletionStreamAsync();
|
|
await OpenAITextCompletionStreamAsync();
|
|
}
|
|
|
|
private static async Task AzureOpenAIMultiTextCompletionStreamAsync()
|
|
{
|
|
Console.WriteLine("======== Azure OpenAI - Multiple Text Completion - Raw Streaming ========");
|
|
|
|
var textCompletion = new AzureTextCompletion(
|
|
Env.Var("AZURE_OPENAI_DEPLOYMENT_NAME"),
|
|
Env.Var("AZURE_OPENAI_ENDPOINT"),
|
|
Env.Var("AZURE_OPENAI_KEY"));
|
|
|
|
await TextCompletionStreamAsync(textCompletion);
|
|
}
|
|
|
|
private static async Task OpenAITextCompletionStreamAsync()
|
|
{
|
|
Console.WriteLine("======== Open AI - Multiple Text Completion - Raw Streaming ========");
|
|
|
|
ITextCompletion textCompletion = new OpenAITextCompletion(
|
|
"text-davinci-003",
|
|
Env.Var("OPENAI_API_KEY"));
|
|
|
|
await TextCompletionStreamAsync(textCompletion);
|
|
}
|
|
|
|
private static async Task TextCompletionStreamAsync(ITextCompletion textCompletion)
|
|
{
|
|
var requestSettings = new CompleteRequestSettings()
|
|
{
|
|
MaxTokens = 200,
|
|
FrequencyPenalty = 0,
|
|
PresencePenalty = 0,
|
|
Temperature = 1,
|
|
TopP = 0.5,
|
|
ResultsPerPrompt = 3
|
|
};
|
|
|
|
var prompt = "Write one paragraph why AI is awesome";
|
|
var consoleLinesPerResult = 12;
|
|
|
|
PrepareDisplay();
|
|
|
|
List<Task> resultTasks = new();
|
|
int currentResult = 0;
|
|
await foreach (var completionResult in textCompletion.GetStreamingCompletionsAsync(prompt, requestSettings))
|
|
{
|
|
resultTasks.Add(ProcessStreamAsyncEnumerableAsync(completionResult, currentResult++, consoleLinesPerResult));
|
|
}
|
|
|
|
Console.WriteLine();
|
|
|
|
await Task.WhenAll(resultTasks.ToArray());
|
|
|
|
/*
|
|
|
|
int position = 0;
|
|
await foreach (ITextCompletionStreamingResult completionResult in textCompletion.CompleteMultiStreamAsync(prompt, requestSettings))
|
|
{
|
|
string fullMessage = string.Empty;
|
|
|
|
await foreach (string message in completionResult.GetCompletionStreamingAsync())
|
|
{
|
|
fullMessage += message;
|
|
|
|
Console.SetCursorPosition(0, (position * consoleLinesPerResult));
|
|
Console.Write(fullMessage);
|
|
}
|
|
|
|
position++;
|
|
}*/
|
|
|
|
Console.SetCursorPosition(0, requestSettings.ResultsPerPrompt * consoleLinesPerResult);
|
|
Console.WriteLine();
|
|
}
|
|
|
|
private static async Task ProcessStreamAsyncEnumerableAsync(ITextStreamingResult result, int resultNumber, int linesPerResult)
|
|
{
|
|
var fullSentence = string.Empty;
|
|
await foreach (var word in result.GetCompletionStreamingAsync())
|
|
{
|
|
fullSentence += word;
|
|
|
|
lock (s_lockObject)
|
|
{
|
|
Console.SetCursorPosition(0, (resultNumber * linesPerResult));
|
|
Console.Write(fullSentence);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Break enough lines as the current console window size to display the results
|
|
/// </summary>
|
|
private static void PrepareDisplay()
|
|
{
|
|
for (int i = 0; i < Console.WindowHeight - 2; i++)
|
|
{
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
}
|