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 minor bugfix #release #895

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
Copy link
Collaborator

@dustinwloring1988 dustinwloring1988 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worked for static models but the dynmic models tried to call claude-3-5-sonnet-latest when using ollama, then it tried the one I had selected but never got a response. I can look into this more later one today.
image

@thecodacus
Copy link
Collaborator Author

the model selector is sometimes not updating the model correctly when selected for dropdown, can you check how can we replicate this issue ?

@chrismahoney
Copy link
Collaborator

I just tested this with the following:

  • Qwen2.5 Coder 32B: Generate a todo app
  • Llama 3.3:70B: Same thing
  • Qwen2.5 Coder 14b: Generate Snake in python

This all worked fine, which would not be the case if it was loading all of these models at once based on my hardware. I ran into issues trying to use the starter templates but I believe that's unrelated. I'm happy with this update.

@thecodacus thecodacus changed the title fix: updated logger and model caching #release fix: updated logger and model caching minor bugfix #release Dec 30, 2024
@HlebKrah
Copy link

When i add screenshot to deepseek-chat model i got this error:

Cannot read properties of undefined (reading 'toolCalls')
    at Object.flush (file:///Users/apple/Projects/dash-ai/node_modules/ai/core/generate-text/stream-text.ts:569:33)
    at invokePromiseCallback (node:internal/webstreams/util:162:10)
    at Object.<anonymous> (node:internal/webstreams/util:167:23)
    at transformStreamDefaultSinkCloseAlgorithm (node:internal/webstreams/transformstream:621:43)
    at node:internal/webstreams/transformstream:379:11
    at writableStreamDefaultControllerProcessClose (node:internal/webstreams/writablestream:1162:28)
    at writableStreamDefaultControllerAdvanceQueueIfNeeded (node:internal/webstreams/writablestream:1242:5)
    at writableStreamDefaultControllerClose (node:internal/webstreams/writablestream:1209:3)
    at writableStreamClose (node:internal/webstreams/writablestream:722:3)
    at writableStreamDefaultWriterClose (node:internal/webstreams/writablestream:1091:10)
    at writableStreamDefaultWriterCloseWithErrorPropagation (node:internal/webstreams/writablestream:1083:10)
    at node:internal/webstreams/readablestream:1558:15
    at complete (node:internal/webstreams/readablestream:1437:9)
    at processTicksAndRejections (node:internal/process/task_queues:105:5

With claude sonnet everything is fine

@dustinwloring1988
Copy link
Collaborator

@HlebKrah I dont not think deepseek supports images

@thecodacus thecodacus mentioned this pull request Dec 31, 2024
@thecodacus thecodacus merged commit 6494f5a into stackblitz-labs:main Dec 31, 2024
3 checks passed
Tony-BDS pushed a commit to Tony-BDS/bolt.diy that referenced this pull request Jan 1, 2025
…tz-labs#895)

* fix: updated logger and model caching

* usage token stream issue fix

* minor changes

* updated starter template change to fix the app title

* starter template bigfix

* fixed hydretion errors and raw logs

* removed raw log

* made auto select template false by default

* more cleaner logs and updated logic to call dynamicModels only if not found in static models

* updated starter template instructions

* browser console log improved for firefox

* provider icons fix icons
przbadu added a commit to przbadu/bolt.diy that referenced this pull request Jan 6, 2025
* Fixed console error for SettingsWIndow & Removed ts-nocheck where not needed

* fix: added wait till terminal prompt for bolt shell execution

* removed logs

* add/toc-for-readme

added a TOC for the README file, renamed some headings to better suite the TOC

* Update README.md

* feat: added terminal error capturing and automated fix prompt

* add: xAI grok-2-1212 model

* feat: Data Tab

Removed Chat History Tab
Added Data Tab
Data tab can export and delete chat history, import API keys, import and export settings

* Update DataTab.tsx

* feat: improved providers list style

made the list 2 columns wide and separate out the experimental providers

* fixed API Key import

* updated styling wordings and animations icons

* updated title header

* docs: updated setup guide to have more detailed instructions

* updated some text

* chore: update commit hash to 95dbcf1

* chore: update commit hash to de64007

* chore: update commit hash to 381d490

* chore: update commit hash to a53b10f

* Update ProvidersTab.tsx

* Update ProvidersTab.tsx

* chore: update commit hash to 75ec49b

* docs: updated style in faq

updated style in FAQ docs to be an accordion like style
added a TOC to the index page in the docs

* chore: update commit hash to 636f87f

* docs: updated Contributing

updated Contributing in the docs
updated Contributing and FAQ in the GitHub part as well

* docs: added info on updating using docker

Added docker-compose --profile development up --build  to the update section

* docs: added info on the Releases Page

Added the option to download from the Releases Page instead of git clone in the README

* docs: added info on both ways to set api keys

* chore: update commit hash to ab5cde3

* refactor: updated vite config to inject add version metadata into the app on build (stackblitz-labs#841)

* refactor: removes commit.json and used vite.config to load these variables

* updated precommit hook

* updated the pre start script

* updated the workflows

* ci: updated the docs ci to only trigger if any files changed in the docs folder (stackblitz-labs#849)

* docs: updated download link (stackblitz-labs#850)

* fix: add Message Processing Throttling to Prevent Browser Crashes (stackblitz-labs#848)

* fix hotfix for version metadata issue (stackblitz-labs#853)

* refactor:  refactored LLM Providers: Adapting Modular Approach (stackblitz-labs#832)

* refactor: Refactoring Providers to have providers as modules

* updated package and lock file

* added grok model back

* updated registry system

* ignored alert on project reload

* updated read me

* fix: provider menu dropdown fix (ghost providers) (stackblitz-labs#862)

* better osc code cleanup

* fix: ollama provider module base url hotfix for docker (stackblitz-labs#863)

* fix: ollama base url hotfix

* cleanup logic

* docs: updated env.example of OLLAMA & LMSTUDIO base url (stackblitz-labs#877)

* correct OLLAMA_API_BASE_URL

* correct OLLAMA_API_BASE_URL

* correct OLLAMA_API_BASE_URL

* fix: check for updates does not look for commit.json now (stackblitz-labs#861)

* feat: add Starter template menu in homepage (stackblitz-labs#884)

* added icons and component

* updated unocss to add dynamic icons

* removed temp logs

* updated readme

* feat: catch errors from web container preview and show in actionable alert so user can send them to AI for fixing (stackblitz-labs#856)

* Catch errors from web container

* Show fix error popup on errors in preview

* Remove unneeded action type

* PR comments

* Cleanup urls in stacktrace

---------

Co-authored-by: Anirban Kar <[email protected]>

* ci: improved change-log generation script and cleaner release ci action (stackblitz-labs#896)

* build: improved-changelog

* added a better change log script

* improved changelog script

* improved change log script

* fix: detect and remove markdown block syntax that llms sometimes hallucinate for file actions (stackblitz-labs#886)

* Clean out markdown syntax

* Remove identation removal

* Improve for streaming

* feat: redact file contents from chat and put latest files into system prompt  (stackblitz-labs#904)

* feat: added Automatic Code Template Detection And Import (stackblitz-labs#867)

* initial setup

* updated template list

* added optional switch to control this feature

* removed some logs

* fix: import folder filtering

* fix: add defaults for LMStudio to work out of the box (stackblitz-labs#928)

* feat: added hyperbolic llm models (stackblitz-labs#943)

* Added Hyperbolic Models

* Fix: Fixed problem in connecting with hyperbolic models

* added dynamic models for hyperbolic

* removed logs

* fix: refresh model list after api key changes (stackblitz-labs#944)

* fix: better model loading ui feedback and model list update (stackblitz-labs#954)

* fix: better model loading feedback and model list update

* added load on providersettings  update

* fix: updated logger and model caching minor bugfix #release (stackblitz-labs#895)

* fix: updated logger and model caching

* usage token stream issue fix

* minor changes

* updated starter template change to fix the app title

* starter template bigfix

* fixed hydretion errors and raw logs

* removed raw log

* made auto select template false by default

* more cleaner logs and updated logic to call dynamicModels only if not found in static models

* updated starter template instructions

* browser console log improved for firefox

* provider icons fix icons

* chore: release version 0.0.4

* fix: hotfix auto select starter template works without github token #release (stackblitz-labs#959)

* fix: hotfix starter template fix, updated header link to use navigate

* template auth fix

* updated changelog script

* chore: release version 0.0.5

* fix: show warning on starter template failure and continue (stackblitz-labs#960)

* Update hyperbolic.ts

Changed updated Hyperbolic Settings link

* fix: introduce our own cors proxy for git import to fix 403 errors on isometric git cors proxy (stackblitz-labs#924)

* Exploration of improving git import

* Fix our own git proxy

* Clean out file counting for progress, does not seem to work well anyways

* fix: git private clone with custom proxy (stackblitz-labs#1010)

* cookie fix

* fix: git private clone with custom proxy

* list -fix

* docs: updating copyright in LICENSE (stackblitz-labs#796)

* fix: added XAI to docker config (stackblitz-labs#274)

* commit

* Create .env.example

* Update docker-compose.yaml

---------

Co-authored-by: Anirban Kar <[email protected]>

* ci: docker Image creation pipeline (stackblitz-labs#1011)

* Create docker.yaml

* Add build target

* Use build target var

* Use github token instead

---------

Co-authored-by: kris1803 <[email protected]>
Co-authored-by: Anirban Kar <[email protected]>
Co-authored-by: Dustin Loring <[email protected]>
Co-authored-by: GK <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Cole Medin <[email protected]>
Co-authored-by: Eduard Ruzga <[email protected]>
Co-authored-by: Alex Parker <[email protected]>
Co-authored-by: Juan Manuel Campos Olvera <[email protected]>
Co-authored-by: Arsalaan Ahmed <[email protected]>
Co-authored-by: Gaurav-Wankhede <[email protected]>
Co-authored-by: Siddarth <[email protected]>
Co-authored-by: twsl <[email protected]>
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.

4 participants