From 4b1fb4a49edab2cf705bd0244f90af8904243f3f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 5 Jul 2023 12:09:53 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20rename=20`cache?= =?UTF-8?q?=5Fclass`=20variable=20to=20`cache=5Ftype`=20for=20clarity=20an?= =?UTF-8?q?d=20consistency=20=F0=9F=94=A7=20chore(utils.py):=20refactor=20?= =?UTF-8?q?`setup=5Fllm=5Fcaching`=20to=20extract=20cache=20setup=20logic?= =?UTF-8?q?=20into=20a=20separate=20function=20for=20better=20modularity?= =?UTF-8?q?=20and=20readability=20The=20variable=20`cache=5Fclass`=20has?= =?UTF-8?q?=20been=20renamed=20to=20`cache=5Ftype`=20to=20improve=20clarit?= =?UTF-8?q?y=20and=20consistency=20with=20the=20naming=20conventions.=20Th?= =?UTF-8?q?e=20`setup=5Fllm=5Fcaching`=20function=20has=20been=20refactore?= =?UTF-8?q?d=20to=20extract=20the=20cache=20setup=20logic=20into=20a=20sep?= =?UTF-8?q?arate=20function=20called=20`set=5Flangchain=5Fcache`.=20This?= =?UTF-8?q?=20improves=20modularity=20and=20readability=20of=20the=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/utils.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/backend/langflow/interface/utils.py b/src/backend/langflow/interface/utils.py index ff89e92bf..1ab2b4ce5 100644 --- a/src/backend/langflow/interface/utils.py +++ b/src/backend/langflow/interface/utils.py @@ -66,17 +66,24 @@ def extract_input_variables_from_prompt(prompt: str) -> list[str]: def setup_llm_caching(): """Setup LLM caching.""" + from langflow.settings import settings + try: - import langchain - from langflow.settings import settings - from langflow.interface.importing.utils import import_class - - cache_class = import_class(f"langchain.cache.{settings.cache}") - - logger.debug(f"Setting up LLM caching with {cache_class.__name__}") - langchain.llm_cache = cache_class() - logger.info(f"LLM caching setup with {cache_class.__name__}") + set_langchain_cache(settings) except ImportError: logger.warning(f"Could not import {settings.cache}. ") except Exception as exc: logger.warning(f"Could not setup LLM caching. Error: {exc}") + + +# TODO Rename this here and in `setup_llm_caching` +def set_langchain_cache(settings): + import langchain + from langflow.interface.importing.utils import import_class + + cache_type = os.getenv("LANGFLOW_LANGCHAIN_CACHE") + cache_class = import_class(f"langchain.cache.{cache_type or settings.cache}") + + logger.debug(f"Setting up LLM caching with {cache_class.__name__}") + langchain.llm_cache = cache_class() + logger.info(f"LLM caching setup with {cache_class.__name__}")