feat(qa): support synthesis-wide chat context (#63)
This commit is contained in:
11
backend/internal/summaryqa/context.go
Normal file
11
backend/internal/summaryqa/context.go
Normal file
@ -0,0 +1,11 @@
|
||||
package summaryqa
|
||||
|
||||
// ContextBytes measures the original UTF-8 content, not rune count or JSON
|
||||
// escaping. Both adapters use the same bound and must never trim chat context.
|
||||
func (r StatelessRequest) ContextBytes() int {
|
||||
size := len(r.Summary) + len(r.Passage) + len(r.Question)
|
||||
for _, turn := range r.History {
|
||||
size += len(turn.Passage) + len(turn.Question) + len(turn.Answer)
|
||||
}
|
||||
return size
|
||||
}
|
||||
@ -33,7 +33,7 @@ func (s *Service) Delete(ctx context.Context, owner, summary string) error {
|
||||
var requestIDPattern = regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
|
||||
|
||||
func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (Turn, error) {
|
||||
if !requestIDPattern.MatchString(q.RequestID) || !utf8.ValidString(q.Passage) || !utf8.ValidString(q.Question) || strings.ContainsRune(q.Passage, 0) || strings.ContainsRune(q.Question, 0) || strings.TrimSpace(q.Passage) == "" || strings.TrimSpace(q.Question) == "" || utf8.RuneCountInString(q.Passage) > MaxPassageRunes || utf8.RuneCountInString(q.Question) > MaxQuestionRunes {
|
||||
if !requestIDPattern.MatchString(q.RequestID) || !utf8.ValidString(q.Passage) || !utf8.ValidString(q.Question) || strings.ContainsRune(q.Passage, 0) || strings.ContainsRune(q.Question, 0) || strings.TrimSpace(q.Question) == "" || utf8.RuneCountInString(q.Passage) > MaxPassageRunes || utf8.RuneCountInString(q.Question) > MaxQuestionRunes {
|
||||
return Turn{}, ErrInvalid
|
||||
}
|
||||
q.RequestID = strings.ToLower(q.RequestID)
|
||||
@ -64,16 +64,7 @@ func (s *Service) Ask(ctx context.Context, owner, summary string, q Question) (T
|
||||
return s.Repo.FinishQAQuestion(saveCtx, owner, summary, work.Turn.ID, answer, code, s.Clock())
|
||||
}
|
||||
request := StatelessRequest{Summary: work.Summary, Language: work.Language, History: work.History, Passage: q.Passage, Question: q.Question, UseWebSearch: q.UseWebSearch}
|
||||
// Web-enabled requests deliberately exclude the full synthesis and history.
|
||||
if q.UseWebSearch {
|
||||
request.Summary = ""
|
||||
request.History = nil
|
||||
}
|
||||
size := len(request.Summary) + len(request.Passage) + len(request.Question)
|
||||
for _, turn := range request.History {
|
||||
size += len(turn.Passage) + len(turn.Question) + len(turn.Answer)
|
||||
}
|
||||
if size > MaxContextBytes {
|
||||
if request.ContextBytes() > MaxContextBytes {
|
||||
if _, err = finish(AnswerResult{}, ErrLimit.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
|
||||
68
backend/internal/summaryqa/synthesis_chat_ticket63_test.go
Normal file
68
backend/internal/summaryqa/synthesis_chat_ticket63_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
package summaryqa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket63OptionalPassageAndFullContextInBothModes(t *testing.T) {
|
||||
for _, web := range []bool{false, true} {
|
||||
for _, passage := range []string{"", "attached passage"} {
|
||||
t.Run(map[bool]string{false: "normal", true: "web"}[web]+"/"+passage, func(t *testing.T) {
|
||||
history := []Exchange{{Question: "earlier question", Answer: "earlier answer"}, {Passage: "earlier attachment", Question: "second question", Answer: "second answer"}}
|
||||
r := &fixtureRepo{work: Work{Summary: "complete synthesis", History: history, Language: "fr", Turn: Turn{ID: "turn"}}}
|
||||
p := &web60ServiceProvider{result: AnswerResult{Answer: "answer"}}
|
||||
_, err := New(r, &fixtureResolver{p: p}).Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000006301", Question: "general question", Passage: passage, UseWebSearch: web})
|
||||
if err != nil || len(p.requests) != 1 {
|
||||
t.Fatalf("optional passage rejected: %v", err)
|
||||
}
|
||||
got := p.requests[0]
|
||||
if got.Summary != r.work.Summary || !reflect.DeepEqual(got.History, history) || got.Passage != passage || got.UseWebSearch != web {
|
||||
t.Fatal("full context changed or dropped")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket63ExactContextByteBoundaryBeforeResolutionAndCall(t *testing.T) {
|
||||
for _, web := range []bool{false, true} {
|
||||
for _, delta := range []int{-1, 0, 1} {
|
||||
history := []Exchange{{Passage: "ancien", Question: "pourquoi", Answer: "réponse"}, {Question: "next", Answer: "next answer"}}
|
||||
q := Question{RequestID: "00000000-0000-0000-0000-000000006301", Question: "é", Passage: "joint", UseWebSearch: web}
|
||||
fixed := len(q.Question) + len(q.Passage)
|
||||
for _, h := range history {
|
||||
fixed += len(h.Passage) + len(h.Question) + len(h.Answer)
|
||||
}
|
||||
r := "a55Repo{fixtureRepo: fixtureRepo{work: Work{Summary: strings.Repeat("s", MaxContextBytes-fixed+delta), History: history, Language: "fr", Turn: Turn{ID: "turn"}}}}
|
||||
p := &web60ServiceProvider{result: AnswerResult{Answer: "answer"}}
|
||||
resolver := &fixtureResolver{p: p}
|
||||
_, err := New(r, resolver).Ask(context.Background(), "owner", "summary", q)
|
||||
if delta > 0 {
|
||||
if !errors.Is(err, ErrLimit) || resolver.calls != 0 || r.admissions != 0 || len(p.requests) != 0 || r.code != "qa_context_limit" {
|
||||
t.Fatalf("web=%v over-limit request reached provider: %v", web, err)
|
||||
}
|
||||
} else if err != nil || len(p.requests) != 1 || p.requests[0].Summary != r.work.Summary || !reflect.DeepEqual(p.requests[0].History, history) {
|
||||
t.Fatalf("web=%v delta=%d context rejected/truncated: %v", web, delta, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTicket63OptionalPassageKeepsValidationOfAttachmentsAndQuestion(t *testing.T) {
|
||||
for _, q := range []Question{
|
||||
{Question: ""}, {Question: " "},
|
||||
{Question: "q", Passage: "\x00"}, {Question: "q", Passage: string([]byte{0xff})},
|
||||
{Question: "q", Passage: strings.Repeat("x", MaxPassageRunes+1)},
|
||||
} {
|
||||
q.RequestID = "00000000-0000-0000-0000-000000006301"
|
||||
r := &fixtureRepo{}
|
||||
_, err := New(r, &fixtureResolver{}).Ask(context.Background(), "owner", "summary", q)
|
||||
if !errors.Is(err, ErrInvalid) || r.begins != 0 {
|
||||
t.Fatal("invalid question/attachment reached repository", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,7 @@ package summaryqa
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -18,13 +18,13 @@ func (p *web60ServiceProvider) Answer(_ context.Context, r StatelessRequest) (An
|
||||
return p.result, nil
|
||||
}
|
||||
|
||||
func TestTicket60ServiceProjectsMinimalWebContextAndNormalizesCitations(t *testing.T) {
|
||||
func TestTicket63ServicePreservesFullWebContextAndNormalizesCitations(t *testing.T) {
|
||||
p := &web60ServiceProvider{result: AnswerResult{Answer: "answer", WebSearchPerformed: true, Citations: []Citation{{Title: " source ", URL: "https://EXAMPLE.COM:443/"}, {Title: "duplicate", URL: "https://example.com/"}}}}
|
||||
r := &fixtureRepo{work: Work{Summary: strings.Repeat("PRIVATE_SUMMARY", MaxContextBytes), History: []Exchange{{Answer: strings.Repeat("PRIVATE_HISTORY", MaxContextBytes)}}, Language: "fr", Turn: Turn{ID: "turn"}}}
|
||||
r := &fixtureRepo{work: Work{Summary: "FULL_SYNTHESIS", History: []Exchange{{Answer: "PREVIOUS_ANSWER"}}, Language: "fr", Turn: Turn{ID: "turn"}}}
|
||||
s := New(r, &fixtureResolver{p: p})
|
||||
_, err := s.Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000006001", Passage: "p", Question: "q", UseWebSearch: true})
|
||||
if err != nil || len(p.requests) != 1 || p.requests[0].Summary != "" || len(p.requests[0].History) != 0 || !p.requests[0].UseWebSearch || r.finishes != 1 || len(r.result.Citations) != 1 {
|
||||
t.Fatalf("non-minimal request or invalid result: %v %+v", err, p.requests)
|
||||
if err != nil || len(p.requests) != 1 || p.requests[0].Summary != r.work.Summary || !reflect.DeepEqual(p.requests[0].History, r.work.History) || !p.requests[0].UseWebSearch || r.finishes != 1 || len(r.result.Citations) != 1 {
|
||||
t.Fatalf("incomplete context or invalid result: %v %+v", err, p.requests)
|
||||
}
|
||||
}
|
||||
func TestTicket60UnsupportedSearchNeverAcquiresQuotaAndReplaysSameFailure(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user