Skip to content

Commit

Permalink
.Net: Added an example how to get citations from chat with data respo…
Browse files Browse the repository at this point in the history
…nse (#9353)

### 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.
-->

This PR contains a change with an example how to get citations from
Azure OpenAI chat completion with data response.

### 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 Oct 21, 2024
1 parent 92ce1dc commit 0b330c9
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public async Task ExampleWithChatCompletionAsync()
// Response: Emily and David, both passionate scientists, met during a research expedition to Antarctica.
Console.WriteLine($"Ask: {ask}");
Console.WriteLine($"Response: {response}");

var citations = GetCitations(chatMessage);

OutputCitations(citations);

Console.WriteLine();

// Chat history maintenance
Expand All @@ -79,6 +84,10 @@ public async Task ExampleWithChatCompletionAsync()
Console.Write(word);
}

citations = GetCitations(chatMessage);

OutputCitations(citations);

Console.WriteLine(Environment.NewLine);
}

Expand Down Expand Up @@ -137,5 +146,34 @@ private static AzureSearchChatDataSource GetAzureSearchDataSource()
IndexName = TestConfiguration.AzureAISearch.IndexName
};
}

/// <summary>
/// Returns a collection of <see cref="ChatCitation"/>.
/// </summary>
private static IReadOnlyList<ChatCitation> GetCitations(ChatMessageContent chatMessageContent)
{
var message = chatMessageContent.InnerContent as OpenAI.Chat.ChatCompletion;
var messageContext = message.GetMessageContext();

return messageContext.Citations;
}

/// <summary>
/// Outputs a collection of <see cref="ChatCitation"/>.
/// </summary>
private void OutputCitations(IReadOnlyList<ChatCitation> citations)
{
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($"URI: {citation.Uri}");
Console.WriteLine($"Content: {citation.Content}");
}
}

#pragma warning restore AOAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}

0 comments on commit 0b330c9

Please sign in to comment.