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 1/3] =?UTF-8?q?=F0=9F=9A=80=20feat(utils.py):=20add=20supp?= =?UTF-8?q?ort=20for=20configurable=20LLM=20caching=20This=20commit=20adds?= =?UTF-8?q?=20support=20for=20configurable=20LLM=20caching.=20The=20`setup?= =?UTF-8?q?=5Fllm=5Fcaching`=20function=20now=20imports=20the=20cache=20cl?= =?UTF-8?q?ass=20from=20the=20`langchain.cache`=20module=20based=20on=20th?= =?UTF-8?q?e=20`settings.cache`=20value.=20If=20the=20import=20is=20succes?= =?UTF-8?q?sful,=20the=20`langchain.llm=5Fcache`=20is=20set=20to=20an=20in?= =?UTF-8?q?stance=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}") From a15da8eb0d1697cc194917606142597db260a4f9 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 25 Jun 2023 18:20:13 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9A=80=20feat(settings.py):=20add=20c?= =?UTF-8?q?ache=20configuration=20option=20The=20cache=20configuration=20o?= =?UTF-8?q?ption=20has=20been=20added=20to=20the=20settings=20file=20with?= =?UTF-8?q?=20a=20default=20value=20of=20"InMemoryCache".=20This=20allows?= =?UTF-8?q?=20the=20user=20to=20choose=20the=20cache=20implementation=20th?= =?UTF-8?q?ey=20want=20to=20use.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/langflow/settings.py b/src/backend/langflow/settings.py index 9d6ac3fa9..e3644e84c 100644 --- a/src/backend/langflow/settings.py +++ b/src/backend/langflow/settings.py @@ -21,6 +21,7 @@ class Settings(BaseSettings): utilities: List[str] = [] dev: bool = False database_url: str = "sqlite:///./langflow.db" + cache: str = "InMemoryCache" remove_api_keys: bool = False class Config: From 5f56384dce10d327ea29c6ea35ed7417a8c7d028 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 25 Jun 2023 18:20:21 -0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9A=80=20feat(=5F=5Fmain=5F=5F.py):?= =?UTF-8?q?=20add=20support=20for=20cache=20configuration=20The=20`update?= =?UTF-8?q?=5Fsettings`=20function=20now=20accepts=20a=20`cache`=20paramet?= =?UTF-8?q?er=20that=20allows=20the=20user=20to=20specify=20the=20type=20o?= =?UTF-8?q?f=20cache=20to=20use.=20The=20`cache`=20parameter=20is=20set=20?= =?UTF-8?q?to=20a=20default=20value=20of=20`SQLiteCache`=20and=20can=20be?= =?UTF-8?q?=20overridden=20by=20setting=20the=20`LANGCHAIN=5FCACHE`=20envi?= =?UTF-8?q?ronment=20variable.=20This=20feature=20improves=20the=20flexibi?= =?UTF-8?q?lity=20of=20the=20application=20as=20it=20allows=20the=20user?= =?UTF-8?q?=20to=20choose=20the=20type=20of=20cache=20that=20best=20suits?= =?UTF-8?q?=20their=20needs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/__main__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py index 29f60ed23..a3841ec93 100644 --- a/src/backend/langflow/__main__.py +++ b/src/backend/langflow/__main__.py @@ -30,6 +30,7 @@ def get_number_of_workers(workers=None): def update_settings( config: str, + cache: str, dev: bool = False, database_url: Optional[str] = None, remove_api_keys: bool = False, @@ -41,6 +42,8 @@ def update_settings( settings.update_settings(database_url=database_url) if remove_api_keys: settings.update_settings(remove_api_keys=remove_api_keys) + if cache: + settings.update_settings(cache=cache) def serve_on_jcloud(): @@ -102,6 +105,11 @@ def serve( ), log_level: str = typer.Option("critical", help="Logging level."), log_file: Path = typer.Option("logs/langflow.log", help="Path to the log file."), + cache: str = typer.Argument( + envvar="LANGCHAIN_CACHE", + help="Type of cache to use. (InMemoryCache, SQLiteCache)", + default="SQLiteCache", + ), jcloud: bool = typer.Option(False, help="Deploy on Jina AI Cloud"), dev: bool = typer.Option(False, help="Run in development mode (may contain bugs)"), database_url: str = typer.Option( @@ -130,7 +138,11 @@ def serve( configure(log_level=log_level, log_file=log_file) update_settings( - config, dev=dev, database_url=database_url, remove_api_keys=remove_api_keys + config, + dev=dev, + database_url=database_url, + remove_api_keys=remove_api_keys, + cache=cache, ) # get the directory of the current file if not path: