From 89c2e5b0649eea501c9a10338da487cdf7617bf5 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 25 Jun 2023 18:19:59 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(utils.py):=20add=20support?= =?UTF-8?q?=20for=20configurable=20LLM=20caching=20This=20commit=20adds=20?= =?UTF-8?q?support=20for=20configurable=20LLM=20caching.=20The=20`setup=5F?= =?UTF-8?q?llm=5Fcaching`=20function=20now=20imports=20the=20cache=20class?= =?UTF-8?q?=20from=20the=20`langchain.cache`=20module=20based=20on=20the?= =?UTF-8?q?=20`settings.cache`=20value.=20If=20the=20import=20is=20success?= =?UTF-8?q?ful,=20the=20`langchain.llm=5Fcache`=20is=20set=20to=20an=20ins?= =?UTF-8?q?tance=20of=20the=20cache=20class.=20If=20the=20import=20fails,?= =?UTF-8?q?=20a=20warning=20is=20logged.=20If=20an=20exception=20is=20rais?= =?UTF-8?q?ed=20during=20the=20setup,=20a=20warning=20is=20logged=20with?= =?UTF-8?q?=20the=20error=20message.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/interface/utils.py b/src/backend/langflow/interface/utils.py index 668e1e6e4..1a37e89b4 100644 --- a/src/backend/langflow/interface/utils.py +++ b/src/backend/langflow/interface/utils.py @@ -64,7 +64,16 @@ def extract_input_variables_from_prompt(prompt: str) -> list[str]: def setup_llm_caching(): """Setup LLM caching.""" import langchain - from langchain.cache import SQLiteCache + from langflow.settings import settings + from langflow.interface.importing.utils import import_class - logger.debug("Setting up LLM caching") - langchain.llm_cache = SQLiteCache() + try: + 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__}") + except ImportError: + logger.warning(f"Could not import {settings.cache}. ") + except Exception as exc: + logger.warning(f"Could not setup LLM caching. Error: {exc}")