167 lines
5.6 KiB
Go
167 lines
5.6 KiB
Go
package itemoccurrences
|
|
|
|
import (
|
|
"bytes"
|
|
"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"
|
|
)
|
|
|
|
func extractionRequest() contracts.TypedExtractionRequest[dnd.ItemOccurrenceList] {
|
|
doc := sourceDocument()
|
|
chunk := &source.Chunk{
|
|
ID: "session-alpha:chunk:0",
|
|
SourceID: doc.ID,
|
|
Index: 0,
|
|
Ref: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 5},
|
|
Content: []byte(`{"segments":[1,2,3,4,5]}`),
|
|
MediaType: "application/json",
|
|
Units: append([]source.SourceUnit(nil), doc.Units...),
|
|
}
|
|
return contracts.TypedExtractionRequest[dnd.ItemOccurrenceList]{
|
|
Source: doc,
|
|
Chunk: chunk,
|
|
SourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:chunk", "file:///session-alpha.json"),
|
|
SessionID: "session-123",
|
|
LLMProfile: "profile-item-occurrences",
|
|
Operation: contracts.ProducerOperation[dnd.ItemOccurrenceList]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.ItemOccurrenceList) (contracts.CandidateAssessment, error) {
|
|
return contracts.CandidateAssessment{}, nil
|
|
}},
|
|
}
|
|
}
|
|
|
|
func sourceDocument() *source.SourceDocument {
|
|
return &source.SourceDocument{
|
|
ID: "session-alpha", Kind: "transcript", Format: "application/vnd.seriatim.minimal+json", Digest: "sha256:test",
|
|
Units: []source.SourceUnit{
|
|
{ID: 1, Kind: "transcript_segment", Text: "The party discovers a hidden cache."},
|
|
{ID: 2, Kind: "transcript_segment", Text: "They acquire 20 gold pieces."},
|
|
{ID: 3, Kind: "transcript_segment", Text: "Aria gives Borin the moonblade."},
|
|
{ID: 4, Kind: "transcript_segment", Text: "The party consumes a healing potion."},
|
|
{ID: 5, Kind: "transcript_segment", Text: "The party loses a torch."},
|
|
},
|
|
}
|
|
}
|
|
|
|
func responseRefs(start, end int) []itemOccurrenceSourceRefResponse {
|
|
return []itemOccurrenceSourceRefResponse{{StartUnitID: start, EndUnitID: end}}
|
|
}
|
|
|
|
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("occurrence fixture", []byte(`promptkit:
|
|
profiles:
|
|
- id: profile-item-occurrences
|
|
endpoint: https://example.invalid/v1
|
|
model: occurrence-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 renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
|
|
t.Helper()
|
|
key := map[string]string{"transcript": "segments", "item_registry": "items"}[name]
|
|
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 bytes.TrimSpace(content)
|
|
}
|
|
}
|
|
t.Fatalf("missing rendered %s input", name)
|
|
return nil
|
|
}
|
|
|
|
type fakeItemOccurrencesLLMClient struct {
|
|
response extractionResponse
|
|
responses []string
|
|
content []byte
|
|
err error
|
|
errors []error
|
|
requests []promptkit.GenerateRequest
|
|
onGenerate func(context.Context, promptkit.GenerateRequest)
|
|
}
|
|
|
|
func (client *fakeItemOccurrencesLLMClient) 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 := string(client.content)
|
|
if index < len(client.responses) {
|
|
content = client.responses[index]
|
|
}
|
|
if content == "" {
|
|
encoded, err := completeResponse(client.response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
content = string(encoded)
|
|
}
|
|
return &promptkit.GenerateResponse{Content: content}, nil
|
|
}
|
|
|
|
func completeResponse(response extractionResponse) ([]byte, error) {
|
|
content, err := json.Marshal(response)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var wire struct {
|
|
Occurrences []map[string]any `json:"occurrences"`
|
|
}
|
|
if err := json.Unmarshal(content, &wire); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, occurrence := range wire.Occurrences {
|
|
for _, name := range []string{"quantity", "from", "to"} {
|
|
if _, present := occurrence[name]; !present {
|
|
occurrence[name] = nil
|
|
}
|
|
}
|
|
}
|
|
return json.Marshal(wire)
|
|
}
|