56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
// Package generatedtext validates structured LLM text and render contexts.
|
|
package generatedtext
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type Hourly struct {
|
|
Summary string `json:"summary"`
|
|
Timing string `json:"timing"`
|
|
Impacts string `json:"impacts"`
|
|
Confidence string `json:"confidence,omitempty"`
|
|
}
|
|
|
|
func ValidateHourly(data []byte) (Hourly, []byte, error) {
|
|
decoder := json.NewDecoder(bytes.NewReader(data))
|
|
decoder.DisallowUnknownFields()
|
|
|
|
var value Hourly
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return Hourly{}, nil, fmt.Errorf("decode hourly generated text: %w", err)
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); err != nil {
|
|
if err != io.EOF {
|
|
return Hourly{}, nil, fmt.Errorf("decode hourly generated text: %w", err)
|
|
}
|
|
} else {
|
|
return Hourly{}, nil, fmt.Errorf("decode hourly generated text: multiple JSON values")
|
|
}
|
|
|
|
value.Summary = strings.TrimSpace(value.Summary)
|
|
value.Timing = strings.TrimSpace(value.Timing)
|
|
value.Impacts = strings.TrimSpace(value.Impacts)
|
|
value.Confidence = strings.TrimSpace(value.Confidence)
|
|
if value.Summary == "" {
|
|
return Hourly{}, nil, fmt.Errorf("hourly generated text summary is required")
|
|
}
|
|
if value.Timing == "" {
|
|
return Hourly{}, nil, fmt.Errorf("hourly generated text timing is required")
|
|
}
|
|
if value.Impacts == "" {
|
|
return Hourly{}, nil, fmt.Errorf("hourly generated text impacts is required")
|
|
}
|
|
|
|
normalized, err := json.Marshal(value)
|
|
if err != nil {
|
|
return Hourly{}, nil, fmt.Errorf("normalize hourly generated text: %w", err)
|
|
}
|
|
return value, normalized, nil
|
|
}
|