Prepare structured D&D reference context
This commit is contained in:
@@ -37,10 +37,21 @@ var _ contracts.Chunker = (*Chunker)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
type Chunker struct{}
|
||||
type Chunker struct{ context shared.PreparedContext }
|
||||
|
||||
func New(_ Options) (*Chunker, error) {
|
||||
return &Chunker{}, nil
|
||||
func New(_ Options, references ...contracts.ReferenceSet) (*Chunker, error) {
|
||||
if len(references) > 1 {
|
||||
return nil, chunkerErrorf("at most one reference set may be supplied")
|
||||
}
|
||||
var set contracts.ReferenceSet
|
||||
if len(references) == 1 {
|
||||
set = references[0]
|
||||
}
|
||||
context, err := shared.PrepareContext(set)
|
||||
if err != nil {
|
||||
return nil, chunkerErrorf("prepare references: %w", err)
|
||||
}
|
||||
return &Chunker{context: context}, nil
|
||||
}
|
||||
|
||||
func (c *Chunker) Key() string {
|
||||
@@ -70,7 +81,7 @@ func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (c
|
||||
if err := source.ValidateDocument(req.Source); err != nil {
|
||||
return nil, chunkerErrorf("validate source document: %w", err)
|
||||
}
|
||||
materials := shared.PromptInputs(req.SourceInput, req.References)
|
||||
materials := c.context.Inputs(req.SourceInput)
|
||||
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
||||
for name, material := range materials {
|
||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||
@@ -121,7 +132,7 @@ func Register(registry *pipeline.ChunkerRegistry) error {
|
||||
if err := shared.ValidateReferenceSelections(request.References); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return New(options)
|
||||
return New(options, request.References)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -89,16 +89,17 @@ func TestRegisterNilRegistryReturnsError(t *testing.T) {
|
||||
|
||||
func wantReferenceSlots() []contracts.ReferenceSlot {
|
||||
accepted := []string{"application/json", "application/x-yaml", "application/yaml", "text/markdown", "text/plain"}
|
||||
structured := []string{"application/json", "application/x-yaml", "application/yaml"}
|
||||
return []contracts.ReferenceSlot{
|
||||
{
|
||||
Name: "glossary",
|
||||
Description: "Optional campaign glossary reference material used only for scene disambiguation.",
|
||||
AcceptedMediaTypes: append([]string(nil), accepted...),
|
||||
AcceptedMediaTypes: append([]string(nil), structured...),
|
||||
},
|
||||
{
|
||||
Name: "party",
|
||||
Description: "Optional party roster reference material used only for scene disambiguation.",
|
||||
AcceptedMediaTypes: append([]string(nil), accepted...),
|
||||
AcceptedMediaTypes: append([]string(nil), structured...),
|
||||
},
|
||||
{
|
||||
Name: "players",
|
||||
@@ -108,7 +109,7 @@ func wantReferenceSlots() []contracts.ReferenceSlot {
|
||||
{
|
||||
Name: "roster",
|
||||
Description: "Deprecated alias for party roster reference material used only for scene disambiguation.",
|
||||
AcceptedMediaTypes: append([]string(nil), accepted...),
|
||||
AcceptedMediaTypes: append([]string(nil), structured...),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -232,26 +233,26 @@ func TestPlanPassesReferencesAsPromptInputs(t *testing.T) {
|
||||
"party": {
|
||||
Slot: contracts.ReferenceSlot{Name: "party"},
|
||||
Items: []contracts.ReferenceItem{
|
||||
{SlotName: "party", Content: []byte("Aria: cleric")},
|
||||
{SlotName: "party", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria","classes":[{"name":"cleric"}]}}}}`)},
|
||||
},
|
||||
},
|
||||
"glossary": {
|
||||
Slot: contracts.ReferenceSlot{Name: "glossary"},
|
||||
Items: []contracts.ReferenceItem{
|
||||
{SlotName: "glossary", Content: []byte("Brightmantle: local temple")},
|
||||
{SlotName: "glossary", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.glossary.v1","collections":{"campaign":{"entries":[{"id":"b","name":"Brightmantle","category":"place","description":"local temple"}]}}}`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := runChunk(context.Background(), newChunker(t, client), req); err != nil {
|
||||
if _, err := runChunk(context.Background(), newChunker(t, client, req.References), req); err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
var messages string
|
||||
for _, message := range client.requests[0].Prompt.Messages {
|
||||
messages += message.Content
|
||||
}
|
||||
for _, reference := range []string{"Alice: Aria", "Aria: cleric", "Brightmantle: local temple"} {
|
||||
for _, reference := range []string{"Alice: Aria", "cleric", "Brightmantle", "local temple"} {
|
||||
if strings.Count(messages, reference) != 1 {
|
||||
t.Fatalf("reference not rendered once: %q", reference)
|
||||
}
|
||||
@@ -259,20 +260,33 @@ func TestPlanPassesReferencesAsPromptInputs(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestPromptInputsMapLegacyRosterReferenceToParty(t *testing.T) {
|
||||
inputs := shared.PromptInputs(sceneSourceInput(), contracts.ReferenceSet{
|
||||
func TestConstructionRejectsInvalidGlossaryReference(t *testing.T) {
|
||||
refs := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"glossary": {Items: []contracts.ReferenceItem{{MediaType: "application/json", Content: []byte("{}")}}},
|
||||
}}
|
||||
if _, err := New(Options{}, refs); err == nil {
|
||||
t.Fatal("invalid glossary accepted during construction")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedContextMapsRosterReferenceToParty(t *testing.T) {
|
||||
prepared, err := shared.PrepareContext(contracts.ReferenceSet{
|
||||
Slots: map[string]contracts.ResolvedReferenceSlot{
|
||||
"roster": {
|
||||
Slot: contracts.ReferenceSlot{Name: "roster"},
|
||||
Items: []contracts.ReferenceItem{
|
||||
{SlotName: "roster", Content: []byte("Legacy roster text")},
|
||||
{SlotName: "roster", MediaType: "application/json", Content: []byte(`{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"Aria"}}}}`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inputs := prepared.Inputs(sceneSourceInput())
|
||||
|
||||
if got := string(inputs["party"].Content); got != "Legacy roster text" {
|
||||
t.Fatalf("party input = %q, want legacy roster content", got)
|
||||
if got := string(inputs["party"].Content); !strings.Contains(got, "Aria") {
|
||||
t.Fatalf("party input = %q, want roster content", got)
|
||||
}
|
||||
if _, ok := inputs["roster"]; ok {
|
||||
t.Fatalf("roster prompt input was present; want only party input")
|
||||
@@ -426,7 +440,7 @@ type chunkFixture struct {
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
|
||||
func newChunker(t *testing.T, client promptkit.LLMClient) *chunkFixture {
|
||||
func newChunker(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *chunkFixture {
|
||||
t.Helper()
|
||||
type settings struct {
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
@@ -450,7 +464,7 @@ func newChunker(t *testing.T, client promptkit.LLMClient) *chunkFixture {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chunker, err := New(Options{})
|
||||
chunker, err := New(Options{}, references...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user