Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: citations do not work in c# project. #2311

Open
xzf0587 opened this issue Feb 14, 2025 · 3 comments
Open

[Bug]: citations do not work in c# project. #2311

xzf0587 opened this issue Feb 14, 2025 · 3 comments
Assignees
Labels
bug Something isn't working dotnet Change/fix applies to dotnet. If all three, use the 'JS & dotnet & Python' label

Comments

@xzf0587
Copy link

xzf0587 commented Feb 14, 2025

Language

C#

Version

latest

Description

I tried to add citations for ai messages by overriding SayCommandAsync in C# project. And I found that there will be no citation in the messages.
It works fine in js/ts projects. List the code below. Please help me to figure out the problem. Thanks.
Since the citation util class is not exposed yet mentioned in #2309, I copied the CitationUtils code from the library into my project.

using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Teams.AI.AI.Action;
using Microsoft.Teams.AI.AI.Planners;
using Microsoft.Teams.AI.AI;
using Newtonsoft.Json.Linq;
using Microsoft.Bot.Connector;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MyTeamsApp021401
{
    public class ActionHandlers
    {
        [Action(AIConstants.SayCommandActionName, isDefault: true)]
        public async Task<string> SayCommandAsync([ActionTurnContext] ITurnContext turnContext, [ActionParameters] PredictedSayCommand command, CancellationToken cancellationToken = default)
        {
            if (command.Response.Content == null || command.Response.GetContent<string>() == string.Empty)
            {
                return "";
            }

            string originalContent = command.Response.GetContent<string>();
            string content = "";
            Root response = null;
            try
            {
                response = JsonSerializer.Deserialize<Root>(originalContent);
            }
            catch (Exception error)
            {
                Console.Error.WriteLine($"Response is not valid json, send the raw text. error: {error}");
                await turnContext.SendActivityAsync(MessageFactory.Text(originalContent), default);
                return "";
            }

            // If the response from the AI includes citations, those citations will be parsed and added to the SAY command.
            List<ClientCitation> citations = new();
            int position = 1;

            if (response.Results != null && response.Results.Count > 0)
            {
                foreach (var contentItem in response.Results)
                {
                    if (!string.IsNullOrEmpty(contentItem.CitationTitle))
                    {
                        var clientCitation = new ClientCitation
                        {
                            Position = position,
                            Appearance = new ClientCitationAppearance
                            {
                                Name = contentItem.CitationTitle ?? $"Document #{position}",
                                Url = contentItem.CitationUrl,
                                Abstract = CitationUtils.Snippet(contentItem.CitationContent, 500)
                            }
                        };
                        content += $"{contentItem.Answer}[{position}]<br>";
                        position++;
                        citations.Add(clientCitation);
                    }
                    else
                    {
                        content += $"{contentItem.Answer}<br>";
                    }
                }
            }
            else
            {
                content = originalContent;
            }

            bool isTeamsChannel = turnContext.Activity.ChannelId == Channels.Msteams;

            if (isTeamsChannel)
            {
                content.Replace("\n", "<br>");
            }


            var contentText = citations.Count < 1 ? content : CitationUtils.FormatCitationsResponse(content);

            var referencedCitations = citations.Count > 0 ? CitationUtils.GetUsedCitations(contentText, citations) : null;

            var message = MessageFactory.Text(contentText);
            message.Type = ActivityTypes.Message;
            if (isTeamsChannel)
            {
                message.ChannelData = new
                {
                    feedbackLoopEnabled = false

                };
            }
            message.Entities = new List<Entity>
             {
                new AIEntity
                {
                    AtType = "Message",
                    AtContext = "https://schema.org",
                    AtId = "",
                    AdditionalType = new List<string> { "AIGeneratedContent" },
                    Citation = referencedCitations,
                    //Citation = "Source: Microsoft Teams Documentation",
                    Type = "https://schema.org/Message"
                }
            };
            await turnContext.SendActivityAsync(message, default);

            return string.Empty;
        }
    }
}

public class Result
{
    [JsonPropertyName("answer")]
    public string Answer { get; set; }
    [JsonPropertyName("citationTitle")]
    public string CitationTitle { get; set; }
    [JsonPropertyName("citationContent")]
    public string CitationContent { get; set; }
    [JsonPropertyName("citationUrl")]
    public string CitationUrl { get; set; }
}

public class Root
{
    // add json property name result
    [JsonPropertyName("results")]
    public List<Result> Results { get; set; }
}

Reproduction Steps

1.
2.
3.
...
@xzf0587 xzf0587 added the bug Something isn't working label Feb 14, 2025
@Prasad-MSFT
Copy link

Hi @xzf0587,
Currently there is no information available regarding the plans to support ai labels, citations, and feedback buttons in C#.
To achieve your requirements, we recommend you give your feedback in Teams Feedback Portal and you can now get latest Teams platform updates by subscribing to the RSS feed

@corinagum
Copy link
Collaborator

@Prasad-MSFT, that information is incorrect... Teams AI does support Powered by AI features

This is the correct place to file Teams AI bugs.

@singhk97 Could you please investigate? Thanks.

@lilyydu lilyydu added the dotnet Change/fix applies to dotnet. If all three, use the 'JS & dotnet & Python' label label Feb 21, 2025
@Prasad-MSFT
Copy link

Apologies @corinagum, when we searched, we could not find any C# references in any of the official documentation.
https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bot-messages-ai-generated-content?tabs=desktop%2Cbotmessage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dotnet Change/fix applies to dotnet. If all three, use the 'JS & dotnet & Python' label
Projects
None yet
Development

No branches or pull requests

5 participants