mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Refactor citation handling in Ask.razor and ChatService.cs
Updated the assistant message construction in `Ask.razor` to manage citations more effectively by introducing a `RawText` property and a new `RemoveCitations` method. The `ExtractCitations` method now processes raw input for citation extraction. Removed outdated comments in `ChatService.cs` regarding citation formatting rules, indicating a potential shift in how citation handling is enforced.
This commit is contained in:
@@ -196,15 +196,16 @@
|
|||||||
}
|
}
|
||||||
else if (delta.StreamState == StreamState.Append)
|
else if (delta.StreamState == StreamState.Append)
|
||||||
{
|
{
|
||||||
// Adds tokens to the assistant message as they are received
|
// Adds tokens to the assistant message as they are received.
|
||||||
assistantMessage.Text += delta.Answer;
|
assistantMessage.RawText += delta.Answer;
|
||||||
|
|
||||||
|
// Updates the Text property to remove citations, if any.
|
||||||
|
assistantMessage.Text = RemoveCitations(assistantMessage.RawText);
|
||||||
}
|
}
|
||||||
else if (delta.StreamState == StreamState.End)
|
else if (delta.StreamState == StreamState.End)
|
||||||
{
|
{
|
||||||
// Extracts citations, if any.
|
// Extracts citations, if any.
|
||||||
var (cleanText, citations) = ExtractCitations(assistantMessage.Text);
|
var (_, citations) = ExtractCitations(assistantMessage.RawText);
|
||||||
|
|
||||||
assistantMessage.Text = cleanText;
|
|
||||||
assistantMessage.Citations = citations;
|
assistantMessage.Citations = citations;
|
||||||
|
|
||||||
assistantMessage.IsCompleted = true;
|
assistantMessage.IsCompleted = true;
|
||||||
@@ -294,6 +295,16 @@
|
|||||||
await JSRuntime.InvokeVoidAsync("scrollTo", chat);
|
await JSRuntime.InvokeVoidAsync("scrollTo", chat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string RemoveCitations(string? text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (text.AsSpan().IndexOf("<cit", StringComparison.OrdinalIgnoreCase) is var index and >= 0 ? text[..index] : text).TrimEnd();
|
||||||
|
}
|
||||||
|
|
||||||
private static (string, IEnumerable<Citation>) ExtractCitations(string? text)
|
private static (string, IEnumerable<Citation>) ExtractCitations(string? text)
|
||||||
{
|
{
|
||||||
var citations = new List<Citation>();
|
var citations = new List<Citation>();
|
||||||
@@ -326,6 +337,13 @@
|
|||||||
|
|
||||||
public class Message
|
public class Message
|
||||||
{
|
{
|
||||||
|
private string? rawText;
|
||||||
|
public string? RawText
|
||||||
|
{
|
||||||
|
get => rawText ?? Text;
|
||||||
|
set => rawText = value;
|
||||||
|
}
|
||||||
|
|
||||||
public string? Text { get; set; }
|
public string? Text { get; set; }
|
||||||
|
|
||||||
public required string Role { get; set; }
|
public required string Role { get; set; }
|
||||||
|
|||||||
@@ -144,24 +144,6 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer
|
|||||||
- The citations must always be in a list at the end of the response, one after the other. Never add the citations between the actual response text or inside sentences.
|
- The citations must always be in a list at the end of the response, one after the other. Never add the citations between the actual response text or inside sentences.
|
||||||
- Do NOT add any text after the citations.
|
- Do NOT add any text after the citations.
|
||||||
- ALWAYS leave a blank line between your answer and the first citation.
|
- ALWAYS leave a blank line between your answer and the first citation.
|
||||||
|
|
||||||
Examples (CORRECT):
|
|
||||||
Here is my complete answer to your question. I'm providing all the information based on the context.
|
|
||||||
|
|
||||||
<citation filename='doc1.pdf' page_number='1'>Paris is the capital</citation>
|
|
||||||
<citation filename='doc2.pdf' page_number='2'>largest city in France</citation>
|
|
||||||
|
|
||||||
Examples (WRONG):
|
|
||||||
Here is my answer <citation filename='doc1.pdf' page_number='1'>Paris is the capital of France and is known for the Eiffel Tower</citation> with more text.
|
|
||||||
<citation filename='doc1.pdf' page_number='1'>Paris is the capital of France and is known for the Eiffel Tower</citation> Here is my answer.
|
|
||||||
Here is my answer. (without any citations when information is available)
|
|
||||||
Here is my answer.
|
|
||||||
<citation filename='doc1.pdf' page_number='1'>Paris is the capital of France and is known for the Eiffel Tower</citation> More answer text.
|
|
||||||
|
|
||||||
YOU MUST SEPARATE YOUR ANSWER FROM CITATIONS WITH A BLANK LINE.
|
|
||||||
NEVER INSERT CITATIONS WITHIN YOUR ANSWER TEXT.
|
|
||||||
CITATIONS MUST ONLY APPEAR AT THE END, AFTER A BLANK LINE.
|
|
||||||
IF YOU DO NOT FOLLOW THESE RULES, YOUR RESPONSE IS INVALID.
|
|
||||||
""");
|
""");
|
||||||
|
|
||||||
var prompt = new StringBuilder($"""
|
var prompt = new StringBuilder($"""
|
||||||
|
|||||||
Reference in New Issue
Block a user