Skip to content

Commit

Permalink
Updated example
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrostruk committed Nov 19, 2024
1 parent 1112434 commit 7110004
Showing 1 changed file with 8 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public async Task ExampleWithChatCompletionAsync()
Console.WriteLine($"Ask: {ask}");
Console.WriteLine("Response: ");

await foreach (var word in chatCompletion.GetStreamingChatMessageContentsAsync(chatHistory, promptExecutionSettings))
await foreach (var update in chatCompletion.GetStreamingChatMessageContentsAsync(chatHistory, promptExecutionSettings))
{
Console.Write(word);
}
Console.Write(update);

citations = GetCitations(chatMessage);
var streamingCitations = GetCitations(update);

OutputCitations(citations);
OutputCitations(streamingCitations);
}

Console.WriteLine(Environment.NewLine);
}
Expand Down Expand Up @@ -133,34 +133,6 @@ public async Task ExampleWithKernelAsync()
Console.WriteLine();
}

[RetryFact(typeof(HttpOperationException))]
public async Task ExampleWithStreamingAsync()
{
Console.WriteLine("=== Example with Streaming ===");

var ask = "How did Emily and David meet?";

var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
TestConfiguration.AzureOpenAI.Endpoint,
TestConfiguration.AzureOpenAI.ApiKey)
.Build();

var dataSource = GetAzureSearchDataSource();
var promptExecutionSettings = new AzureOpenAIPromptExecutionSettings { AzureChatDataSource = dataSource };

Console.WriteLine($"Ask: {ask}");
Console.WriteLine("Response:");

await foreach (var update in kernel.InvokePromptStreamingAsync(ask, new(promptExecutionSettings)))
{
Console.Write(update.ToString());

var citations = GetCitations(update);
}
}

/// <summary>
/// Initializes a new instance of the <see cref="AzureSearchChatDataSource"/> class.
/// </summary>
Expand Down Expand Up @@ -189,12 +161,12 @@ private static IReadOnlyList<ChatCitation> GetCitations(ChatMessageContent chatM
/// <summary>
/// Returns a collection of <see cref="ChatCitation"/>.
/// </summary>
private static IReadOnlyList<ChatCitation> GetCitations(StreamingKernelContent streamingContent)
private static IReadOnlyList<ChatCitation>? GetCitations(StreamingChatMessageContent streamingContent)
{
var message = streamingContent.InnerContent as OpenAI.Chat.StreamingChatCompletionUpdate;
var messageContext = message.GetMessageContext();
var messageContext = message?.GetMessageContext();

return messageContext.Citations;
return messageContext?.Citations;
}

/// <summary>
Expand Down

0 comments on commit 7110004

Please sign in to comment.