Skip to content
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

fix: updated logger and model caching #release #895

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

thecodacus
Copy link
Collaborator

Optimize Model List Fetching and Improve Provider Management

Overview

This PR optimizes the LLM provider management by implementing lazy loading and caching of dynamic models. Instead of fetching models from all providers on every LLM call, we now only fetch models from the selected provider. Additionally, we've added a caching mechanism for model lists and improved logging throughout the system.

Key Changes

1. Optimized Model List Fetching

  • Implemented lazy loading for dynamic models - only fetch from selected provider
  • Added caching mechanism for provider model lists
  • Moved model list management logic to LLMManager class

2. Universal Logging System

  • Enhanced logging with structured scopes using Chalk
  • Added cross-platform colored console output that works in terminals
  • Implemented consistent debug levels and scoped loggers
  • Unified logging format across different environments

Technical Details

Model Fetching Optimization

Key implementation changes (simplified):

// Before
const MODEL_LIST = await getModelList({ apiKeys, providerSettings, serverEnv });
const modelDetails = MODEL_LIST.find((m) => m.name === currentModel);

// After
const modelsList = [
  ...(provider.staticModels || []),
  ...(await LLMManager.getInstance().getModelListFromProvider(provider, {
    apiKeys,
    providerSettings,
    serverEnv
  }))
];

Caching Implementation

class BaseProvider {
  cachedDynamicModels?: {
    cacheId: string;
    models: ModelInfo[];
  };

  getModelsFromCache(options: ProviderOptions): ModelInfo[] | null {
    if (!this.cachedDynamicModels) return null;
    
    const cacheKey = this.getDynamicModelsCacheKey(options);
    if (cacheKey !== this.cachedDynamicModels.cacheId) {
      this.cachedDynamicModels = undefined;
      return null;
    }
    
    return this.cachedDynamicModels.models;
  }
}

Enhanced Cross-Platform Logging

// Universal logging with Chalk for consistent terminal output
const chalk = new Chalk({ level: 3 });

function formatText(text: string, color: string, bg: string) {
  return chalk.bgHex(bg)(chalk.hex(color)(text));
}

// Usage in logger
const labelText = formatText(` ${level.toUpperCase()} `, textColor, bgColor);
console.log(`${labelText}`, allMessages);

// Scoped logger usage example
const logger = createScopedLogger('stream-text');
logger.info(`Sending llm call to ${provider.name}`); // Outputs with consistent colors

Migration Impact

  • No breaking changes in the API
  • Cached models are automatically invalidated when provider settings change
  • Existing code paths continue to work with fallback mechanisms

Testing

  • Verified model caching behavior across multiple requests
  • Tested cache invalidation scenarios
  • Confirmed logging output in different environments
  • Validated fallback behavior for unknown models

Future Improvements

  • Add cache expiration mechanism
  • Implement background refresh for cached models
  • Add metrics for cache hit/miss rates
  • Consider alternative color schemes for different terminal themes
  • Add log rotation and persistence options
  • Consider implementing a shared cache for multi-instance deployments

@thecodacus thecodacus added the stable-release Used In PR: Tag to publish the changes from main to stable Branch label Dec 25, 2024
@thecodacus thecodacus added this to the v0.0.4 milestone Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stable-release Used In PR: Tag to publish the changes from main to stable Branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant