fix(embeddings): truncate query content for temp doc query (#12573)

* fix(embeddings): truncate query  content for temp doc query

* fix: use tokenService
This commit is contained in:
SuYao 2026-01-23 20:34:54 +08:00 committed by GitHub
parent 46fa59a10c
commit 022e1aee4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,6 +30,7 @@ import { isEmpty } from 'lodash'
import { getProviderByModel } from './AssistantService'
import FileManager from './FileManager'
import type { BlockManager } from './messageStreaming'
import { estimateTextTokens } from './TokenService'
const logger = loggerService.withContext('RendererKnowledgeService')
@ -146,6 +147,16 @@ export const searchKnowledgeBase = async (
parentSpanId?: string,
modelName?: string
): Promise<Array<KnowledgeSearchResult & { file: FileMetadata | null }>> => {
// Truncate query based on embedding model's max_context to prevent embedding errors
const maxContext = getEmbeddingMaxContext(base.model.id)
if (maxContext) {
const estimatedTokens = estimateTextTokens(query)
if (estimatedTokens > maxContext) {
const ratio = maxContext / estimatedTokens
query = query.slice(0, Math.floor(query.length * ratio))
}
}
let currentSpan: Span | undefined = undefined
try {
const baseParams = getKnowledgeBaseParams(base)