Skip to content

Commit ef84871

Browse files
committed
fix(knowledge): use KB embedding model for search and fix single-result normalization
1 parent 322dc4e commit ef84871

File tree

1 file changed

+15
-5
lines changed
  • apps/sim/app/api/knowledge/search

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,19 @@ export async function POST(request: NextRequest) {
244244
const hasQuery = validatedData.query && validatedData.query.trim().length > 0
245245
const hasFilters = structuredFilters && structuredFilters.length > 0
246246

247-
// Generate OpenAI search embedding
247+
// Generate OpenAI search embedding using the KB's configured model
248248
let openaiQueryVector: string | null = null
249+
let openaiEmbeddingModel = 'text-embedding-3-small'
249250
if (hasQuery && openaiKbIds.length > 0) {
250-
const emb = await generateSearchEmbedding(validatedData.query!, undefined, workspaceId)
251+
const firstOpenaiConfig = kbConfigMap.get(openaiKbIds[0])
252+
if (firstOpenaiConfig) {
253+
openaiEmbeddingModel = firstOpenaiConfig.embeddingModel
254+
}
255+
const emb = await generateSearchEmbedding(
256+
validatedData.query!,
257+
openaiEmbeddingModel,
258+
workspaceId
259+
)
251260
openaiQueryVector = JSON.stringify(emb)
252261
}
253262

@@ -349,7 +358,8 @@ export async function POST(request: NextRequest) {
349358
// embedding spaces are not directly comparable — normalize each provider's
350359
// scores to 0-1 range before merging.
351360
const normalizeScores = (items: SearchResult[]): SearchResult[] => {
352-
if (items.length <= 1) return items
361+
if (items.length === 0) return items
362+
if (items.length === 1) return [{ ...items[0], distance: 0 }]
353363
const min = Math.min(...items.map((r) => r.distance))
354364
const max = Math.max(...items.map((r) => r.distance))
355365
const range = max - min || 1
@@ -378,7 +388,7 @@ export async function POST(request: NextRequest) {
378388
if (hasQuery && openaiKbIds.length > 0) {
379389
try {
380390
tokenCount = estimateTokenCount(validatedData.query!, 'openai')
381-
cost = calculateCost('text-embedding-3-small', tokenCount.count, 0, false)
391+
cost = calculateCost(openaiEmbeddingModel, tokenCount.count, 0, false)
382392
} catch (error) {
383393
logger.warn(`[${requestId}] Failed to calculate cost for search query`, {
384394
error: error instanceof Error ? error.message : 'Unknown error',
@@ -473,7 +483,7 @@ export async function POST(request: NextRequest) {
473483
completion: 0,
474484
total: tokenCount.count,
475485
},
476-
model: 'text-embedding-3-small',
486+
model: openaiEmbeddingModel,
477487
pricing: cost.pricing,
478488
},
479489
}

0 commit comments

Comments
 (0)