From d6020d8bd786416794dd734f9d7c0637207c82be Mon Sep 17 00:00:00 2001 From: MarkLo127 Date: Thu, 12 Feb 2026 04:52:16 +0800 Subject: [PATCH] --- frontend/bun.lock | 1 + tradingagents/agents/utils/memory.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/frontend/bun.lock b/frontend/bun.lock index b29dacc6..26ad08e0 100644 --- a/frontend/bun.lock +++ b/frontend/bun.lock @@ -1,5 +1,6 @@ { "lockfileVersion": 1, + "configVersion": 0, "workspaces": { "": { "name": "frontend", diff --git a/tradingagents/agents/utils/memory.py b/tradingagents/agents/utils/memory.py index 6ee38eb1..e635185c 100644 --- a/tradingagents/agents/utils/memory.py +++ b/tradingagents/agents/utils/memory.py @@ -4,6 +4,9 @@ from chromadb.config import Settings class FinancialSituationMemory: + # Class-level cache: share a single SentenceTransformer instance across all memory objects + _model_cache: dict = {} + def __init__(self, name, config): """ Initialize the memory with configurable embedding provider. @@ -42,12 +45,17 @@ class FinancialSituationMemory: model_name = config.get("embedding_model", "all-MiniLM-L6-v2") import logging - logging.info(f"Loading local embedding model: {model_name}") - self.model = SentenceTransformer(model_name) + # Reuse cached model if already loaded (avoids loading 5 copies and OOM) + if model_name not in FinancialSituationMemory._model_cache: + logging.info(f"Loading local embedding model: {model_name}") + FinancialSituationMemory._model_cache[model_name] = SentenceTransformer(model_name) + logging.info(f"Local embedding model loaded successfully.") + else: + logging.info(f"Reusing cached embedding model: {model_name}") + + self.model = FinancialSituationMemory._model_cache[model_name] self.embedding_dim = self.model.get_sentence_embedding_dimension() - - logging.info(f"Local embedding model loaded. Dimension: {self.embedding_dim}") def _init_openai_embedding(self, config): """Initialize OpenAI embedding client."""