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

[C#] fix: streaming - citations and duplicate typing activity #2202

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ void CaptureSend(Activity[] arg)
StreamingResponse streamer = new(turnContext);
List<Citation> citations = new List<Citation>();
citations.Add(new Citation(content: "test-content", title: "test", url: "https://example.com"));
streamer.QueueTextChunk("first", citations);
streamer.SetCitations(citations);
streamer.QueueTextChunk("first");
await streamer.WaitForQueue();
streamer.QueueTextChunk("second");
await streamer.WaitForQueue();
Expand All @@ -229,6 +230,10 @@ void CaptureSend(Activity[] arg)
streamer.SensitivityLabel = new SensitivityUsageInfo() { Name= "Sensitivity"};
await streamer.EndStream();
Assert.Equal(2, streamer.UpdatesSent());
if (streamer.Citations != null)
{
Assert.Single(streamer.Citations);
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public async Task<PromptResponse> CompletePromptAsync(
return;
}

IList<Citation>? citations = args.Chunk.delta?.Context?.Citations ?? null;

if (citations != null)
{
streamer.SetCitations(citations);
}

// Ignore content without text
// - The chunk is likely for a Tool Call.
Expand All @@ -210,12 +216,10 @@ public async Task<PromptResponse> CompletePromptAsync(

// Send chunk to client
string text = args.Chunk.delta?.GetContent<string>() ?? "";
IList<Citation>? citations = args.Chunk.delta?.Context?.Citations ?? null;


if (text.Length > 0)
{
streamer.QueueTextChunk(text, citations);
streamer.QueueTextChunk(text);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,40 @@
return this._queueSync != null ? this._queueSync : Task.CompletedTask;
}

/// <summary>
/// Sets the citations for the full message.
/// </summary>
/// <param name="citations">Citations to be included in the message.</param>
public void SetCitations(IList<Citation> citations)
{
if (citations.Count > 0)
{
if (this.Citations == null)
{
this.Citations = new List<ClientCitation>();
}

int currPos = this.Citations.Count;

foreach (Citation citation in citations)
{
string abs = CitationUtils.Snippet(citation.Content, 480);

this.Citations.Add(new ClientCitation()
{
Position = $"{currPos}",
Appearance = new ClientCitationAppearance()
{
Name = citation.Title,
Abstract = abs
}
});
currPos++;
}

}
}

/// <summary>
/// Queues an informative update to be sent to the client.
/// </summary>
Expand Down Expand Up @@ -131,38 +165,8 @@

Message += text;

if (citations != null && citations.Count > 0)
{
if (this.Citations == null)
{
this.Citations = new List<ClientCitation>();
}

int currPos = this.Citations.Count;

foreach (Citation citation in citations)
{
string abs = CitationUtils.Snippet(citation.Content, 480);

this.Citations.Add(new ClientCitation()
{
Position = $"{currPos}",
Appearance = new ClientCitationAppearance()
{
Name = citation.Title,
Abstract = abs
}
});
currPos++;
}

// If there are citations, modify the content so that the sources are numbers instead of [doc1], [doc2], etc.
this.Message = this.Citations.Count == 0 ? this.Message : CitationUtils.FormatCitationsResponse(this.Message);

// If there are citations, filter out the citations unused in content.
this.Citations = this.Citations.Count > 0 ? CitationUtils.GetUsedCitations(this.Message, this.Citations) : new List<ClientCitation>();

}
// If there are citations, modify the content so that the sources are numbers instead of [doc1], [doc2], etc.
this.Message = CitationUtils.FormatCitationsResponse(this.Message);

QueueNextChunk();
}
Expand Down Expand Up @@ -320,6 +324,19 @@
}
};

if (this.Citations != null && this.Citations.Count > 0 && !this._ended)
{
// If there are citations, filter out the citations unused in content.
List<ClientCitation>? currCitations = CitationUtils.GetUsedCitations(this.Message, this.Citations);
AIEntity entity = new AIEntity();
if (currCitations != null && currCitations.Count > 0)
{
entity.Citation = currCitations;
}

activity.Entities.Add(entity);
}

// Add in Powered by AI feature flags
if (this._ended)
{
Expand All @@ -331,10 +348,11 @@
// Add in Generated by AI
if (this.EnableGeneratedByAILabel == true)
{
List<ClientCitation>? currCitations = CitationUtils.GetUsedCitations(this.Message, this.Citations);

Check warning on line 351 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Possible null reference argument for parameter 'citations' in 'List<ClientCitation>? CitationUtils.GetUsedCitations(string text, List<ClientCitation> citations)'.

Check warning on line 351 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Possible null reference argument for parameter 'citations' in 'List<ClientCitation>? CitationUtils.GetUsedCitations(string text, List<ClientCitation> citations)'.

Check warning on line 351 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Possible null reference argument for parameter 'citations' in 'List<ClientCitation>? CitationUtils.GetUsedCitations(string text, List<ClientCitation> citations)'.

Check warning on line 351 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Possible null reference argument for parameter 'citations' in 'List<ClientCitation>? CitationUtils.GetUsedCitations(string text, List<ClientCitation> citations)'.
AIEntity entity = new AIEntity();
if (this.Citations != null && this.Citations.Count > 0)
{
entity.Citation = this.Citations;
entity.Citation = currCitations;

Check warning on line 355 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Possible null reference assignment.

Check warning on line 355 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Possible null reference assignment.

Check warning on line 355 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Possible null reference assignment.

Check warning on line 355 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/StreamingResponse.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Possible null reference assignment.
}

entity.UsageInfo = this.SensitivityLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Bot.Schema;
using Microsoft.Bot.Builder;
using Microsoft.Identity.Client;
using Microsoft.Teams.AI.Application;

namespace Microsoft.Teams.AI
{
Expand Down Expand Up @@ -128,7 +129,7 @@ private async Task<ResourceResponse[]> StopTimerWhenSendMessageActivityHandlerAs
{
foreach (Activity activity in activities)
{
if (activity.Type == ActivityTypes.Message)
if (activity.Type == ActivityTypes.Message || activity.GetChannelData<StreamingChannelData>()?.StreamType != null)
{
await _lastSend;
Dispose();
Expand Down
Loading