Skip to content

Commit

Permalink
fixed citations
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyydu committed Nov 19, 2024
1 parent 9bcd35f commit 8ada3b3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
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,10 +199,15 @@ public async Task<PromptResponse> CompletePromptAsync(
string text = args.Chunk.delta?.GetContent<string>() ?? "";
IList<Citation>? citations = args.Chunk.delta?.Context?.Citations ?? null;

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


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 @@ public Task WaitForQueue()
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 @@ public void QueueTextChunk(string text, IList<Citation>? citations = null)

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 @@ private async Task SendActivity(Activity activity)
}
};

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 Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Teams.AI.Utilities;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Builder;
using Microsoft.Teams.AI.Application;

namespace Microsoft.Teams.AI
{
Expand Down Expand Up @@ -121,7 +122,7 @@ private Task<ResourceResponse[]> StopTimerWhenSendMessageActivityHandlerAsync(IT
{
foreach (Activity activity in activities)
{
if (activity.Type == ActivityTypes.Message)
if (activity.Type == ActivityTypes.Message || activity.GetChannelData<StreamingChannelData>()?.StreamType != null)
{
Dispose();
break;
Expand Down

0 comments on commit 8ada3b3

Please sign in to comment.