UNPKG

@jackhua/mini-langchain

Version:

A lightweight TypeScript implementation of LangChain with cost optimization features

112 lines 2.76 kB
import { BaseLLM } from '../llms/base'; /** * Configuration for LLM routing */ export interface RouterConfig { llms: { [key: string]: { llm: BaseLLM; capabilities: string[]; costPerToken: number; speedScore: number; qualityScore: number; }; }; defaultLLM?: string; costThreshold?: number; enableCostOptimization?: boolean; enableLoadBalancing?: boolean; } /** * Task types for routing */ export declare enum TaskType { CODE_GENERATION = "code_generation", CODE_REVIEW = "code_review", CREATIVE_WRITING = "creative_writing", TRANSLATION = "translation", SUMMARIZATION = "summarization", QA = "question_answering", CONVERSATION = "conversation", ANALYSIS = "analysis", MATH = "math", REASONING = "reasoning" } /** * Context for routing decisions */ export interface RoutingContext { taskType?: TaskType; complexity?: 'low' | 'medium' | 'high'; maxCost?: number; requireSpeed?: boolean; requireQuality?: boolean; userTier?: 'free' | 'basic' | 'premium'; previousErrors?: string[]; } /** * Auto-Adaptive LLM Router * Automatically selects the best LLM based on task analysis */ export declare class LLMRouter { private config; private usageStats; constructor(config: RouterConfig); /** * Initialize usage statistics */ private initializeStats; /** * Route to the best LLM based on prompt and context */ route(prompt: string, context?: RoutingContext): Promise<BaseLLM>; /** * Detect task type from prompt */ private detectTaskType; /** * Check if text contains keywords */ private containsKeywords; /** * Assess complexity of the prompt */ private assessComplexity; /** * Filter LLMs suitable for the task */ private filterSuitableLLMs; /** * Check if LLM has capability for task */ private checkCapability; /** * Check if LLM quality matches complexity */ private checkComplexityMatch; /** * Score LLMs based on multiple factors */ private scoreLLMs; /** * Select the best LLM from scored options */ private selectBestLLM; /** * Update usage statistics after a call */ updateStats(llmName: string, success: boolean, cost: number, responseTime: number): void; /** * Get routing statistics */ getStats(): Record<string, any>; /** * Create a routed LLM that automatically selects the best provider */ createRoutedLLM(): BaseLLM; /** * Estimate cost for a response */ private estimateCost; } //# sourceMappingURL=router.d.ts.map