Skip to content

Commit 5d1446f

Browse files
committed
fix(knowledge): bill rerank on every successful API call and fix MDX docs literal
1 parent c56b7a4 commit 5d1446f

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

apps/docs/content/docs/en/tools/knowledge.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Search for similar content in a knowledge base using vector similarity
4848
| `tagName` | string | No | No description |
4949
| `tagValue` | string | No | No description |
5050
| `rerankerEnabled` | boolean | No | Whether to apply Cohere reranking to vector search results |
51-
| `rerankerModel` | string | No | Cohere rerank model to use \(one of: $\{SUPPORTED_RERANKER_MODELS.join\(', '\)\}\) |
51+
| `rerankerModel` | string | No | Cohere rerank model to use \(one of: rerank-v4.0-pro, rerank-v4.0-fast, rerank-v3.5\) |
5252
| `tagFilters` | string | No | No description |
5353

5454
#### Output

apps/sim/app/api/knowledge/search/route.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
365365

366366
// Optional Cohere rerank pass on top of vector results.
367367
const rerankedScores = new Map<string, number>()
368+
// `rerankBilled` = Cohere was successfully called (even with 0 results) and we owe the search unit.
369+
// `rerankApplied` = result ordering was actually replaced by the reranker output.
370+
let rerankBilled = false
368371
let rerankApplied = false
369372
if (useReranker && rerankerModel && results.length > 0) {
370373
const candidateCount = results.length
@@ -374,6 +377,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
374377
results.map((r) => ({ id: r.id, text: r.content })),
375378
{ model: rerankerModel, topN: validatedData.topK, workspaceId }
376379
)
380+
rerankBilled = true
377381
if (ranked.length === 0) {
378382
logger.warn(
379383
`[${requestId}] Reranker returned 0 results; falling back to vector ordering`,
@@ -419,9 +423,10 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
419423
}
420424
}
421425

422-
// Add Cohere rerank cost (1 search unit per call, since we cap candidates ≤100).
426+
// Add Cohere rerank cost (1 search unit per successful call, since we cap candidates ≤100).
427+
// Bill on every successful API response — Cohere charges even when 0 results are returned.
423428
let rerankerCost = 0
424-
if (rerankApplied && rerankerModel) {
429+
if (rerankBilled && rerankerModel) {
425430
const pricing = getRerankModelPricing(rerankerModel)
426431
if (pricing) {
427432
rerankerCost = pricing.perSearchUnit
@@ -535,7 +540,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
535540
},
536541
model: queryEmbeddingModel,
537542
pricing: cost.pricing,
538-
...(rerankApplied ? { rerankerCost, rerankerModel, rerankerSearchUnits: 1 } : {}),
543+
...(rerankBilled ? { rerankerCost, rerankerModel, rerankerSearchUnits: 1 } : {}),
539544
},
540545
}
541546
: {}),

0 commit comments

Comments
 (0)