74 lines
6.7 KiB
C#
74 lines
6.7 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using SopHelper.Engine.SopAgent;
|
|
|
|
internal static class ReferenceToolChecks
|
|
{
|
|
public static async Task RunAsync(SopVideoSample video)
|
|
{
|
|
var sample = video with { Frames = SopVideoSampler.ReadFrames(video, [.5, 1.5, 2.5, 3.5], 0, default) };
|
|
var current = new SopVideoDraft { Steps = Enumerable.Range(0, 4).Select(i => new SopDraftStep
|
|
{ Name = "保留工艺" + i, Description = "主体与可见完成关系", Region = "work", StartSeconds = i, EndSeconds = i + 1, EvidenceFrame = i }).ToList() };
|
|
foreach (var mode in new[] { "valid", "renamed", "bad_evidence", "extra_argument", "out_of_focus", "advance", "advance_changed", "drop_negative", "earlier_negative", "follow_failure", "equivalent_defaults", "auto_replay" })
|
|
{
|
|
if (mode is "drop_negative" or "earlier_negative") current.Steps[2].IncompleteEvidenceFrame = 1;
|
|
else current.Steps[2].IncompleteEvidenceFrame = null;
|
|
var candidate = JsonSerializer.Deserialize<SopVideoDraft>(JsonSerializer.Serialize(current))!;
|
|
if (mode == "drop_negative") candidate.Steps[2].IncompleteEvidenceFrame = null;
|
|
if (mode == "earlier_negative") candidate.Steps[2].IncompleteEvidenceFrame = 0;
|
|
if (mode == "auto_replay") candidate.Steps[0].Description += ",最新候选状态";
|
|
if (mode == "renamed") candidate.Steps[0].Name = "另一工艺";
|
|
if (mode == "bad_evidence") candidate.Steps[0].EvidenceFrame = 99;
|
|
using var handler = new ProbeHttp(current, candidate, mode);
|
|
using var http = new HttpClient(handler);
|
|
var checks = 0; var replays = 0;
|
|
var result = await new SopVideoAgentClient(http).ChatAsync(new("通用流程", "保留顺序", sample, ["work"],
|
|
UserMessage: "回测并修订", CurrentDraft: current, VerifyCurrentDraftFirst: true, AutoReplayAfterReference: mode == "auto_replay",
|
|
VerifyDraft: (_, _, _) => { replays++; return Task.FromResult(new SopDraftVerification(replays > 1, "真实回测", [new(1, "保留工艺", replays > 1, "结果")], [])); },
|
|
CheckReference: (_, observed, id, _) =>
|
|
{
|
|
checks++;
|
|
Check(observed.Frames.Count == 4 && (id == 1 || mode is "advance" or "equivalent_defaults" && id == 3 || mode == "follow_failure" && id is 2 or 3), "候选参考检查只能使用实际读取的证据");
|
|
if (mode is "advance" or "advance_changed" or "follow_failure" or "equivalent_defaults")
|
|
return Task.FromResult<IReadOnlyList<SopDraftReferenceCheck>>(new[] { id, id + 1 }.SelectMany(step => new SopDraftReferenceCheck[]
|
|
{ new(step, step - 1, step - .5, "work", true, .95, "完成参考", sample.Frames[step - 1].Jpeg),
|
|
new(step, -1, 0, "work", mode == "follow_failure" && id == 2 && step == 3, .1, "前序反例", [], "previous_state") }).ToArray());
|
|
return Task.FromResult<IReadOnlyList<SopDraftReferenceCheck>>([new(1, 0, .5, "work", true, .95, "静态参考通过", sample.Frames[0].Jpeg),
|
|
new(1, -1, 0, "work", false, .1, "起始对照不满足", [], "previous_state")]);
|
|
}), new("https://probe.invalid/v1", "model", "", ContextLength: 131702), default);
|
|
Check(result.Draft?.Verification?.Passed == true && replays == 2 && checks == (mode is "advance" or "follow_failure" or "equivalent_defaults" ? 2 : mode is "valid" or "advance_changed" or "auto_replay" ? 1 : 0),
|
|
"参考检查不替代真实回放,改工艺、伪造证据或额外路径参数不得进入本地检查");
|
|
if (mode == "auto_replay")
|
|
{
|
|
Check(handler.Bodies.Count == 1 && replays == 2 && result.Draft!.Steps[0].Description == candidate.Steps[0].Description, "参考正反例通过后须直接回放,不能再等待模型确认或重复读图;通过仍来自真实回测");
|
|
continue;
|
|
}
|
|
using var final = JsonDocument.Parse(handler.Bodies[1]);
|
|
var resultText = final.RootElement.GetProperty("messages").EnumerateArray().First(m => m.GetProperty("role").GetString() == "tool").GetProperty("content").GetString()!;
|
|
Check(resultText.Contains(mode is "valid" or "advance" or "advance_changed" or "follow_failure" or "equivalent_defaults" ? "reference_checks" : "error"), "真实参考结果或具体参数错误须反馈给模型");
|
|
}
|
|
Console.WriteLine("PASS: 候选参考工具、正反对照逐步推进与修改失效、真实回放不可替代、工艺/证据/参数保护与完整结果反馈");
|
|
}
|
|
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
|
private sealed class ProbeHttp(SopVideoDraft current, SopVideoDraft candidate, string mode) : HttpMessageHandler
|
|
{
|
|
public List<string> Bodies { get; } = [];
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
|
|
{
|
|
Bodies.Add(await request.Content!.ReadAsStringAsync(token));
|
|
if (mode == "advance_changed" && Bodies.Count == 2) candidate.Steps[0].Description = "已改变的要求";
|
|
if (mode == "equivalent_defaults" && Bodies.Count == 2)
|
|
foreach (var step in candidate.Steps.Take(2)) step.Inspection = new() { EvidenceMode = "single_frame", CheckWindowMs = 14000, ObservationMaxFrames = 24, ObservationMaxMs = 10000, ObservationRequestMaxFrames = 12 };
|
|
var arguments = JsonSerializer.Serialize(new { step_id = mode == "out_of_focus" ? 4
|
|
: mode is "advance" or "advance_changed" or "follow_failure" or "equivalent_defaults" && Bodies.Count == 2 ? 3 : mode == "follow_failure" ? 2 : 1, draft = candidate });
|
|
if (mode == "extra_argument") arguments = arguments[..^1] + ",\"path\":\"other.avi\"}";
|
|
object response = Bodies.Count == 1 || mode is "advance" or "advance_changed" or "follow_failure" or "equivalent_defaults" && Bodies.Count == 2
|
|
? new { choices = new[] { new { finish_reason = "tool_calls", message = new { role = "assistant", content = (string?)null,
|
|
tool_calls = new[] { new { id = "probe", type = "function", function = new { name = "check_draft_reference", arguments } } } } } } }
|
|
: new { choices = new[] { new { finish_reason = "stop", message = new { content = JsonSerializer.Serialize(new { reply = "依据检查修订", draft = current }) } } } };
|
|
return new(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(response), Encoding.UTF8, "application/json") };
|
|
}
|
|
}
|
|
}
|