Exched/DeepSeek_Coder
0
1/**2 * Calculate optimal max_tokens based on provider capabilities and input size3 * 4 * @param selectedProvider - The selected provider object from getBestProvider5 * @param inputTokens - Estimated input tokens (prompt + system message + context)6 * @param isStreaming - Whether this is a streaming request (affects buffer)7 * @returns Optimal max_tokens value8 */9export function calculateMaxTokens(10 selectedProvider: any,11 inputTokens: number = 0,12 isStreaming: boolean = false13): number {14 if (!selectedProvider?.context_length) {15 // Fallback for unknown providers - use conservative default16 return 4096;17 }18 19 const contextLength = selectedProvider.context_length;20 21 // Reserve buffer for safety and potential tokenization differences22 const safetyBuffer = isStreaming ? 1000 : 500;23 24 // Calculate available tokens for output25 const availableTokens = contextLength - inputTokens - safetyBuffer;26 27 // Define reasonable max output limits based on use case28 const useCase = {29 // For HTML generation, we typically need substantial output30 htmlGeneration: Math.min(32_000, availableTokens),31 // For code editing, moderate output is usually sufficient 32 codeEditing: Math.min(16_000, availableTokens),33 // Conservative fallback34 default: Math.min(8_000, availableTokens)35 };36 37 // Choose based on available tokens and provider capabilities38 let targetTokens: number;39 40 if (availableTokens >= 32_000) {41 targetTokens = useCase.htmlGeneration;42 } else if (availableTokens >= 16_000) {43 targetTokens = useCase.codeEditing;44 } else {45 targetTokens = useCase.default;46 }47 48 // Ensure we don't go below minimum viable output49 const minimumViableOutput = 2048;50 if (targetTokens < minimumViableOutput) {51 // If we can't provide minimum viable output, try with minimal buffer52 const minimalBuffer = 200;53 targetTokens = Math.max(54 minimumViableOutput,55 contextLength - inputTokens - minimalBuffer56 );57 }58 59 // Final safety check - never exceed context length60 return Math.min(targetTokens, contextLength - inputTokens - 100);61}62 63/**64 * Estimate input tokens for a request (rough estimation)65 * 66 * @param systemPrompt - System prompt content67 * @param userPrompt - User prompt content 68 * @param additionalContext - Additional context (templates, pages, etc.)69 * @returns Estimated token count70 */71export function estimateInputTokens(72 systemPrompt: string = "",73 userPrompt: string = "",74 additionalContext: string = ""75): number {76 // Rough estimation: ~4 characters per token for English text77 // This is conservative - actual tokenization may vary78 const totalChars = systemPrompt.length + userPrompt.length + additionalContext.length;79 return Math.ceil(totalChars / 3.5); // Slightly more conservative than 4 chars/token80}81 82/**83 * Get max_tokens configuration for specific providers with special handling84 */85export function getProviderSpecificConfig(selectedProvider: any, baseMaxTokens: number) {86 const providerName = selectedProvider?.provider;87 88 switch (providerName) {89 case "sambanova":90 // SambaNova has specific limitations - don't set max_tokens91 return {};92 default:93 return { max_tokens: baseMaxTokens };94 }95}96 