09b2c1dce5
build / build (push) Successful in 1m17s
修正筛选入口位置:五项条件在表头就地选择,保持完整集筛选与无感续载。 统一固定规则:未覆盖一律待确认,原问答/人工模型证据不改写,已覆盖仍保留点踩优先。 验证:行为红绿、前端/Go全套及隔离PostgreSQL全mirror竞态通过;完整本地门禁与原生备份真实恢复通过。
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package qaopsai
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"knowledge-governance/internal/qaopsfields"
|
|
"knowledge-governance/internal/strictjson"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const DictionaryVersion = "qa-approved-labels-v1"
|
|
const EvaluationPromptVersion = "qa-evaluate-question-answer-v3"
|
|
const TranslationPromptVersion = "qa-question-zh-v1"
|
|
|
|
var Taxonomy = qaopsfields.ClassificationTaxonomy
|
|
|
|
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"`
|
|
Coverage string `json:"coverage"`
|
|
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 == "is_correct" && ((value.Value == "待确认") != (input.Coverage == "uncovered")) {
|
|
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
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|