feat(qa): add synthesis conversations with source retention (#52)
This commit is contained in:
112
backend/internal/summaryqa/service.go
Normal file
112
backend/internal/summaryqa/service.go
Normal file
@ -0,0 +1,112 @@
|
||||
package summaryqa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Repo Repository
|
||||
Resolver Resolver
|
||||
Clock func() time.Time
|
||||
}
|
||||
|
||||
func New(repo Repository, resolver Resolver) *Service {
|
||||
return &Service{Repo: repo, Resolver: resolver, Clock: time.Now}
|
||||
}
|
||||
func (s *Service) Get(ctx context.Context, owner, summary string) (Conversation, error) {
|
||||
return s.Repo.GetQAConversation(ctx, owner, summary, s.Clock())
|
||||
}
|
||||
func (s *Service) Delete(ctx context.Context, owner, summary string) error {
|
||||
return s.Repo.DeleteQAConversation(ctx, owner, summary)
|
||||
}
|
||||
|
||||
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 {
|
||||
return Turn{}, ErrInvalid
|
||||
}
|
||||
q.RequestID = strings.ToLower(q.RequestID)
|
||||
work, err := s.Repo.BeginQAQuestion(ctx, owner, summary, q, s.Clock())
|
||||
if err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
if work.Replay {
|
||||
work.Turn.Replayed = true
|
||||
if work.Turn.Status == "failed" {
|
||||
switch work.Turn.FailureCode {
|
||||
case "qa_model_unavailable":
|
||||
return work.Turn, ErrModel
|
||||
case "qa_context_limit":
|
||||
return work.Turn, ErrLimit
|
||||
default:
|
||||
return work.Turn, ErrProvider
|
||||
}
|
||||
}
|
||||
return work.Turn, nil
|
||||
}
|
||||
finish := func(answer, code string) (Turn, error) {
|
||||
// Persist a bounded terminal result even when the HTTP request is canceled.
|
||||
saveCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
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}
|
||||
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 _, err = finish("", ErrLimit.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrLimit
|
||||
}
|
||||
var provider Provider
|
||||
if s.Resolver != nil {
|
||||
provider, err = s.Resolver.ResolveQA(ctx, owner, summary)
|
||||
} else {
|
||||
err = ErrModel
|
||||
}
|
||||
if err != nil || provider == nil {
|
||||
if _, err = finish("", ErrModel.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrModel
|
||||
}
|
||||
callCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||||
defer cancel()
|
||||
permit, admissionErr := s.Repo.AcquireQAProviderCall(callCtx, owner, summary, work.Turn.ID)
|
||||
if admissionErr != nil {
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
cleanupErr := s.Repo.CancelQAQuestion(cleanupCtx, owner, summary, work.Turn.ID)
|
||||
cleanupCancel()
|
||||
if cleanupErr != nil {
|
||||
return Turn{}, cleanupErr
|
||||
}
|
||||
return Turn{}, admissionErr
|
||||
}
|
||||
// Release immediately after Answer returns, before persistence: the permit
|
||||
// represents an actual provider call, not the pending turn or an HTTP timeout.
|
||||
answer, providerErr := func() (answer string, err error) {
|
||||
defer permit.Release()
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
answer = ""
|
||||
err = ErrProvider
|
||||
}
|
||||
}()
|
||||
return provider.Answer(callCtx, request)
|
||||
}()
|
||||
if providerErr != nil || callCtx.Err() != nil || strings.TrimSpace(answer) == "" || !utf8.ValidString(answer) || utf8.RuneCountInString(answer) > MaxAnswerRunes {
|
||||
if _, err = finish("", ErrProvider.Error()); err != nil {
|
||||
return Turn{}, err
|
||||
}
|
||||
return Turn{}, ErrProvider
|
||||
}
|
||||
return finish(answer, "")
|
||||
}
|
||||
Reference in New Issue
Block a user