Skip to content

Commit

Permalink
Fix #85 (#228)
Browse files Browse the repository at this point in the history
## Purpose
<!-- Describe the intention of the changes being proposed. What problem
does it solve or functionality does it add? -->
* ...

## Does this introduce a breaking change?
<!-- Mark one with an "x". -->
```
[ ] Yes
[ ] No
```

## Pull Request Type
What kind of change does this Pull Request introduce?

<!-- Please check the one that applies to this PR using "x". -->
```
[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:
```

## How to Test
*  Get the code

```
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
```

* Test the code
<!-- Add steps to run the tests suite and/or manually test -->
```
```

## What to Check
Verify that the following are valid
* ...

## Other Information
<!-- Add any other helpful information that may be needed here. -->
  • Loading branch information
LittleLittleCloud authored Nov 8, 2023
1 parent afeccfd commit 1e2b031
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
9 changes: 4 additions & 5 deletions app/backend/Extensions/SearchClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MinimalApi.Extensions;

internal static class SearchClientExtensions
{
internal static async Task<string> QueryDocumentsAsync(
internal static async Task<SupportingContentRecord[]> QueryDocumentsAsync(
this SearchClient searchClient,
string? query = null,
float[]? embedding = null,
Expand Down Expand Up @@ -68,7 +68,7 @@ internal static async Task<string> QueryDocumentsAsync(
// "sourcepage": "Northwind_Standard_Benefits_Details-24.pdf",
// "sourcefile": "Northwind_Standard_Benefits_Details.pdf"
// }
var sb = new StringBuilder();
var sb = new List<SupportingContentRecord>();
foreach (var doc in searchResult.GetResults())
{
doc.Document.TryGetValue("sourcepage", out var sourcePageValue);
Expand All @@ -94,11 +94,10 @@ internal static async Task<string> QueryDocumentsAsync(
if (sourcePageValue is string sourcePage && contentValue is string content)
{
content = content.Replace('\r', ' ').Replace('\n', ' ');
sb.AppendLine($"{sourcePage}:{content}");
sb.Add(new SupportingContentRecord(sourcePage,content));
}
}
documentContents = sb.ToString();

return documentContents;
return sb.ToArray();
}
}
11 changes: 8 additions & 3 deletions app/backend/Services/ReadRetrieveReadChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ standard plan AND dental AND employee benefit.

// step 2
// use query to search related docs
var documentContents = await _searchClient.QueryDocumentsAsync(query, embeddings, overrides, cancellationToken);
var documentContentList = await _searchClient.QueryDocumentsAsync(query, embeddings, overrides, cancellationToken);

if (string.IsNullOrEmpty(documentContents))
string documentContents = string.Empty;
if (documentContentList.Length == 0)
{
documentContents = "no source available.";
}
else
{
documentContents = string.Join("\r", documentContentList.Select(x =>$"{x.Title}:{x.Content}"));
}

Console.WriteLine(documentContents);
// step 3
Expand Down Expand Up @@ -152,7 +157,7 @@ Return the follow-up question as a json string list.
}
}
return new ApproachResponse(
DataPoints: documentContents.Split('\r'),
DataPoints: documentContentList,
Answer: ans,
Thoughts: thoughts,
CitationBaseUrl: _configuration.ToCitationBaseUrl());
Expand Down
8 changes: 4 additions & 4 deletions app/frontend/Components/SupportingContent.razor.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace ClientApp.Components;

public sealed partial class SupportingContent
{
internal static ParsedSupportingContentItem ParseSupportingContent(string item)
internal static ParsedSupportingContentItem ParseSupportingContent(SupportingContentRecord item)
{
// Assumes the item starts with the file name followed by : and the content.
// Example: "sdp_corporate.pdf: this is the content that follows".
var parts = item.Split(":");
var title = parts[0];
var title = item.Title;
var content = item.Content;

return parts is { Length: 2 } ? new ParsedSupportingContentItem(title, parts[1].Trim()) : new ParsedSupportingContentItem(title);
return new ParsedSupportingContentItem(title, content.Trim());
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/frontend/Components/SupportingContent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ClientApp.Components;

public sealed partial class SupportingContent
{
[Parameter, EditorRequired] public required string[] DataPoints { get; set; }
[Parameter, EditorRequired] public required SupportingContentRecord[] DataPoints { get; set; }

private ParsedSupportingContentItem[] _supportingContent = [];

Expand Down
3 changes: 2 additions & 1 deletion app/shared/Shared/Models/ApproachResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Shared.Models;

public record SupportingContentRecord(string Title, string Content);
public record ApproachResponse(
string Answer,
string? Thoughts,
string[] DataPoints,
SupportingContentRecord[] DataPoints, // title, content
string CitationBaseUrl,
string? Error = null);
8 changes: 5 additions & 3 deletions app/tests/ClientApp.Tests/SupportingContentParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using Shared.Models;

namespace ClientApp.Tests;

#pragma warning disable CA1416 // Validate platform compatibility
Expand All @@ -12,14 +14,14 @@ public static IEnumerable<object[]> ParserInput
{
yield return new object[]
{
"test.pdf:blah blah",
new SupportingContentRecord("test.pdf","blah blah"),
"test.pdf",
"blah blah",
};

yield return new object[]
{
"sdp_corporate.pdf: this is the content that follows",
new SupportingContentRecord("sdp_corporate.pdf", "this is the content that follows"),
"sdp_corporate.pdf",
"this is the content that follows",
};
Expand All @@ -28,7 +30,7 @@ public static IEnumerable<object[]> ParserInput

[Theory, MemberData(nameof(ParserInput))]
public void SupportingContentCorrectlyParsesText(
string supportingContent,
SupportingContentRecord supportingContent,
string expectedTitle,
string? expectedContent)
{
Expand Down

0 comments on commit 1e2b031

Please sign in to comment.