Skip to content

Commit 8e1d2ee

Browse files
Chat template: PDF citation viewer (#5843)
1 parent a0fdbfd commit 8e1d2ee

File tree

76 files changed

+21609
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+21609
-6
lines changed

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web/Components/Pages/Chat/Chat.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
2626
Use the search tool to find relevant information. When you do this, end your
2727
reply with citations in the special format, always formatted as XML:
28-
<citation file='filename' page='page_number'>verbatim quote here</citation>.
29-
The quote must be max 5 words, taken directly from search result text, and is the basis for why the citation is relevant.
28+
<citation filename='string' page_number='number'>exact quote here</citation>.
29+
The quote must be max 5 words, taken word-for-word from the search result, and is the basis for why the citation is relevant.
3030
Don't refer to the presence of citations; just emit these tags right at the end, with no surrounding text.
3131
";
3232

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web/Components/Pages/Chat/ChatCitation.razor

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<div class="citation inline-flex bg-white px-3 py-2 rounded mt-4 mr-4 gap-2 text-sm min-w-48">
1+
@using System.Web
2+
<a href="@viewerUrl" target="_blank" class="citation inline-flex bg-white px-3 py-2 rounded mt-4 mr-4 gap-2 text-sm min-w-48">
23
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
34
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
45
</svg>
56
<div class="flex flex-col">
67
<div class="font-semibold">@File</div>
78
<div>@Quote</div>
89
</div>
9-
</div>
10+
</a>
1011

1112
@code {
1213
[Parameter]
@@ -17,4 +18,18 @@
1718

1819
[Parameter]
1920
public required string Quote { get; set; }
21+
22+
private string? viewerUrl;
23+
24+
protected override void OnParametersSet()
25+
{
26+
viewerUrl = null;
27+
28+
// If you ingest other types of content besides PDF files, construct a URL to an appropriate viewer here
29+
if (File.EndsWith(".pdf"))
30+
{
31+
var search = Quote.Trim('.', ',', ' ', '\n', '\r', '\t', '"', '\'');
32+
viewerUrl = $"pdfjs/web/viewer.html?file=/citation/{HttpUtility.UrlEncode(File)}#page={PageNumber}&search={HttpUtility.UrlEncode(search)}&phrase=true";
33+
}
34+
}
2035
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
.citation {
22
border-bottom: 2px solid #a770de;
33
}
4+
5+
.citation[href]:hover {
6+
outline: 1px solid #865cb1;
7+
}
8+
9+
.citation:active {
10+
background-color: rgba(0,0,0,0.05);
11+
}

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web/Components/Pages/Chat/ChatMessageItem.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ else if (Message.Role == ChatRole.Assistant)
5656

5757
@code {
5858
private static readonly ConditionalWeakTable<ChatMessage, ChatMessageItem> SubscribersLookup = new();
59-
private static readonly Regex CitationRegex = new(@"<citation file='(?<file>[^']*)' page='(?<page>\d*)'>(?<quote>.*?)</citation>", RegexOptions.Compiled);
59+
private static readonly Regex CitationRegex = new(@"<citation filename='(?<file>[^']*)' page_number='(?<page>\d*)'>(?<quote>.*?)</citation>", RegexOptions.Compiled);
6060

6161
private List<(string File, int? Page, string Quote)>? citations;
6262

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using ChatWithCustomData.Web.Services;
1717
using ChatWithCustomData.Web.Services.Ingestion;
1818
using System.ClientModel;
19+
using Microsoft.Extensions.FileProviders;
20+
using Microsoft.Extensions.FileProviders.Physical;
1921
#if (UseAzureAISearch)
2022
using Azure.Search.Documents.Indexes;
2123
using Microsoft.SemanticKernel.Connectors.AzureAISearch;
@@ -111,7 +113,15 @@
111113
app.UseHttpsRedirection();
112114
app.UseAntiforgery();
113115

114-
app.MapStaticAssets();
116+
// Serve any file in the /Data directory for the purpose of showing citations
117+
// Caution: only place files in this directory that you want to be publicly accessible
118+
app.UseStaticFiles(new StaticFileOptions
119+
{
120+
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "Data")),
121+
RequestPath = "/citation"
122+
});
123+
124+
app.UseStaticFiles();
115125
app.MapRazorComponents<App>()
116126
.AddInteractiveServerRenderMode();
117127

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pdf.js version 4.10.38
2+
https://github.com/mozilla/pdf.js
3+
License: Apache-2.0
4+
5+
To update, replace the files with an updated build from https://github.com/mozilla/pdf.js/releases
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 30 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)