94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
pk "go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
type VerificationStatus string
|
|
|
|
const (
|
|
VerificationSuccess VerificationStatus = "success"
|
|
VerificationNegative VerificationStatus = "negative"
|
|
VerificationUnsupported VerificationStatus = "unsupported"
|
|
VerificationInconclusive VerificationStatus = "inconclusive"
|
|
)
|
|
|
|
type ProfileVerification struct {
|
|
ProfileID string
|
|
Status VerificationStatus
|
|
Report *pk.SmokeTestReport
|
|
Err error
|
|
}
|
|
|
|
type smokeTester interface {
|
|
SmokeTest(context.Context, pk.SmokeTestRequest) (*pk.SmokeTestReport, error)
|
|
}
|
|
|
|
func classifyStructuredOutput(report *pk.SmokeTestReport, err error) VerificationStatus {
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
return VerificationInconclusive
|
|
}
|
|
if err != nil {
|
|
return VerificationNegative
|
|
}
|
|
if report == nil || report.Kind != pk.SmokeTestStructuredOutput {
|
|
return VerificationInconclusive
|
|
}
|
|
if !report.Supported || (report.Applicable != nil && !*report.Applicable) {
|
|
return VerificationUnsupported
|
|
}
|
|
if report.Failure != pk.ExecutionFailureNone {
|
|
return VerificationNegative
|
|
}
|
|
if !report.Completed || report.Applicable == nil {
|
|
return VerificationInconclusive
|
|
}
|
|
for _, fact := range []*bool{report.Facts.GenerationCompleted, report.Facts.ValidJSON, report.Facts.SchemaMatched} {
|
|
if fact != nil && !*fact {
|
|
return VerificationNegative
|
|
}
|
|
}
|
|
for _, fact := range []*bool{report.Facts.GenerationCompleted, report.Facts.ValidJSON, report.Facts.SchemaMatched} {
|
|
if fact == nil {
|
|
return VerificationInconclusive
|
|
}
|
|
}
|
|
return VerificationSuccess
|
|
}
|
|
|
|
// VerifyStructuredOutput checks selected profiles sequentially and retains native evidence.
|
|
func VerifyStructuredOutput(ctx context.Context, engine smokeTester, profiles []string) ([]ProfileVerification, error) {
|
|
var results []ProfileVerification
|
|
var failures []error
|
|
for _, id := range profiles {
|
|
if err := ctx.Err(); err != nil {
|
|
failures = append(failures, err)
|
|
break
|
|
}
|
|
report, err := engine.SmokeTest(ctx, pk.SmokeTestRequest{Kind: pk.SmokeTestStructuredOutput, ProfileID: id})
|
|
status := classifyStructuredOutput(report, err)
|
|
results = append(results, ProfileVerification{ProfileID: id, Status: status, Report: report, Err: err})
|
|
if status != VerificationSuccess {
|
|
if err != nil {
|
|
failures = append(failures, fmt.Errorf("profile %q: %w", id, err))
|
|
} else {
|
|
failures = append(failures, fmt.Errorf("profile %q: structured-output verification %s", id, status))
|
|
}
|
|
}
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
if !errors.Is(err, canceled) {
|
|
failures = append(failures, canceled)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if err := ctx.Err(); err != nil && !errors.Is(errors.Join(failures...), err) {
|
|
failures = append(failures, err)
|
|
}
|
|
return results, errors.Join(failures...)
|
|
}
|