73 lines
4.9 KiB
C#
73 lines
4.9 KiB
C#
using System.Net.Http.Headers;
|
||
using System.Text;
|
||
using System.Text.Json;
|
||
|
||
namespace SopHelper.Engine.Sop;
|
||
|
||
/// <summary>对接 E:\LayaAI 的图片上传接口,保留模型拒答与复核语义。</summary>
|
||
public sealed class LayaVisionDecisionClient(HttpClient http) : IVisionDecisionClient
|
||
{
|
||
public async Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken cancellationToken)
|
||
{
|
||
var endpoint = Environment.GetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT")
|
||
?? "http://127.0.0.1:8000/v1/vision/decide";
|
||
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https"))
|
||
throw new InvalidOperationException("Laya 视觉接口地址无效");
|
||
if (request.Jpeg.Length is 0 or > 10 * 1024 * 1024)
|
||
throw new InvalidOperationException("Laya 判定图必须介于 1 字节与 10MB 之间");
|
||
|
||
var questions = new
|
||
{
|
||
step_completed = new
|
||
{
|
||
type = "noul",
|
||
instructions = $"请只依据图片判断当前步骤是否已经完成。步骤:{request.Step}。说明:{request.Description}。完成判据:{request.Criteria}。所有判据均须满足;遮挡、不确定、看不到关键部位时回答否。手部动作本身不能作为完成证据。"
|
||
}
|
||
};
|
||
using var message = new HttpRequestMessage(HttpMethod.Post, uri);
|
||
using var form = new MultipartFormDataContent();
|
||
form.Add(new StringContent(JsonSerializer.Serialize(questions), Encoding.UTF8), "questions");
|
||
var image = new ByteArrayContent(request.Jpeg);
|
||
image.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
||
form.Add(image, "file", $"frame-{request.FrameId}.jpg");
|
||
message.Content = form;
|
||
var key = Environment.GetEnvironmentVariable("SOPHELPER_VISION_API_KEY");
|
||
if (!string.IsNullOrWhiteSpace(key)) message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", key);
|
||
using var response = await http.SendAsync(message, cancellationToken).ConfigureAwait(false);
|
||
if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"Laya 视觉接口 HTTP {(int)response.StatusCode}");
|
||
using var document = JsonDocument.Parse(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
|
||
var root = document.RootElement;
|
||
if (root.GetProperty("modality").GetString() != "image") throw new FormatException("Laya 未返回图片判定");
|
||
var calibrated = root.GetProperty("vision_calibrated").GetBoolean();
|
||
var answer = root.GetProperty("answers").GetProperty("step_completed");
|
||
if (answer.GetProperty("type").GetString() != "noul") throw new FormatException("Laya 未返回是非判定");
|
||
var probability = answer.GetProperty("noul").GetDouble();
|
||
if (!double.IsFinite(probability) || probability is < 0 or > 1) throw new FormatException("Laya 完成概率无效");
|
||
var abstain = answer.GetProperty("abstain").GetBoolean();
|
||
var unbound = answer.GetProperty("unbound").GetBoolean();
|
||
var route = answer.GetProperty("route").GetString();
|
||
var experimental = string.Equals(Environment.GetEnvironmentVariable("SOPHELPER_LAYA_ALLOW_UNCALIBRATED"), "true", StringComparison.OrdinalIgnoreCase);
|
||
// 仅显式开启工位实验时允许未标定的 review 结果;仍不绕过拒答、无界或其他路由。
|
||
var acceptedRoute = route == "auto" && calibrated || route == "review" && !calibrated && experimental;
|
||
var completed = probability >= 0.5 && !abstain && !unbound && acceptedRoute;
|
||
var reason = $"Laya 完成概率 {probability:P1},route={route},图片标定={(calibrated ? "是" : "否")}";
|
||
if (abstain || unbound) reason += ",模型拒答或结果无界,未放行";
|
||
else if (!acceptedRoute) reason += ",需要复核,未放行";
|
||
else if (!calibrated) reason += ",按显式开启的未标定实验模式判断";
|
||
return new(completed, probability, reason);
|
||
}
|
||
}
|
||
|
||
/// <summary>每次取图时读取配置,切换服务无需替换引擎实例。</summary>
|
||
public sealed class ConfiguredVisionDecisionClient(HttpClient http) : IVisionDecisionClient
|
||
{
|
||
public Task<VisionDecisionResult> DecideAsync(VisionDecisionRequest request, CancellationToken cancellationToken)
|
||
=> Environment.GetEnvironmentVariable("SOPHELPER_VISION_PROVIDER")?.ToLowerInvariant() switch
|
||
{
|
||
"d3" or "d3-lite" => new D3VisionDecisionClient(http).DecideAsync(request, cancellationToken),
|
||
"laya" => new LayaVisionDecisionClient(http).DecideAsync(request, cancellationToken),
|
||
null or "" or "chat-completions" => new ChatCompletionsVisionClient(http).DecideAsync(request, cancellationToken),
|
||
_ => throw new InvalidOperationException("SOPHELPER_VISION_PROVIDER 仅支持 d3-lite、laya 或 chat-completions")
|
||
};
|
||
}
|