126 lines
4.5 KiB
Go
126 lines
4.5 KiB
Go
package locationregistry
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
|
"go.maximumdirect.net/configkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
)
|
|
|
|
type recordingLocationNormalizerClient struct {
|
|
response string
|
|
responses []string
|
|
err error
|
|
errors []error
|
|
requests []promptkit.GenerateRequest
|
|
onGenerate func(context.Context, promptkit.GenerateRequest)
|
|
}
|
|
|
|
func (c *recordingLocationNormalizerClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
index := len(c.requests)
|
|
c.requests = append(c.requests, request)
|
|
if c.onGenerate != nil {
|
|
c.onGenerate(ctx, request)
|
|
}
|
|
if c.err != nil {
|
|
return nil, c.err
|
|
}
|
|
if index < len(c.errors) && c.errors[index] != nil {
|
|
return nil, c.errors[index]
|
|
}
|
|
response := c.response
|
|
if index < len(c.responses) {
|
|
response = c.responses[index]
|
|
}
|
|
if response == "" {
|
|
response = `{"duplicate_groups":[]}`
|
|
}
|
|
return &promptkit.GenerateResponse{Content: response}, nil
|
|
}
|
|
|
|
func newNormalizer(t *testing.T, client promptkit.LLMClient) *Normalizer {
|
|
t.Helper()
|
|
type settings struct {
|
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
|
}
|
|
definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit })
|
|
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
|
|
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("normalizer fixture", []byte(`promptkit:
|
|
profiles:
|
|
- id: location-normalize-test
|
|
endpoint: https://example.invalid/v1
|
|
model: normalization-test-model
|
|
api_key_required: false
|
|
`))}, configkit.LayeredOptions[settings]{Definition: definition, Underlays: []configkit.Underlay{
|
|
promptkit.BundledOpenRouterUnderlay(),
|
|
{Inputs: []configkit.Input{{Source: filesystem.File("notarius.yml"), Imports: true, Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."}}}},
|
|
}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
normalizer, err := New(engine, Options{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return normalizer
|
|
}
|
|
|
|
func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
|
|
t.Helper()
|
|
key := "candidate_number_range"
|
|
if name == "transcript" {
|
|
key = "windows"
|
|
}
|
|
for _, message := range request.Prompt.Messages {
|
|
start := strings.Index(message.Content, "{")
|
|
if start < 0 {
|
|
continue
|
|
}
|
|
content := []byte(message.Content[start:])
|
|
var fields map[string]json.RawMessage
|
|
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
|
return content
|
|
}
|
|
}
|
|
t.Fatalf("missing rendered %s input", name)
|
|
return nil
|
|
}
|
|
func normalizeRequest(value dnd.LocationRegistry) contracts.TypedNormalizeRequest[dnd.LocationRegistry] {
|
|
return contracts.TypedNormalizeRequest[dnd.LocationRegistry]{
|
|
Source: &source.SourceDocument{}, LLMProfile: "location-normalize-test",
|
|
Operation: contracts.ProducerOperation[dnd.LocationRegistry]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.LocationRegistry) (contracts.CandidateAssessment, error) {
|
|
return contracts.CandidateAssessment{}, nil
|
|
}},
|
|
MergeOutput: contracts.MergeArtifact[dnd.LocationRegistry]{Value: value},
|
|
}
|
|
}
|
|
|
|
func normalizeRequestWithSource(value dnd.LocationRegistry, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.LocationRegistry] {
|
|
request := normalizeRequest(value)
|
|
request.Source = doc
|
|
return request
|
|
}
|
|
func semanticDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{ID: "location-session", Units: []source.SourceUnit{{ID: 10, Kind: "speech", Text: "The old mill is the Greencloak's refuge."}, {ID: 20, Kind: "speech", Text: "The mill stands on the northern road."}, {ID: 30, Kind: "speech", Text: "The tavern is beside the mill."}, {ID: 40, Kind: "speech", Text: "The mill's cellar is flooded."}}}
|
|
}
|
|
func hasDiagnostic(diagnostics []contracts.ProducerDiagnostic, reason string, disposition contracts.DiagnosticDisposition) bool {
|
|
for _, diagnostic := range diagnostics {
|
|
if diagnostic.ReasonCode == reason && diagnostic.Disposition == disposition {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|