feat(generation): score articles with selected relevance model (#48)
This commit is contained in:
@ -14,26 +14,37 @@ import (
|
||||
"github.com/tradarr/backend/internal/companycatalog"
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/rankingcapability"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/summarybudget"
|
||||
)
|
||||
|
||||
type Fixtures struct {
|
||||
SummaryModelID string `json:"summaryModelID"`
|
||||
CompanyQuery string `json:"companyQuery"`
|
||||
CompanyID string `json:"companyID"`
|
||||
CompanyName string `json:"companyName"`
|
||||
CompanySymbol string `json:"companySymbol"`
|
||||
CompanyFIGI string `json:"companyFIGI"`
|
||||
SummarySentinel string `json:"summarySentinel"`
|
||||
ArticleTitle string `json:"articleTitle"`
|
||||
ArticleURL string `json:"articleURL"`
|
||||
ArticleContent string `json:"articleContent"`
|
||||
CandidateID string `json:"candidateID"`
|
||||
RankingModelID string `json:"rankingModelID"`
|
||||
RankingModelName string `json:"rankingModelName"`
|
||||
RankingCapability rankingcapability.Capability `json:"rankingCapability"`
|
||||
SummaryModelID string `json:"summaryModelID"`
|
||||
CompanyQuery string `json:"companyQuery"`
|
||||
CompanyID string `json:"companyID"`
|
||||
CompanyName string `json:"companyName"`
|
||||
CompanySymbol string `json:"companySymbol"`
|
||||
CompanyFIGI string `json:"companyFIGI"`
|
||||
SummarySentinel string `json:"summarySentinel"`
|
||||
ArticleTitle string `json:"articleTitle"`
|
||||
ArticleURL string `json:"articleURL"`
|
||||
ArticleContent string `json:"articleContent"`
|
||||
CandidateID string `json:"candidateID"`
|
||||
}
|
||||
|
||||
func DefaultFixtures() Fixtures {
|
||||
return Fixtures{"00000000-0000-0000-0000-000000001203", "E2E Company", "isin:US0000000001", "E2E Company Inc.", "E2ECO", "BBG000E2E001", "E2E synthesized financial summary", "E2E private article title", "https://fixture.invalid/private-article", "E2E private article body", "pending-candidate-id"}
|
||||
return Fixtures{
|
||||
SummaryModelID: "00000000-0000-0000-0000-000000001203",
|
||||
RankingModelID: "00000000-0000-0000-0000-000000001204",
|
||||
RankingModelName: "llama3",
|
||||
RankingCapability: rankingcapability.Lookup("ollama", "llama3", ""),
|
||||
CompanyQuery: "E2E Company", CompanyID: "isin:US0000000001", CompanyName: "E2E Company Inc.", CompanySymbol: "E2ECO", CompanyFIGI: "BBG000E2E001",
|
||||
SummarySentinel: "E2E synthesized financial summary", ArticleTitle: "E2E private article title", ArticleURL: "https://fixture.invalid/private-article", ArticleContent: "E2E private article body", CandidateID: "pending-candidate-id",
|
||||
}
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
@ -92,7 +103,7 @@ func (f *Fakes) Handler() http.Handler {
|
||||
mux.HandleFunc("/audit", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"openfigiSearches": f.counts["openfigiSearches"], "companySearches": f.counts["companySearches"], "companyResolutions": f.counts["companyResolutions"], "scraperCalls": f.counts["scraperCalls"], "layaRankingCalls": f.counts["layaRankingCalls"], "layaSummaryCalls": f.counts["layaSummaryCalls"], "events": f.Events})
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"openfigiSearches": f.counts["openfigiSearches"], "companySearches": f.counts["companySearches"], "companyResolutions": f.counts["companyResolutions"], "scraperCalls": f.counts["scraperCalls"], "relevanceScoringCalls": f.counts["relevanceScoringCalls"], "layaRankingCalls": f.counts["layaRankingCalls"], "layaSummaryCalls": f.counts["layaSummaryCalls"], "events": f.Events})
|
||||
})
|
||||
mux.HandleFunc("/v3/search", func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
@ -117,16 +128,42 @@ func (f *Fakes) Handler() http.Handler {
|
||||
})
|
||||
mux.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Prompt string `json:"prompt"`
|
||||
Format string `json:"format"`
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
Format json.RawMessage `json:"format"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
if r.Method != "POST" || json.NewDecoder(r.Body).Decode(&req) != nil {
|
||||
http.Error(w, "invalid fixture provider", 400)
|
||||
return
|
||||
}
|
||||
response := f.Fixture.SummarySentinel
|
||||
if req.Format == "json" {
|
||||
if len(req.Format) > 0 && req.Format[0] == '{' {
|
||||
if req.Model != f.Fixture.RankingModelName {
|
||||
http.Error(w, "invalid scoring model", 400)
|
||||
return
|
||||
}
|
||||
start := strings.LastIndex(req.Prompt, "\n{")
|
||||
if start < 0 {
|
||||
http.Error(w, "scoring payload missing", 400)
|
||||
return
|
||||
}
|
||||
var payload generation.TitleScoreRequest
|
||||
if json.Unmarshal([]byte(req.Prompt[start+1:]), &payload) != nil || len(payload.Titles) == 0 {
|
||||
http.Error(w, "scoring titles missing", 400)
|
||||
return
|
||||
}
|
||||
rows := []map[string]any{}
|
||||
for i, title := range payload.Titles {
|
||||
if title.Index != i {
|
||||
http.Error(w, "invalid scoring index", 400)
|
||||
return
|
||||
}
|
||||
rows = append(rows, map[string]any{"index": title.Index, "score": 80})
|
||||
}
|
||||
encoded, _ := json.Marshal(map[string]any{"scores": rows})
|
||||
response = string(encoded)
|
||||
f.record(Event{Kind: "relevanceScoringCalls", Model: req.Model})
|
||||
} else if string(req.Format) == `"json"` {
|
||||
// The candidate UUID is generated by the real use case; never hard-code it.
|
||||
start := strings.LastIndex(req.Prompt, "\n{")
|
||||
if start < 0 {
|
||||
@ -152,7 +189,7 @@ func (f *Fakes) Handler() http.Handler {
|
||||
} else {
|
||||
f.record(Event{Kind: "layaSummaryCalls", Model: req.Model})
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"response": response})
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"response": response, "done": true, "done_reason": "stop"})
|
||||
})
|
||||
return mux
|
||||
}
|
||||
@ -214,6 +251,9 @@ func (p FakeProvider) Summarize(ctx context.Context, prompt string, opts ai.GenO
|
||||
if opts.JSONMode {
|
||||
body["format"] = "json"
|
||||
}
|
||||
if len(opts.JSONSchema) > 0 {
|
||||
body["format"] = opts.JSONSchema
|
||||
}
|
||||
raw, _ := json.Marshal(body)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", p.URL+"/api/generate", bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
@ -228,9 +268,14 @@ func (p FakeProvider) Summarize(ctx context.Context, prompt string, opts ai.GenO
|
||||
return "", fmt.Errorf("fake provider failed")
|
||||
}
|
||||
var result struct {
|
||||
Response string `json:"response"`
|
||||
Done bool `json:"done"`
|
||||
DoneReason string `json:"done_reason"`
|
||||
Response string `json:"response"`
|
||||
}
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
if err == nil && opts.RequireComplete && (!result.Done || result.DoneReason != "stop") {
|
||||
return "", fmt.Errorf("fake provider incomplete")
|
||||
}
|
||||
return result.Response, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user