Accept equivalent integer references in model responses
This commit is contained in:
@@ -2,10 +2,22 @@
|
||||
package jsonnumber
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Integer accepts exact JSON integer values within the native integer range.
|
||||
type Integer int
|
||||
|
||||
func (n *Integer) UnmarshalJSON(raw []byte) error {
|
||||
if value, ok := Int(string(raw)); ok {
|
||||
*n = Integer(value)
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, (*int)(n))
|
||||
}
|
||||
|
||||
// Int accepts lexical integers and equivalent decimal and exponent spellings
|
||||
// in the native integer range. value must be a valid JSON number token.
|
||||
func Int(value string) (int, bool) {
|
||||
|
||||
@@ -143,17 +143,7 @@ func extractSegments(raw []byte) (json.RawMessage, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// sourceID decodes native integers without routing them through float64.
|
||||
// Decimal and exponent spellings are accepted if they denote an exact integer.
|
||||
type sourceID int
|
||||
|
||||
func (id *sourceID) UnmarshalJSON(raw []byte) error {
|
||||
if value, ok := jsonnumber.Int(string(raw)); ok {
|
||||
*id = sourceID(value)
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, (*int)(id))
|
||||
}
|
||||
type sourceID = jsonnumber.Integer
|
||||
|
||||
func validateSourceSegments(segments []SourceSegment) error {
|
||||
for i, s := range segments {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||
@@ -19,9 +20,9 @@ import (
|
||||
|
||||
// StructuredCorrectionProposal is one LLM response correction payload.
|
||||
type StructuredCorrectionProposal struct {
|
||||
SegmentReference int `json:"id"`
|
||||
OriginalText string `json:"original_text"`
|
||||
CorrectedText string `json:"corrected_text"`
|
||||
SegmentReference jsonnumber.Integer `json:"id"`
|
||||
OriginalText string `json:"original_text"`
|
||||
CorrectedText string `json:"corrected_text"`
|
||||
}
|
||||
|
||||
// StructuredCorrectionSet is the reusable structured LLM response model for
|
||||
@@ -134,10 +135,11 @@ func GenerateCandidates(ctx context.Context, req Request) (Result, error) {
|
||||
references := make([]int, 0, len(response.Corrections))
|
||||
corrections := make([]proposals.CorrectionProposal, 0, len(response.Corrections))
|
||||
for _, raw := range response.Corrections {
|
||||
references = append(references, raw.SegmentReference)
|
||||
ref := int(raw.SegmentReference)
|
||||
references = append(references, ref)
|
||||
// Unknown references remain invalid candidates for the existing domain validators.
|
||||
corrections = append(corrections, proposals.CorrectionProposal{
|
||||
TargetSegmentID: proposals.SegmentIDAtReference(req.WorkingTranscript, raw.SegmentReference),
|
||||
TargetSegmentID: proposals.SegmentIDAtReference(req.WorkingTranscript, ref),
|
||||
OriginalText: raw.OriginalText,
|
||||
CorrectedText: raw.CorrectedText,
|
||||
})
|
||||
|
||||
@@ -3,14 +3,15 @@ package proposal_generation
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput"
|
||||
)
|
||||
|
||||
// RepairResponse is one model response for an original proposal reference.
|
||||
type RepairResponse struct {
|
||||
ProposalRef int `json:"proposal_ref"`
|
||||
Decision RepairDecision `json:"decision"`
|
||||
ProposalRef jsonnumber.Integer `json:"proposal_ref"`
|
||||
Decision RepairDecision `json:"decision"`
|
||||
}
|
||||
|
||||
// RepairDecision selects revision or withdrawal for the referenced proposal.
|
||||
@@ -26,7 +27,7 @@ func DecodeRepairResponse(content []byte, expectedRef int) (RepairResponse, erro
|
||||
if err := json.Unmarshal(content, &response); err != nil {
|
||||
return RepairResponse{}, invalidRepairResponse(err)
|
||||
}
|
||||
if response.ProposalRef != expectedRef || (response.Decision.Action != "revise" || response.Decision.Correction == nil) && (response.Decision.Action != "withdraw" || response.Decision.Correction != nil) {
|
||||
if int(response.ProposalRef) != expectedRef || (response.Decision.Action != "revise" || response.Decision.Correction == nil) && (response.Decision.Action != "withdraw" || response.Decision.Correction != nil) {
|
||||
return response, invalidRepairResponse(nil)
|
||||
}
|
||||
return response, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestDecodeRepairResponse(t *testing.T) {
|
||||
}{
|
||||
{"revision with deletion", `{"proposal_ref":2,"decision":{"action":"revise","correction":{"id":1,"original_text":" ","corrected_text":""}}}`, true},
|
||||
{"withdrawal", `{"proposal_ref":2,"decision":{"action":"withdraw","correction":null}}`, true},
|
||||
{"wrong reference", `{"proposal_ref":1,"decision":{"action":"withdraw","correction":null}}`, false},
|
||||
{"wrong reference", `{"proposal_ref":1e0,"decision":{"action":"withdraw","correction":null}}`, false},
|
||||
{"missing revision", `{"proposal_ref":2,"decision":{"action":"revise","correction":null}}`, false},
|
||||
{"withdrawal with revision", `{"proposal_ref":2,"decision":{"action":"withdraw","correction":{"id":1}}}`, false},
|
||||
} {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposal_generation"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
||||
@@ -144,7 +145,7 @@ func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repair
|
||||
buildRequest := func() (*structuredoutput.Request, error) {
|
||||
request := proposal_generation.RepairRequest{
|
||||
ProposalRef: proposalRef,
|
||||
Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: localRef, OriginalText: current.OriginalText, CorrectedText: current.CorrectedText},
|
||||
Correction: proposal_generation.StructuredCorrectionProposal{SegmentReference: jsonnumber.Integer(localRef), OriginalText: current.OriginalText, CorrectedText: current.CorrectedText},
|
||||
Findings: findings,
|
||||
}
|
||||
if pos, ok := input.Section.Position(current.TargetSegmentID); ok {
|
||||
@@ -249,7 +250,7 @@ func runRepairBranch(ctx context.Context, branchInput repairBranchInput) (repair
|
||||
return nil, nil
|
||||
}
|
||||
revision := response.Decision.Correction
|
||||
localRef = revision.SegmentReference
|
||||
localRef = int(revision.SegmentReference)
|
||||
current = original
|
||||
current.CorrectionProposal = proposals.CorrectionProposal{TargetSegmentID: proposals.SegmentIDAtReference(input.WorkingTranscript, localRef), OriginalText: revision.OriginalText, CorrectedText: revision.CorrectedText}
|
||||
if debug {
|
||||
|
||||
@@ -157,7 +157,7 @@ func TestRepairRequestRefreshesPreviewAfterRetarget(t *testing.T) {
|
||||
if err := json.Unmarshal([]byte(client.calls[i].Variables["RepairRequestJSON"]), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.ProposalRef != 1 || payload.Correction.SegmentReference != i+1 || payload.OriginalSegmentText == nil || *payload.OriginalSegmentText != want.before || payload.CorrectedSegmentText == nil || *payload.CorrectedSegmentText != want.after {
|
||||
if payload.ProposalRef != 1 || int(payload.Correction.SegmentReference) != i+1 || payload.OriginalSegmentText == nil || *payload.OriginalSegmentText != want.before || payload.CorrectedSegmentText == nil || *payload.CorrectedSegmentText != want.after {
|
||||
t.Fatalf("attempt %d did not describe its current proposal: %+v", i+1, payload)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
|
||||
domain "gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm"
|
||||
@@ -42,7 +43,7 @@ func (f *fakeLLMClient) CompleteStructured(ctx context.Context, req contracts.St
|
||||
items, _ := payload["corrections"].([]map[string]any)
|
||||
for _, item := range items {
|
||||
target.Corrections = append(target.Corrections, proposal_generation.StructuredCorrectionProposal{
|
||||
SegmentReference: item["id"].(int),
|
||||
SegmentReference: jsonnumber.Integer(item["id"].(int)),
|
||||
OriginalText: item["original_text"].(string),
|
||||
CorrectedText: item["corrected_text"].(string),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user