139 lines
5.0 KiB
Go
139 lines
5.0 KiB
Go
package locationregistry
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/assets"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
"go.maximumdirect.net/configkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func extractionRequest() contracts.TypedExtractionRequest[dnd.LocationRegistry] {
|
|
doc := sourceDocument()
|
|
chunk := &source.Chunk{
|
|
ID: "session-locations:chunk:0", SourceID: doc.ID, Index: 0,
|
|
Ref: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 3},
|
|
Content: []byte(`{"units":[1,2,3]}`), MediaType: "application/json",
|
|
Units: append([]source.SourceUnit(nil), doc.Units...),
|
|
}
|
|
return contracts.TypedExtractionRequest[dnd.LocationRegistry]{
|
|
Source: doc, Chunk: chunk,
|
|
SourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-locations.json"),
|
|
SessionID: "location-session", LLMProfile: "location-profile",
|
|
Operation: registryOperation(),
|
|
}
|
|
}
|
|
|
|
func sourceDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{ID: "session-locations", Kind: "transcript", Format: "application/json", Digest: "sha256:test", Units: []source.SourceUnit{
|
|
{ID: 1, Kind: "transcript_segment", Text: "The party enters the Old Mill."},
|
|
{ID: 2, Kind: "transcript_segment", Text: "They leave the old road behind."},
|
|
{ID: 3, Kind: "transcript_segment", Text: "The tavern is quiet."},
|
|
}}
|
|
}
|
|
|
|
func responseSourceRefs(startUnitID, endUnitID int) []locationSourceRefResponse {
|
|
return []locationSourceRefResponse{{StartUnitID: startUnitID, EndUnitID: endUnitID}}
|
|
}
|
|
|
|
func registryOperation() contracts.ProducerOperation[dnd.LocationRegistry] {
|
|
return 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
|
|
},
|
|
}
|
|
}
|
|
|
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
|
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("registry fixture", []byte(`promptkit:
|
|
profiles:
|
|
- id: location-profile
|
|
endpoint: https://example.invalid/v1
|
|
model: registry-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)
|
|
}
|
|
extractor, err := New(engine, Options{}, references...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return extractor
|
|
}
|
|
|
|
func messageContaining(t *testing.T, request promptkit.GenerateRequest, text string) promptkit.RenderedMessage {
|
|
t.Helper()
|
|
for _, message := range request.Prompt.Messages {
|
|
if strings.Contains(message.Content, text) {
|
|
return message
|
|
}
|
|
}
|
|
t.Fatalf("missing rendered input %q", text)
|
|
return promptkit.RenderedMessage{}
|
|
}
|
|
|
|
func mismatchedSourceInputRequest(req contracts.TypedExtractionRequest[dnd.LocationRegistry]) contracts.TypedExtractionRequest[dnd.LocationRegistry] {
|
|
req.SourceInput = contracts.NewLLMInputMaterial("source", "application/json", []byte(`{"different":true}`), "sha256:other", "file:///other.json")
|
|
return req
|
|
}
|
|
|
|
type fakeLocationsLLMClient struct {
|
|
response extractionResponse
|
|
content []byte
|
|
responses []string
|
|
err error
|
|
errors []error
|
|
requests []promptkit.GenerateRequest
|
|
onGenerate func(context.Context, promptkit.GenerateRequest)
|
|
}
|
|
|
|
func (client *fakeLocationsLLMClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
index := len(client.requests)
|
|
client.requests = append(client.requests, request)
|
|
if client.onGenerate != nil {
|
|
client.onGenerate(ctx, request)
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if client.err != nil {
|
|
return nil, client.err
|
|
}
|
|
if index < len(client.errors) && client.errors[index] != nil {
|
|
return nil, client.errors[index]
|
|
}
|
|
content := append([]byte(nil), client.content...)
|
|
if index < len(client.responses) {
|
|
content = []byte(client.responses[index])
|
|
}
|
|
if len(content) == 0 {
|
|
var err error
|
|
content, err = json.Marshal(client.response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return &promptkit.GenerateResponse{Content: string(content)}, nil
|
|
}
|