Repository navigation
fix(core): validate topN in cohere and voyage rerankers (#582) - #589
Conversation
|
@anurag629 ci is green now , could u plz review this pr |
anurag629
left a comment
There was a problem hiding this comment.
Thanks @bhushan4work, nearly there. The changeset is in and the NaN case is fixed, but one case gets worse than today.
Infinity now returns []. Number.isFinite(topN) at packages/core/src/retrieval/cohere-reranker.ts:26 and voyage-reranker.ts:26 turns it into 0. plan() at packages/core/src/retrieval/retrievers.ts:83 keeps topK: Infinity as it is (Math.max(1, Infinity)), and finish() passes it to rerank() at line 99, so a query that asks for every chunk gets no sources at all. On dev it sends top_n: 3 for three chunks. Use the check from my comment on #582 in both files:
const limit = Math.floor(topN);
if (chunks.length === 0 || !(limit > 0)) return [];That still returns [] for NaN and -Infinity. In the new tests at packages/core/tests/retrieval/cohere-reranker.test.ts:48-55 and voyage-reranker.test.ts:48-55, keep the NaN and -Infinity lines and replace the Infinity one with a case that checks the request body has top_n: 3 (top_k: 3 for Voyage), like the test at line 33. Then change "non-finite or invalid inputs" in the changeset to NaN, since Infinity should keep working.
typecheck-test-build, e2e and changeset pass. I'll review it again when you push.
anurag629
left a comment
There was a problem hiding this comment.
Thanks @bhushan4work. Infinity asks for every chunk again (top_n: 3 for three, covered by the new tests at packages/core/tests/retrieval/cohere-reranker.test.ts:56 and voyage-reranker.test.ts:56), and NaN and -Infinity still return [] without sending a request. All checks pass, including e2e.
|
Merged, thanks @bhushan4work. |
What does this change?
this pr adds validation for
topNin the cohere and voyage rerankers. invalid or non finite values such asNaN,Infinityand-Infinitynow return an empty result without sending an outbound request.closes #582
How I tested this
pnpm vitest run packages/core/tests/retrieval/cohere-reranker.test.ts packages/core/tests/retrieval/voyage-reranker.test.tspnpm vitest run packages/core/tests/retrieval/workers-ai-reranker.test.tspnpm typechecktopNvalues resolve to[]and thatfetchis not calledAI assistance
none
Checklist
devpnpm -r build,pnpm testandpnpm -r typecheckpass locallypnpm changeset) if this changescore,widgetorserverpnpm apirun andapi/committed if an exported type or option changed