Skip to content

Commit

Permalink
.Net: Updated streaming example of Azure chat completion with data (#…
Browse files Browse the repository at this point in the history
…9760)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Related: #9728

Updated streaming example of Azure chat completion with data to show how
to get citations in streaming mode.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
dmytrostruk authored Nov 20, 2024
1 parent f5facce commit 9f44eb7
Showing 1 changed file with 28 additions and 14 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 @@ -159,19 +159,33 @@ private static IReadOnlyList<ChatCitation> GetCitations(ChatMessageContent chatM
}

/// <summary>
/// Outputs a collection of <see cref="ChatCitation"/>.
/// Returns a collection of <see cref="ChatCitation"/>.
/// </summary>
private void OutputCitations(IReadOnlyList<ChatCitation> citations)
private static IReadOnlyList<ChatCitation>? GetCitations(StreamingChatMessageContent streamingContent)
{
Console.WriteLine("Citations:");
var message = streamingContent.InnerContent as OpenAI.Chat.StreamingChatCompletionUpdate;
var messageContext = message?.GetMessageContext();

foreach (var citation in citations)
return messageContext?.Citations;
}

/// <summary>
/// Outputs a collection of <see cref="ChatCitation"/>.
/// </summary>
private void OutputCitations(IReadOnlyList<ChatCitation>? citations)
{
if (citations is not null)
{
Console.WriteLine($"Chunk ID: {citation.ChunkId}");
Console.WriteLine($"Title: {citation.Title}");
Console.WriteLine($"File path: {citation.FilePath}");
Console.WriteLine($"URL: {citation.Url}");
Console.WriteLine($"Content: {citation.Content}");
Console.WriteLine("Citations:");

foreach (var citation in citations)
{
Console.WriteLine($"Chunk ID: {citation.ChunkId}");
Console.WriteLine($"Title: {citation.Title}");
Console.WriteLine($"File path: {citation.FilePath}");
Console.WriteLine($"URL: {citation.Url}");
Console.WriteLine($"Content: {citation.Content}");
}
}
}

Expand Down

0 comments on commit 9f44eb7

Please sign in to comment.