microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.82k stars 3.25k forks source link

.Net: Add citations information to response metadata when using Azure OpenAI Chat Completion on your data #7438

Closed dmytrostruk closed 1 week ago

dmytrostruk commented 3 months ago

Based on discussion: https://github.com/microsoft/semantic-kernel/discussions/7406

Return citations information like Title and Filepath in chat completion response metadata when using Azure OpenAI Chat Completion on your data feature.

crickman commented 3 months ago

+1

As part of this solution, can we please ensure alignment between chat-completion and agent framework? Agent framework provides citations (annotation) for responses by an assistant-agent which is currently using:

https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/Contents/AnnotationContent.cs

This may require evolving or otherwise refactoring AnnotationContent.

christianarg commented 1 month ago

+1 too. It's kinda not useful as it is right now. In fact we actually need to remove [doc1]...[docX] from the response as it will confuse the user. Any ETA on this?

hk4git commented 1 month ago

Yes, please add citation, looking forward to the solution.

cedricmendelin commented 2 weeks ago

+1 too, would currently need this feature for a customer project. Any workarounds? Or would you in general not use the AzureOpenAI with AiSearch as an API and just implement it as a RAG-Plugin?

cedricmendelin commented 1 week ago

If anybody is looking for a workaround, you can just access the underlying OpenAI object:

...
var chatService = Kernel.GetRequiredService<IChatCompletionService>();
var chatMessage = await chatService.GetChatMessageContentAsync(chat, executionSettings);

var openAiChatCompletion = content.InnerContent as OpenAI.Chat.ChatCompletion;
var context = openAiChatCompletion.GetMessageContext();

on the context element, you will have the Citation property. Have func with it

dmytrostruk commented 1 week ago

As per team's agreement, breaking glass approach would be the main one to access citations: https://github.com/microsoft/semantic-kernel/blob/f2df866541f5a17b402c7aed407ad85de327d244/dotnet/samples/Concepts/ChatCompletion/AzureOpenAIWithData_ChatCompletion.cs#L153-L159

We will consider adding it to the abstractions if more AI providers/connectors will have similar functionality.

Closing this issue for now and we will create a new one in case we decide to add it to abstractions in the future.