Ask Four Ways, Fuse the Rankings
Have a model rewrite the question several ways, search each, and fuse the rankings. Implement parse_queries(reply) and fusion_search(question, generate, search, n=4, top=5, depth=10). generate(prompt) is the model and returns a string; search(query, depth) returns up to depth hits, best first, each {"id", "snippet"}.
- One model call with
QUERIES_PROMPTfilled withnand the question (asn=andquestion=). parse_queriesreads the reply line by line. Strip the line, remove a list marker at its start (-,*,•, or digits then.or), followed by whitespace), strip again, remove"and'from both ends, and turn each run of whitespace into one space. Skip lines left empty and lines ending in:(a header likeHere are 4 queries:). Return the rest in order.- The queries are the question itself first, then the parsed rewrites, skipping any that repeat a query already listed (compared lowercased with whitespace runs collapsed), keeping at most
nrewrites. A reply with nothing usable leaves the question alone. - Search each query with
search(query, depth), in order. - Fuse with reciprocal rank fusion: a document's score is the sum, over the lists it appears in, of
1 / (RRF_K + rank), rank counted from 1 (RRF_Kis 60). Best first; equal scores by id. - Return
{"queries": the queries, "results": [(id, score), ...]}for the besttop.
The catch. The same document comes back from different queries with a different snippet, because a snippet marks the words that matched. Fusion counts documents, so key the scores by id. Keyed by anything else, a document every query found splits into weak copies and loses to one that a single query found.