feat(bloomberg): enrich article content for summaries (#36)
This commit is contained in:
@ -155,18 +155,23 @@ func (r *LayaHTTPRanker) rank(ctx context.Context, mode string, assets []models.
|
||||
tasks := []task{}
|
||||
for article, candidate := range candidates {
|
||||
state := layaArticleState(indices, candidate, mode)
|
||||
if len(indices) == 0 || layaStateFits(state) {
|
||||
if layaStateFits(state) {
|
||||
tasks = append(tasks, task{state, article})
|
||||
continue
|
||||
}
|
||||
if len(indices) == 0 {
|
||||
return nil, errors.New("laya: immutable state exceeds byte budget")
|
||||
}
|
||||
for start := 0; start < len(indices); {
|
||||
end := start + 1
|
||||
for end < len(indices) && end-start < 10 && layaStateFits(layaArticleState(indices[start:end+1], candidate, mode)) {
|
||||
end++
|
||||
}
|
||||
// Even an oversized single-asset article stays whole. The server
|
||||
// must validate it; no windows, shortened content or adaptive retry.
|
||||
tasks = append(tasks, task{layaArticleState(indices[start:end], candidate, mode), article})
|
||||
state := layaArticleState(indices[start:end], candidate, mode)
|
||||
if !layaStateFits(state) {
|
||||
return nil, errors.New("laya: immutable state exceeds byte budget")
|
||||
}
|
||||
tasks = append(tasks, task{state, article})
|
||||
start = end
|
||||
}
|
||||
}
|
||||
@ -327,7 +332,26 @@ func layaArticleState(indices []map[string]string, c generation.Candidate, mode
|
||||
state["content_fallback"] = "title_only"
|
||||
} else {
|
||||
state["content"] = c.Article.Content
|
||||
if c.Article.ContentTruncated {
|
||||
state["content_truncated"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if content, ok := state["content"].(string); ok && !layaStateFits(state) {
|
||||
state["content_truncated"] = true
|
||||
state["content_available"] = true
|
||||
// Budget the serialized JSON, including escaping and the excerpt flag.
|
||||
low, high := 0, len(content)
|
||||
for low < high {
|
||||
mid := (low + high + 1) / 2
|
||||
state["content"] = utf8Prefix(content, mid)
|
||||
if layaStateFits(state) {
|
||||
low = mid
|
||||
} else {
|
||||
high = mid - 1
|
||||
}
|
||||
}
|
||||
state["content"] = utf8Prefix(content, low)
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user