mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 07:53:48 +00:00
Fixed the retrieval context validation keeping unhelpful passages (#1217)
This commit is contained in:
1 parent
808b95b15d
commit
78f5066bfd
10 files changed
+454
-36
No files matched your search
@@ -0,0 +1,115 @@
|
||||
using AIStudio.AIOperations;
|
||||
using AIStudio.AIOperations.RetrievalContextValidation;
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Provider;
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.RAG;
|
||||
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace AIStudio.Tests.AIOperations;
|
||||
|
||||
/// <summary>
|
||||
/// Checks how the validation of retrieval contexts reads what a model answered about a passage.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A failed validation once stood for a decision with a confidence of 1.0, so the passage always stayed and
|
||||
/// moved the threshold of all others. An answer without a usable judgment now says so with IsAnswered,
|
||||
/// and nothing the model left out is read as a value it never wrote.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class RetrievalContextValidationAnswerTests
|
||||
{
|
||||
private static readonly PreparedRetrievalContext PASSAGE = new(new RetrievalTextContext
|
||||
{
|
||||
DataSourceName = "Handbook",
|
||||
Category = RetrievalContentCategory.TEXT,
|
||||
Type = RetrievalContentType.TEXT_DOCUMENT,
|
||||
Path = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "handbook.pdf")),
|
||||
MatchedText = "The handbook explains the travel expenses.",
|
||||
}, "body", [], null);
|
||||
|
||||
[TestCase("""{"decision": true, "reason": "It answers the question.", "confidence": 0.87}""", true, 0.87f)]
|
||||
[TestCase("""{"decision": false, "reason": "It is about something else.", "confidence": 0.9}""", false, 0.9f)]
|
||||
[TestCase("""{"decision": true, "reason": "Certain.", "confidence": 1}""", true, 1f)]
|
||||
[TestCase("""{"decision": false, "reason": "A pure guess.", "confidence": 0}""", false, 0f)]
|
||||
public void AJudgmentIsReadAsTheModelWroteIt(string answer, bool decision, float confidence)
|
||||
{
|
||||
var result = RetrievalContextValidationOperation.ReadAnswer(answer, PASSAGE);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(result.IsAnswered, Is.True);
|
||||
Assert.That(result.Decision, Is.EqualTo(decision));
|
||||
Assert.That(result.Confidence, Is.EqualTo(confidence));
|
||||
Assert.That(result.RetrievalContext, Is.SameAs(PASSAGE), "The classic RAG process finds the passages it keeps by reference.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheJudgmentIsFoundAmidProseAndACodeFence()
|
||||
{
|
||||
const string ANSWER = """
|
||||
Here is my judgment:
|
||||
|
||||
```json
|
||||
{"decision": true, "reason": "It names the limit for hotels.", "confidence": 0.75}
|
||||
```
|
||||
""";
|
||||
|
||||
var result = RetrievalContextValidationOperation.ReadAnswer(ANSWER, PASSAGE);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(result.IsAnswered, Is.True);
|
||||
Assert.That(result.Reason, Is.EqualTo("It names the limit for hotels."));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AJudgmentWithoutAReasonStillCounts()
|
||||
{
|
||||
var result = RetrievalContextValidationOperation.ReadAnswer("""{"decision": true, "confidence": 0.8}""", PASSAGE);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(result.IsAnswered, Is.True, "The reason only explains the judgment; the judgment stands without it.");
|
||||
Assert.That(result.Reason, Is.Empty);
|
||||
});
|
||||
}
|
||||
|
||||
[TestCase("""{"reason": "It answers the question.", "confidence": 0.87}""", TestName = "Without a decision")]
|
||||
[TestCase("""{"decision": true, "reason": "It answers the question."}""", TestName = "Without a confidence")]
|
||||
[TestCase("""{"decision": true, "reason": "In percent.", "confidence": 87}""", TestName = "With a confidence above 1")]
|
||||
[TestCase("""{"decision": true, "reason": "Negative.", "confidence": -0.1}""", TestName = "With a confidence below 0")]
|
||||
[TestCase("""{"decision": "yes", "reason": "A word instead of a value.", "confidence": 0.8}""", TestName = "With a decision which is no boolean")]
|
||||
[TestCase("""{"decision": true, "reason": "Broken off""", TestName = "With JSON which breaks off")]
|
||||
[TestCase("I think this passage helps a lot.", TestName = "Without any JSON")]
|
||||
[TestCase("", TestName = "With an empty answer")]
|
||||
public void AnAnswerWithoutAUsableJudgmentIsNotAnswered(string answer)
|
||||
{
|
||||
var result = RetrievalContextValidationOperation.ReadAnswer(answer, PASSAGE);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(result.IsAnswered, Is.False, "Reading a value the model never wrote, e.g., false for a missing decision, would drop a passage nobody judged.");
|
||||
Assert.That(result.RetrievalContext, Is.SameAs(PASSAGE));
|
||||
Assert.That(result.Reason, Is.Not.Empty, "The log says why the answer was of no use.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEmptyPromptLeavesEveryPassageUnjudged()
|
||||
{
|
||||
var operation = new RetrievalContextValidationOperation(NullLogger<RetrievalContextValidationOperation>.Instance, new SettingsManager(NullLogger<SettingsManager>.Instance, null!));
|
||||
var request = new RetrievalContextValidationRequest("You are a helpful assistant.", new ContentText { Text = " " }, [PASSAGE, PASSAGE], ConfidenceLevel.NONE);
|
||||
|
||||
var results = await operation.RunAsync(request, new AIOperationContext(AIStudio.Settings.Provider.NONE));
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(results, Has.Count.EqualTo(2));
|
||||
Assert.That(results.Select(result => result.IsAnswered), Is.All.False, "Nobody asked the model, so nothing was judged.");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public static partial class JsonTestRoots
|
||||
|
||||
// The AI operations, which select data sources, check retrieved passages, and audit assistant plugins:
|
||||
yield return Case(new(typeof(List<SelectedDataSource>), AIOperationsJson.OPTIONS));
|
||||
yield return Case(new(typeof(RetrievalContextValidationResult), AIOperationsJson.OPTIONS));
|
||||
yield return Case(new(typeof(RetrievalContextValidationAnswer), AIOperationsJson.OPTIONS));
|
||||
yield return Case(new(typeof(PluginAuditResult), AIOperationsJson.OPTIONS));
|
||||
|
||||
// The Assistant Builder, for assistant plugins and for direct chat launchers:
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
using AIStudio.AIOperations.RetrievalContextValidation;
|
||||
using AIStudio.Tools.RAG;
|
||||
using AIStudio.Tools.RAG.AugmentationProcesses;
|
||||
|
||||
namespace AIStudio.Tests.Tools;
|
||||
|
||||
/// <summary>
|
||||
/// Checks which passages the classic RAG process keeps after a model checked which of them fit the user prompt.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A passage whose check failed once counted as judged with a confidence of 1.0: it always stayed and moved
|
||||
/// the threshold of all the passages which were judged. The check of a passage may fail for many reasons,
|
||||
/// e.g., an answer with broken JSON, and none of them says anything about the passage.<br/><br/>
|
||||
/// The decision of the model was once ignored as well: only the confidence counted, so a passage judged not to
|
||||
/// fit with a confidence of 0.9 stayed, and took the room of a passage which fits.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class ClassicRagValidationTests
|
||||
{
|
||||
[Test]
|
||||
public void NothingToKeepIsNoFailedCheck()
|
||||
{
|
||||
var validated = AugmentationOne.KeepFittingPassages([]);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.Empty);
|
||||
Assert.That(validated.IsUnchecked, Is.False, "Without a passage, no check failed, and the user needs no warning.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenEveryCheckFailsEveryPassageStays()
|
||||
{
|
||||
var passages = Passages(4);
|
||||
var validated = AugmentationOne.KeepFittingPassages(passages.Select(Failed).ToList());
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(passages), "A failed check says nothing about its passage, so the passage stays.");
|
||||
Assert.That(validated.NumUnchecked, Is.EqualTo(4));
|
||||
Assert.That(validated.IsUnchecked, Is.True, "The user switched the check on and hears that it failed for every passage.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FailedChecksDoNotMoveTheThresholdOfTheJudgedPassages()
|
||||
{
|
||||
//
|
||||
// Eight passages fit with little confidence, and two checks failed. When the failed checks counted as
|
||||
// judged with 1.0, only those two passages were better than guessing and set the threshold to 0.5: all
|
||||
// eight judged passages fell out, and only the two which nobody judged were left.
|
||||
//
|
||||
var judged = Passages(8);
|
||||
var failed = Passages(2);
|
||||
var results = judged.Select(passage => Fits(passage, 0.3f)).Concat(failed.Select(Failed)).ToList();
|
||||
|
||||
var validated = AugmentationOne.KeepFittingPassages(results);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(judged.Concat(failed)), "The judged passages are all alike, so none of them is worse than another.");
|
||||
Assert.That(validated.NumJudged, Is.EqualTo(8));
|
||||
Assert.That(validated.NumUnchecked, Is.EqualTo(2));
|
||||
Assert.That(validated.IsUnchecked, Is.False);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WeakPassagesFallBelowTheThresholdWhileUncheckedOnesStay()
|
||||
{
|
||||
var passages = Passages(6);
|
||||
List<RetrievalContextValidationResult> results =
|
||||
[
|
||||
Fits(passages[0], 0.2f),
|
||||
Fits(passages[1], 0.3f),
|
||||
Fits(passages[2], 0.9f),
|
||||
Failed(passages[3]),
|
||||
Fits(passages[4], 0.95f),
|
||||
Fits(passages[5], 1.0f),
|
||||
];
|
||||
|
||||
var validated = AugmentationOne.KeepFittingPassages(results);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(new[] { passages[2], passages[3], passages[4], passages[5] }), "The three strong passages stay, and so does the unchecked one, in the order of the results.");
|
||||
Assert.That(validated.NumBelowThreshold, Is.EqualTo(2));
|
||||
Assert.That(validated.Threshold, Is.EqualTo(0.5f).Within(0.001f));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APassageJudgedNotToFitIsLeftOutHoweverConfident()
|
||||
{
|
||||
var passages = Passages(3);
|
||||
var validated = AugmentationOne.KeepFittingPassages([NotFitting(passages[0], 0.95f), Fits(passages[1], 0.6f), Fits(passages[2], 0.7f)]);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(new[] { passages[1], passages[2] }), "The confidence says how sure the model is of its decision, not how well the passage fits.");
|
||||
Assert.That(validated.NumRejected, Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenEveryPassageIsJudgedNotToFitNoneStays()
|
||||
{
|
||||
var passages = Passages(10);
|
||||
var validated = AugmentationOne.KeepFittingPassages(passages.Select(passage => NotFitting(passage, 0.9f)).ToList());
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.Empty, "The model answers as when the search found nothing.");
|
||||
Assert.That(validated.NumRejected, Is.EqualTo(10));
|
||||
Assert.That(validated.IsUnchecked, Is.False, "Every check worked, so the user needs no warning.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PassagesJudgedNotToFitDoNotMoveTheThresholdOfTheFittingOnes()
|
||||
{
|
||||
//
|
||||
// Eight passages fit with little confidence, and two do not fit with much. When only the confidence
|
||||
// counted, the two were the only ones better than guessing and set the threshold to 0.5: the eight which
|
||||
// fit fell out, and only the two which do not fit were left.
|
||||
//
|
||||
var fitting = Passages(8);
|
||||
var notFitting = Passages(2);
|
||||
var results = fitting.Select(passage => Fits(passage, 0.3f)).Concat(notFitting.Select(passage => NotFitting(passage, 0.9f))).ToList();
|
||||
|
||||
var validated = AugmentationOne.KeepFittingPassages(results);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(fitting));
|
||||
Assert.That(validated.NumRejected, Is.EqualTo(2));
|
||||
Assert.That(validated.NumBelowThreshold, Is.Zero);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AMixOfJudgmentsAndFailedChecksKeepsTheStrongFittingAndTheUncheckedPassages()
|
||||
{
|
||||
var passages = Passages(7);
|
||||
List<RetrievalContextValidationResult> results =
|
||||
[
|
||||
Fits(passages[0], 0.2f),
|
||||
NotFitting(passages[1], 0.9f),
|
||||
Fits(passages[2], 0.9f),
|
||||
Failed(passages[3]),
|
||||
Fits(passages[4], 0.95f),
|
||||
NotFitting(passages[5], 0.6f),
|
||||
Fits(passages[6], 1.0f),
|
||||
];
|
||||
|
||||
var validated = AugmentationOne.KeepFittingPassages(results);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(validated.Contexts, Is.EqualTo(new[] { passages[2], passages[3], passages[4], passages[6] }));
|
||||
Assert.That(validated.NumJudged, Is.EqualTo(6));
|
||||
Assert.That(validated.NumUnchecked, Is.EqualTo(1));
|
||||
Assert.That(validated.NumRejected, Is.EqualTo(2));
|
||||
Assert.That(validated.NumBelowThreshold, Is.EqualTo(1));
|
||||
Assert.That(validated.IsUnchecked, Is.False);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Passages which differ from each other, so that a test can tell which of them were left.
|
||||
/// </summary>
|
||||
private static List<PreparedRetrievalContext> Passages(int count) => Enumerable.Range(1, count).Select(number => new PreparedRetrievalContext(new RetrievalTextContext
|
||||
{
|
||||
DataSourceName = "Handbook",
|
||||
Category = RetrievalContentCategory.TEXT,
|
||||
Type = RetrievalContentType.TEXT_DOCUMENT,
|
||||
Path = Path.GetFullPath(Path.Combine(Path.GetTempPath(), $"handbook-{number}.pdf")),
|
||||
MatchedText = $"Passage {number} of the handbook.",
|
||||
}, $"body {number}", [], null)).ToList();
|
||||
|
||||
private static RetrievalContextValidationResult Fits(PreparedRetrievalContext passage, float confidence) => new(passage, true, true, "It fits.", confidence);
|
||||
|
||||
private static RetrievalContextValidationResult NotFitting(PreparedRetrievalContext passage, float confidence) => new(passage, true, false, "It is about something else.", confidence);
|
||||
|
||||
private static RetrievalContextValidationResult Failed(PreparedRetrievalContext passage) => RetrievalContextValidationResult.NotAnswered(passage, "The AI did not return a response.");
|
||||
}
|
||||
Reference in new issue
Block a user