533db535f1
build / build (push) Successful in 1m12s
按已确认需求提供独立安全模型配置、真实探测、正式字典字段评估与原问题译中,持久批次支持只补空白、暂停恢复、有限重试、幂等和逐字段依据。覆盖固定规则、本地语言、组织关联与主表有效看板共用治理侧读模型,保留源生产和冻结历史。 验证:完整 make verify 退出0,702项Go与329项前端通过;默认118项外部/隔离集成明确跳过,另跑完整镜像及组织隔离PG竞态。12并发、输入漂移、过期租约、暂停/恢复及有限重试通过。用户授权的百炼 deepseek-v4.1-flash 完成真实后台与浏览器评估/翻译;552行、16列、11块看板及完整PNG、独立配置页测试/保存、重开恢复和零JS异常通过。仅非业务样本外发,自动处理关闭,原payload不变。 上线前治理库已做导出快照原生备份并真实恢复,60表行数与17个QA表内容哈希一致;不修改DobotAI生产或隧道。本提交时尚未部署,线上验收须独立完成。
132 lines
4.4 KiB
Go
132 lines
4.4 KiB
Go
package qaopsai
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"knowledge-governance/internal/strictjson"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const DictionaryVersion = "qa-approved-labels-v1"
|
|
const EvaluationPromptVersion = "qa-evaluate-evidence-v1"
|
|
const TranslationPromptVersion = "qa-question-zh-v1"
|
|
|
|
var Taxonomy = map[string][]string{
|
|
"is_correct": {"是", "否", "待确认"},
|
|
"product_category": {"CR/CRA/H/AP", "CRV", "CR配件", "Nova及系列", "Nova配件", "MG400", "MG400配件", "M1 Pro", "VX500", "新示教器", "DobotStudio Pro", "仿真软件", "维护工具", "教育所有产品", "人形机器人", "四六足机器人", "X-Trainer", "码垛解决方案", "码垛插件", "焊接解决方案", "焊接插件", "移动复合机器人", "生态插件", "生态合作", "安全皮肤", "未知"},
|
|
"problem_type": {"参数", "故障", "使用配置", "二次开发", "资料获取", "咨询", "认证", "案例", "售后", "培训与学习", "其他"},
|
|
"root_cause": {"知识缺失", "检索不到", "结果不清", "答案生成错误", "问题质量差", "非知识范围", "知识过期"},
|
|
}
|
|
|
|
func allowedLabel(field, value string) bool {
|
|
for _, label := range Taxonomy[field] {
|
|
if label == value {
|
|
return true
|
|
}
|
|
}
|
|
return field == "root_cause" && value == ""
|
|
}
|
|
func decodeContract(raw string, target any) error {
|
|
if !utf8.ValidString(raw) || len(raw) > 4<<20 {
|
|
return ErrInvalid
|
|
}
|
|
check := json.NewDecoder(strings.NewReader(raw))
|
|
if strictjson.Walk(check) != nil {
|
|
return ErrInvalid
|
|
}
|
|
var extra any
|
|
if check.Decode(&extra) != io.EOF {
|
|
return ErrInvalid
|
|
}
|
|
decoder := json.NewDecoder(strings.NewReader(raw))
|
|
decoder.DisallowUnknownFields()
|
|
if decoder.Decode(target) != nil || decoder.Decode(&extra) != io.EOF {
|
|
return ErrInvalid
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Quote struct {
|
|
Source string `json:"source"`
|
|
Text string `json:"text"`
|
|
ReferenceIndex int `json:"reference_index"`
|
|
}
|
|
type Reference struct {
|
|
Title string `json:"title"`
|
|
Excerpt string `json:"excerpt"`
|
|
}
|
|
type FieldEvaluation struct {
|
|
Value string `json:"value"`
|
|
Basis string `json:"basis"`
|
|
Reason string `json:"reason"`
|
|
Quotes []Quote `json:"quotes"`
|
|
}
|
|
type Assessment struct {
|
|
Fields map[string]FieldEvaluation `json:"fields"`
|
|
}
|
|
type AssessmentInput struct {
|
|
Question string `json:"question"`
|
|
Answer string `json:"answer"`
|
|
References []Reference `json:"references"`
|
|
NeededFields []string `json:"needed_fields"`
|
|
CurrentCorrectness string `json:"current_correctness"`
|
|
}
|
|
|
|
func DecodeAssessment(raw string, input AssessmentInput) (Assessment, error) {
|
|
var out Assessment
|
|
if decodeContract(raw, &out) != nil || out.Fields == nil || len(out.Fields) != len(input.NeededFields) {
|
|
return out, ErrInvalid
|
|
}
|
|
needed := map[string]bool{}
|
|
for _, field := range input.NeededFields {
|
|
if _, ok := Taxonomy[field]; !ok || needed[field] {
|
|
return out, ErrInvalid
|
|
}
|
|
needed[field] = true
|
|
}
|
|
correctness := input.CurrentCorrectness
|
|
if f, ok := out.Fields["is_correct"]; ok {
|
|
correctness = f.Value
|
|
}
|
|
for field, value := range out.Fields {
|
|
if !needed[field] || !allowedLabel(field, value.Value) || strings.TrimSpace(value.Reason) == "" || utf8.RuneCountInString(value.Reason) > 2000 || len(value.Quotes) > 8 {
|
|
return out, ErrInvalid
|
|
}
|
|
if value.Basis != "semantic_only" && value.Basis != "reference_evidence" && value.Basis != "insufficient_evidence" {
|
|
return out, ErrInvalid
|
|
}
|
|
if field == "root_cause" && correctness == "是" && value.Value != "" {
|
|
return out, ErrInvalid
|
|
}
|
|
referenceEvidence := false
|
|
for _, quote := range value.Quotes {
|
|
source := ""
|
|
switch quote.Source {
|
|
case "question":
|
|
source = input.Question
|
|
case "answer":
|
|
source = input.Answer
|
|
case "reference":
|
|
if quote.ReferenceIndex < 0 || quote.ReferenceIndex >= len(input.References) {
|
|
return out, ErrInvalid
|
|
}
|
|
source = input.References[quote.ReferenceIndex].Excerpt
|
|
referenceEvidence = true
|
|
default:
|
|
return out, ErrInvalid
|
|
}
|
|
if strings.TrimSpace(quote.Text) == "" || utf8.RuneCountInString(quote.Text) > 2000 || !strings.Contains(source, quote.Text) {
|
|
return out, ErrInvalid
|
|
}
|
|
}
|
|
if value.Basis == "reference_evidence" && !referenceEvidence {
|
|
return out, ErrInvalid
|
|
}
|
|
if field == "is_correct" && value.Value == "是" && value.Basis != "reference_evidence" {
|
|
return out, ErrInvalid
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|