feat(qa): add synthesis conversations with source retention (#52)
This commit is contained in:
@ -15,14 +15,15 @@ import (
|
||||
)
|
||||
|
||||
type UserInput struct {
|
||||
RankingModelID *string
|
||||
SummaryContext string
|
||||
SummaryPromptTokenBudget int
|
||||
SummaryModelID *string
|
||||
Language, RankingMode string
|
||||
GlobalLimit int
|
||||
Assets []models.UserAsset
|
||||
Sources []models.Source
|
||||
SourceArticleRetentionDays int
|
||||
RankingModelID *string
|
||||
SummaryContext string
|
||||
SummaryPromptTokenBudget int
|
||||
SummaryModelID *string
|
||||
Language, RankingMode string
|
||||
GlobalLimit int
|
||||
Assets []models.UserAsset
|
||||
Sources []models.Source
|
||||
}
|
||||
type Candidate struct {
|
||||
RelevanceScore float64
|
||||
@ -70,8 +71,10 @@ type GenerationOutcome struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
type Result struct {
|
||||
Summary *models.Summary `json:"summary"`
|
||||
Outcome GenerationOutcome `json:"outcome"`
|
||||
SourceArticles []models.SummarySourceArticle `json:"-"`
|
||||
SourceArticleRetentionDays int `json:"-"`
|
||||
Summary *models.Summary `json:"summary"`
|
||||
Outcome GenerationOutcome `json:"outcome"`
|
||||
}
|
||||
type GenerationRecord = Result
|
||||
type Repository interface {
|
||||
@ -166,11 +169,14 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
out := GenerationOutcome{PromptBudget: summarybudget.Usage{RequestedTokens: requestedBudget}, ID: id, Language: input.Language, RankingMode: input.RankingMode, GlobalLimit: input.GlobalLimit, CreatedAt: u.Clock(), SourceStats: []SourceStat{}}
|
||||
resolved := ResolvedSummary{Summarizer: u.Summarizer}
|
||||
retainedSources := []models.SummarySourceArticle{}
|
||||
finish := func(status, code, content string) (Result, error) {
|
||||
out.Status = status
|
||||
out.FailureCode = code
|
||||
result := Result{Outcome: out}
|
||||
if content != "" {
|
||||
result.SourceArticles = retainedSources
|
||||
result.SourceArticleRetentionDays = input.SourceArticleRetentionDays
|
||||
result.Summary = &models.Summary{ID: NewID(), UserID: userID, Content: content, GeneratedAt: out.CreatedAt}
|
||||
if resolved.CatalogID != "" {
|
||||
result.Summary.SummaryModelID = &resolved.CatalogID
|
||||
@ -443,6 +449,9 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
if len(selected) == 0 {
|
||||
return finish("no_results", "", "")
|
||||
}
|
||||
for _, c := range selected {
|
||||
retainedSources = append(retainedSources, models.SummarySourceArticle{Title: c.Article.Title, Source: c.Article.Source, Content: c.Article.Content, URL: c.Article.URL, PublishedAt: c.Article.PublishedAt})
|
||||
}
|
||||
var content string
|
||||
if budgeted, ok := resolved.Summarizer.(BudgetedSummarizer); ok {
|
||||
budget := requestedBudget
|
||||
|
||||
@ -50,7 +50,7 @@ func TestLegacyNullFIGIAssetStillContributesToGenerationPostgres(t *testing.T) {
|
||||
})
|
||||
const userID = "00000000-0000-0000-0000-000000000401"
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE users (id UUID PRIMARY KEY, language TEXT NOT NULL DEFAULT 'fr', ranking_mode TEXT NOT NULL DEFAULT 'title',summary_prompt_token_budget INT NOT NULL DEFAULT 250000,ranking_model_id UUID NULL,summary_model_id UUID NULL)`,
|
||||
`CREATE TABLE users (id UUID PRIMARY KEY, language TEXT NOT NULL DEFAULT 'fr', ranking_mode TEXT NOT NULL DEFAULT 'title',summary_prompt_token_budget INT NOT NULL DEFAULT 250000,ranking_model_id UUID NULL,source_article_retention_days INT NOT NULL DEFAULT 7,summary_model_id UUID NULL)`,
|
||||
`CREATE TABLE user_assets (id UUID PRIMARY KEY, user_id UUID NOT NULL, symbol VARCHAR(20) NOT NULL, name TEXT NOT NULL DEFAULT '', figi TEXT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`,
|
||||
`CREATE TABLE sources (id UUID PRIMARY KEY, name TEXT NOT NULL, type TEXT NOT NULL, enabled BOOLEAN NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), article_limit INT NOT NULL DEFAULT 20, source_key TEXT)`,
|
||||
`CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)`,
|
||||
|
||||
@ -37,12 +37,12 @@ func (r RepositoryAdapter) LoadUserInput(ctx context.Context, id string) (UserIn
|
||||
if err != nil {
|
||||
return UserInput{}, err
|
||||
}
|
||||
return UserInput{RankingModelID: p.RankingModelID, SummaryContext: summaryContext, SummaryPromptTokenBudget: p.SummaryPromptTokenBudget, SummaryModelID: p.SummaryModelID, Language: p.Language, RankingMode: p.RankingMode, GlobalLimit: limit, Assets: assets, Sources: models.OperationalSources(sources)}, nil
|
||||
return UserInput{SourceArticleRetentionDays: p.SourceArticleRetentionDays, RankingModelID: p.RankingModelID, SummaryContext: summaryContext, SummaryPromptTokenBudget: p.SummaryPromptTokenBudget, SummaryModelID: p.SummaryModelID, Language: p.Language, RankingMode: p.RankingMode, GlobalLimit: limit, Assets: assets, Sources: models.OperationalSources(sources)}, nil
|
||||
}
|
||||
func (r RepositoryAdapter) SaveGeneration(ctx context.Context, id string, record GenerationRecord) error {
|
||||
payload, err := json.Marshal(record.Outcome)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.Repo.SaveGeneration(ctx, id, record.Outcome.ID, record.Summary, payload, record.Outcome.CreatedAt)
|
||||
return r.Repo.SaveGenerationWithQASources(ctx, id, record.Outcome.ID, record.Summary, payload, record.Outcome.CreatedAt, record.SourceArticles, record.SourceArticleRetentionDays)
|
||||
}
|
||||
|
||||
45
backend/internal/generation/retention_ticket53_test.go
Normal file
45
backend/internal/generation/retention_ticket53_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type qa53Summary struct{ fail bool }
|
||||
|
||||
func (p qa53Summary) Summarize(context.Context, string, []models.UserAsset, []Candidate) (string, error) {
|
||||
if p.fail {
|
||||
return "", errors.New("fixture failure")
|
||||
}
|
||||
return "safe summary", nil
|
||||
}
|
||||
func TestTicket53RetainsOnlyFinalSelectedArticlesOutsideAggregateJSON(t *testing.T) {
|
||||
for _, fail := range []bool{false, true} {
|
||||
key := models.SourceBloomberg
|
||||
feed := &ticket14Feed{}
|
||||
ranker := &ticket14LayaRanker{selectedTitle: "Market title B"}
|
||||
repo := &ticket14Repository{input: UserInput{SourceArticleRetentionDays: 3, Language: "en", RankingMode: "title_content", GlobalLimit: 1, Sources: []models.Source{{ID: "s", SourceKey: &key, Enabled: true, ArticleLimit: 10}}}}
|
||||
r, err := NewUseCase(Dependencies{Repository: repo, Ranker: ranker, Summarizer: qa53Summary{fail: fail}, Scrapers: map[string]scraper.Scraper{key: feed}}).Generate(context.Background(), "u")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fail {
|
||||
if len(repo.saved[0].SourceArticles) != 0 {
|
||||
t.Fatal("failed summary retained articles")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(repo.saved[0].SourceArticles) != 1 || repo.saved[0].SourceArticles[0].Title != "Market title B" || repo.saved[0].SourceArticleRetentionDays != 3 {
|
||||
t.Fatalf("unexpected retention snapshot %+v", repo.saved[0])
|
||||
}
|
||||
raw, _ := json.Marshal(r)
|
||||
if strings.Contains(string(raw), "Market title") || strings.Contains(string(raw), "Market body") {
|
||||
t.Fatal("sources leaked into generation DTO")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user