Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(knowledge): expose Cohere reranker controls#4429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ef97b7b3fcd053232b1ca5803229File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { PackageSearchIcon } from '@/components/icons' | ||
| import { DEFAULT_RERANKER_MODEL, SUPPORTED_RERANKER_MODELS } from '@/lib/knowledge/reranker-models' | ||
| import type { BlockConfig } from '@/blocks/types' | ||
| import { getCohereRerankerApiKeyCondition } from '@/blocks/utils' | ||
| export const KnowledgeBlock: BlockConfig = { | ||
| type: 'knowledge', | ||
| @@ -105,6 +106,28 @@ export const KnowledgeBlock: BlockConfig = { | ||
| and: { field: 'rerankerEnabled', value: true }, | ||
| }, | ||
| }, | ||
| { | ||
| id: 'rerankerInputCount', | ||
| title: 'Documents Sent to Reranker', | ||
| type: 'short-input', | ||
| placeholder: 'Auto (4× results, capped at 100)', | ||
| mode: 'advanced', | ||
| condition: { | ||
| field: 'operation', | ||
| value: 'search', | ||
| and: { field: 'rerankerEnabled', value: true }, | ||
| }, | ||
| }, | ||
| { | ||
| id: 'apiKey', | ||
| title: 'Cohere API Key', | ||
| type: 'short-input', | ||
| placeholder: 'Enter your Cohere API key', | ||
| password: true, | ||
| connectionDroppable: false, | ||
| required: true, | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| condition: getCohereRerankerApiKeyCondition(), | ||
| }, | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // --- List Documents --- | ||
| { | ||
| @@ -419,6 +442,11 @@ export const KnowledgeBlock: BlockConfig = { | ||
| tagFilters: { type: 'string', description: 'Tag filter criteria' }, | ||
| rerankerEnabled: { type: 'boolean', description: 'Apply Cohere reranking to search results' }, | ||
| rerankerModel: { type: 'string', description: 'Cohere rerank model identifier' }, | ||
| rerankerInputCount: { | ||
| type: 'number', | ||
| description: 'Number of vector results sent to the Cohere reranker (1–100)', | ||
| }, | ||
| apiKey: { type: 'string', description: 'Cohere API key (self-hosted only)' }, | ||
| documentTags: { type: 'string', description: 'Document tags' }, | ||
| chunkSearch: { type: 'string', description: 'Search filter for chunks' }, | ||
| chunkEnabledFilter: { type: 'string', description: 'Filter chunks by enabled status' }, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -36,6 +36,24 @@ export const knowledgeSearchBodySchema = z | ||
| .transform((val) => val || undefined), | ||
| rerankerEnabled: z.boolean().optional().default(false), | ||
| rerankerModel: rerankerModelSchema.optional().default(DEFAULT_RERANKER_MODEL), | ||
| /** | ||
| * Number of vector results sent to Cohere as the documents array for reranking. Capped at 100 | ||
| * so each rerank call stays within a single Cohere search unit (1 query × ≤100 docs); see | ||
| * `RERANK_MODEL_PRICING` in `providers/models.ts`. | ||
| */ | ||
| rerankerInputCount: z | ||
| .number() | ||
| .int('rerankerInputCount must be an integer') | ||
| .min(1, 'rerankerInputCount must be at least 1') | ||
| .max(100, 'rerankerInputCount cannot exceed 100') | ||
| .optional() | ||
| .nullable() | ||
| .transform((val) => val ?? undefined), | ||
| rerankerApiKey: z | ||
| .string() | ||
| .optional() | ||
| .nullable() | ||
| .transform((val) => val || undefined), | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }) | ||
| .refine( | ||
| (data) => { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -55,6 +55,19 @@ export const knowledgeSearchTool: ToolConfig<any, KnowledgeSearchResponse> = { | ||
| description: | ||
| 'Cohere rerank model to use (one of: rerank-v4.0-pro, rerank-v4.0-fast, rerank-v3.5)', | ||
| }, | ||
| rerankerInputCount: { | ||
| type: 'number', | ||
| required: false, | ||
| visibility: 'user-only', | ||
| description: | ||
| 'Number of vector results sent to the Cohere reranker (1–100). Defaults to topK × 4 capped at 100.', | ||
| }, | ||
| apiKey: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-only', | ||
| description: 'Cohere API key for reranker (self-hosted deployments only)', | ||
| }, | ||
| }, | ||
| schemaEnrichment: { | ||
| @@ -84,13 +97,29 @@ export const knowledgeSearchTool: ToolConfig<any, KnowledgeSearchResponse> = { | ||
| typeof params.rerankerModel === 'string' && params.rerankerModel.length > 0 | ||
| ? params.rerankerModel | ||
| : DEFAULT_RERANKER_MODEL | ||
| const rerankerApiKey = | ||
| typeof params.apiKey === 'string' && params.apiKey.length > 0 ? params.apiKey : undefined | ||
| const rawInputCount = | ||
| params.rerankerInputCount !== undefined && | ||
| params.rerankerInputCount !== null && | ||
| params.rerankerInputCount !== '' | ||
| ? Number(params.rerankerInputCount) | ||
| : Number.NaN | ||
| const rerankerInputCount = Number.isFinite(rawInputCount) | ||
| ? Math.max(1, Math.min(100, Math.floor(rawInputCount))) | ||
| : undefined | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const requestBody = { | ||
| knowledgeBaseIds, | ||
| query: params.query, | ||
| topK: params.topK ? Math.max(1, Math.min(100, Number(params.topK))) : 10, | ||
| ...(structuredFilters.length > 0 && { tagFilters: structuredFilters }), | ||
| ...(rerankerEnabled && { rerankerEnabled: true, rerankerModel }), | ||
| ...(rerankerEnabled && { | ||
| rerankerEnabled: true, | ||
| rerankerModel, | ||
| ...(rerankerInputCount !== undefined && { rerankerInputCount }), | ||
| ...(rerankerApiKey && { rerankerApiKey }), | ||
| }), | ||
| ...(workflowId && { workflowId }), | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.