From 571f407ef3eadfaa695f896c1cdc812a3f49ae47 Mon Sep 17 00:00:00 2001 From: Ibis Prevedello Date: Mon, 17 Apr 2023 15:59:15 -0300 Subject: [PATCH 01/43] refac: refactor tools and add QuerySQLDataBaseTool --- src/backend/langflow/config.yaml | 4 + .../langflow/interface/importing/utils.py | 4 +- src/backend/langflow/interface/tools/base.py | 110 +++++++++--------- .../langflow/interface/tools/constants.py | 3 + src/backend/langflow/interface/tools/util.py | 24 +--- 5 files changed, 66 insertions(+), 79 deletions(-) diff --git a/src/backend/langflow/config.yaml b/src/backend/langflow/config.yaml index 7f00167b4..3508f5196 100644 --- a/src/backend/langflow/config.yaml +++ b/src/backend/langflow/config.yaml @@ -40,6 +40,10 @@ tools: - Tool - PythonFunction - JsonSpec + - News API + - TMDB API + - Podcast API + - QuerySQLDataBaseTool wrappers: - RequestsWrapper diff --git a/src/backend/langflow/interface/importing/utils.py b/src/backend/langflow/interface/importing/utils.py index a3480928e..c426eaf85 100644 --- a/src/backend/langflow/interface/importing/utils.py +++ b/src/backend/langflow/interface/importing/utils.py @@ -10,7 +10,7 @@ from langchain.chat_models.base import BaseChatModel from langchain.llms.base import BaseLLM from langchain.tools import BaseTool -from langflow.interface.tools.util import get_tool_by_name +from langflow.interface.tools.base import tool_creator def import_module(module_path: str) -> Any: @@ -107,7 +107,7 @@ def import_llm(llm: str) -> BaseLLM: def import_tool(tool: str) -> BaseTool: """Import tool from tool name""" - return get_tool_by_name(tool) + return tool_creator.type_to_loader_dict[tool]["fcn"] def import_chain(chain: str) -> Type[Chain]: diff --git a/src/backend/langflow/interface/tools/base.py b/src/backend/langflow/interface/tools/base.py index 6a3439bf0..a5745f1d3 100644 --- a/src/backend/langflow/interface/tools/base.py +++ b/src/backend/langflow/interface/tools/base.py @@ -1,7 +1,7 @@ +import inspect from typing import Dict, List, Optional from langchain.agents.load_tools import ( - _BASE_TOOLS, _EXTRA_LLM_TOOLS, _EXTRA_OPTIONAL_TOOLS, _LLM_TOOLS, @@ -10,17 +10,16 @@ from langchain.agents.load_tools import ( from langflow.custom import customs from langflow.interface.base import LangChainTypeCreator from langflow.interface.tools.constants import ( + ALL_TOOLS_NAMES, CUSTOM_TOOLS, FILE_TOOLS, + OTHER_TOOLS, ) -from langflow.interface.tools.util import ( - get_tool_by_name, - get_tool_params, - get_tools_dict, -) +from langflow.interface.tools.util import get_tool_params from langflow.settings import settings from langflow.template.base import Template, TemplateField from langflow.utils import util +from langflow.utils.util import build_template_from_class TOOL_INPUTS = { "str": TemplateField( @@ -66,64 +65,79 @@ class ToolCreator(LangChainTypeCreator): @property def type_to_loader_dict(self) -> Dict: if self.tools_dict is None: - self.tools_dict = get_tools_dict() + all_tools = {} + for tool, tool_fcn in ALL_TOOLS_NAMES.items(): + tool_params = get_tool_params(tool_fcn) + tool_name = tool_params.get("name", tool) + + if tool_name in settings.tools or settings.dev: + if tool_name == "JsonSpec": + tool_params["path"] = tool_params.pop("dict_") # type: ignore + all_tools[tool_name] = { + "type": tool, + "params": tool_params, + "fcn": tool_fcn, + } + + self.tools_dict = all_tools + return self.tools_dict def get_signature(self, name: str) -> Optional[Dict]: """Get the signature of a tool.""" base_classes = ["Tool"] - all_tools = {} - for tool in self.type_to_loader_dict.keys(): - tool_fcn = get_tool_by_name(tool) - if tool_params := get_tool_params(tool_fcn): - tool_name = tool_params.get("name") or str(tool) - all_tools[tool_name] = { - "type": tool, - "params": tool_params, - "fcn": tool_fcn, - } + fields = [] + params = [] + tool_params = {} # Raise error if name is not in tools - if name not in all_tools.keys(): + if name not in self.type_to_loader_dict.keys(): raise ValueError("Tool not found") - tool_type: str = all_tools[name]["type"] # type: ignore + tool_type: str = self.type_to_loader_dict[name]["type"] # type: ignore - if all_tools[tool_type]["fcn"] in _BASE_TOOLS.values(): - params = [] - elif all_tools[tool_type]["fcn"] in _LLM_TOOLS.values(): + # if tool_type in _BASE_TOOLS.keys(): + # params = [] + if tool_type in _LLM_TOOLS.keys(): params = ["llm"] - elif all_tools[tool_type]["fcn"] in [ - val[0] for val in _EXTRA_LLM_TOOLS.values() - ]: - n_dict = {val[0]: val[1] for val in _EXTRA_LLM_TOOLS.values()} - extra_keys = n_dict[all_tools[tool_type]["fcn"]] + elif tool_type in _EXTRA_LLM_TOOLS.keys(): + extra_keys = _EXTRA_LLM_TOOLS[tool_type][1] params = ["llm"] + extra_keys - elif all_tools[tool_type]["fcn"] in [ - val[0] for val in _EXTRA_OPTIONAL_TOOLS.values() - ]: - n_dict = {val[0]: val[1] for val in _EXTRA_OPTIONAL_TOOLS.values()} # type: ignore - extra_keys = n_dict[all_tools[tool_type]["fcn"]] + elif tool_type in _EXTRA_OPTIONAL_TOOLS.keys(): + extra_keys = _EXTRA_OPTIONAL_TOOLS[tool_type][1] params = extra_keys # elif tool_type == "Tool": # params = ["name", "description", "func"] elif tool_type in CUSTOM_TOOLS: # Get custom tool params - params = all_tools[name]["params"] # type: ignore + params = self.type_to_loader_dict[name]["params"] # type: ignore base_classes = ["function"] if node := customs.get_custom_nodes("tools").get(tool_type): return node elif tool_type in FILE_TOOLS: - params = all_tools[name]["params"] # type: ignore - if tool_type == "JsonSpec": - params["path"] = params.pop("dict_") # type: ignore + params = self.type_to_loader_dict[name]["params"] # type: ignore base_classes += [name] - else: - params = [] + elif tool_type in OTHER_TOOLS: + tool_dict = build_template_from_class(tool_type, OTHER_TOOLS) + fields = tool_dict["template"] + + # Pop unnecessary fields and add name + fields.pop("_type") + fields.pop("return_direct") + fields.pop("verbose") + + tool_params = { + "name": fields.pop("name")["value"], + "description": fields.pop("description")["value"], + } + + fields = [ + TemplateField(name=name, **field) for name, field in fields.items() + ] + base_classes += tool_dict["base_classes"] # Copy the field and add the name - fields = [] for param in params: field = TOOL_INPUTS.get(param, TOOL_INPUTS["str"]).copy() field.name = param @@ -134,7 +148,7 @@ class ToolCreator(LangChainTypeCreator): template = Template(fields=fields, type_name=tool_type) - tool_params = all_tools[name]["params"] + tool_params = {**tool_params, **self.type_to_loader_dict[name]["params"]} return { "template": util.format_dict(template.to_dict()), **tool_params, @@ -144,21 +158,7 @@ class ToolCreator(LangChainTypeCreator): def to_list(self) -> List[str]: """List all load tools""" - tools = [] - - for tool, fcn in get_tools_dict().items(): - tool_params = get_tool_params(fcn) - - if tool_params and not tool_params.get("name"): - tool_params["name"] = tool - - if tool_params and ( - tool_params.get("name") in settings.tools - or (tool_params.get("name") and settings.dev) - ): - tools.append(tool_params["name"]) - - return tools + return list(self.type_to_loader_dict.keys()) tool_creator = ToolCreator() diff --git a/src/backend/langflow/interface/tools/constants.py b/src/backend/langflow/interface/tools/constants.py index 2ec20cef9..101fefa8b 100644 --- a/src/backend/langflow/interface/tools/constants.py +++ b/src/backend/langflow/interface/tools/constants.py @@ -6,11 +6,13 @@ from langchain.agents.load_tools import ( _LLM_TOOLS, ) from langchain.tools.json.tool import JsonSpec +from langchain.tools.sql_database.tool import QuerySQLDataBaseTool from langflow.interface.tools.custom import PythonFunction FILE_TOOLS = {"JsonSpec": JsonSpec} CUSTOM_TOOLS = {"Tool": Tool, "PythonFunction": PythonFunction} +OTHER_TOOLS = {"QuerySQLDataBaseTool": QuerySQLDataBaseTool} ALL_TOOLS_NAMES = { **_BASE_TOOLS, **_LLM_TOOLS, # type: ignore @@ -18,4 +20,5 @@ ALL_TOOLS_NAMES = { **{k: v[0] for k, v in _EXTRA_OPTIONAL_TOOLS.items()}, **CUSTOM_TOOLS, **FILE_TOOLS, # type: ignore + **OTHER_TOOLS, } diff --git a/src/backend/langflow/interface/tools/util.py b/src/backend/langflow/interface/tools/util.py index 8f8673b6b..41fb4bdb7 100644 --- a/src/backend/langflow/interface/tools/util.py +++ b/src/backend/langflow/interface/tools/util.py @@ -4,28 +4,6 @@ from typing import Dict, Union from langchain.agents.tools import Tool -from langflow.interface.tools.constants import ALL_TOOLS_NAMES - - -def get_tools_dict(): - """Get the tools dictionary.""" - - all_tools = {} - - for tool, fcn in ALL_TOOLS_NAMES.items(): - if tool_params := get_tool_params(fcn): - tool_name = tool_params.get("name") or str(tool) - all_tools[tool_name] = fcn - - return all_tools - - -def get_tool_by_name(name: str): - """Get a tool from the tools dictionary.""" - tools = get_tools_dict() - if name not in tools: - raise ValueError(f"{name} not found.") - return tools[name] def get_func_tool_params(func, **kwargs) -> Union[Dict, None]: @@ -113,6 +91,8 @@ def get_tool_params(tool, **kwargs) -> Dict: elif inspect.isclass(tool): # Get the parameters necessary to # instantiate the class + return get_class_tool_params(tool, **kwargs) or {} + else: raise ValueError("Tool must be a function or class.") From 843ae8efc50253bdea3fd9385a5e646a905ab34e Mon Sep 17 00:00:00 2001 From: Ibis Prevedello Date: Mon, 17 Apr 2023 23:15:39 -0300 Subject: [PATCH 02/43] feat: add extra tools and utilities --- src/backend/langflow/config.yaml | 28 +++++++++++++ .../langflow/interface/custom_lists.py | 6 +++ src/backend/langflow/interface/listing.py | 2 + src/backend/langflow/interface/tools/base.py | 14 ++++--- .../langflow/interface/tools/constants.py | 42 +++++++++++++++++-- src/backend/langflow/interface/tools/util.py | 1 - src/backend/langflow/interface/types.py | 2 + .../langflow/interface/utilities/__init__.py | 0 .../langflow/interface/utilities/base.py | 36 ++++++++++++++++ src/backend/langflow/settings.py | 2 + src/frontend/src/utils.ts | 6 ++- 11 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 src/backend/langflow/interface/utilities/__init__.py create mode 100644 src/backend/langflow/interface/utilities/base.py diff --git a/src/backend/langflow/config.yaml b/src/backend/langflow/config.yaml index 3508f5196..605df68ad 100644 --- a/src/backend/langflow/config.yaml +++ b/src/backend/langflow/config.yaml @@ -44,6 +44,23 @@ tools: - TMDB API - Podcast API - QuerySQLDataBaseTool + - InfoSQLDatabaseTool + - ListSQLDatabaseTool + # - QueryCheckerTool + - BingSearchRun + - GoogleSearchRun + - GoogleSearchResults + - JsonListKeysTool + - JsonGetValueTool + - PythonREPLTool + - PythonAstREPLTool + - RequestsGetTool + - RequestsPostTool + - RequestsPatchTool + - RequestsPutTool + - RequestsDeleteTool + - WikipediaQueryRun + - WolframAlphaQueryRun wrappers: - RequestsWrapper @@ -95,4 +112,15 @@ documentloaders: textsplitters: - CharacterTextSplitter +utilities: + - BingSearchAPIWrapper + - GoogleSearchAPIWrapper + - GoogleSerperAPIWrapper + - SearxResults + - SearxSearchWrapper + - SerpAPIWrapper + - WikipediaAPIWrapper + - WolframAlphaAPIWrapper + # - ZapierNLAWrapper + dev: false diff --git a/src/backend/langflow/interface/custom_lists.py b/src/backend/langflow/interface/custom_lists.py index 2ffc18b14..fb97e8dae 100644 --- a/src/backend/langflow/interface/custom_lists.py +++ b/src/backend/langflow/interface/custom_lists.py @@ -9,6 +9,7 @@ from langchain import ( memory, requests, text_splitter, + utilities, vectorstores, ) from langchain.agents import agent_toolkits @@ -76,3 +77,8 @@ documentloaders_type_to_cls_dict: dict[str, Any] = { textsplitter_type_to_cls_dict: dict[str, Any] = dict( inspect.getmembers(text_splitter, inspect.isclass) ) + +## Utilities +utility_type_to_cls_dict: dict[str, Any] = dict( + inspect.getmembers(utilities, inspect.isclass) +) diff --git a/src/backend/langflow/interface/listing.py b/src/backend/langflow/interface/listing.py index cf45fd9c5..3d73105c2 100644 --- a/src/backend/langflow/interface/listing.py +++ b/src/backend/langflow/interface/listing.py @@ -8,6 +8,7 @@ from langflow.interface.prompts.base import prompt_creator from langflow.interface.text_splitters.base import textsplitter_creator from langflow.interface.toolkits.base import toolkits_creator from langflow.interface.tools.base import tool_creator +from langflow.interface.utilities.base import utility_creator from langflow.interface.vector_store.base import vectorstore_creator from langflow.interface.wrappers.base import wrapper_creator @@ -26,6 +27,7 @@ def get_type_dict(): "vectorStore": vectorstore_creator.to_list(), "embeddings": embedding_creator.to_list(), "textSplitters": textsplitter_creator.to_list(), + "utilities": utility_creator.to_list(), } diff --git a/src/backend/langflow/interface/tools/base.py b/src/backend/langflow/interface/tools/base.py index a5745f1d3..f236831d8 100644 --- a/src/backend/langflow/interface/tools/base.py +++ b/src/backend/langflow/interface/tools/base.py @@ -119,21 +119,23 @@ class ToolCreator(LangChainTypeCreator): params = self.type_to_loader_dict[name]["params"] # type: ignore base_classes += [name] elif tool_type in OTHER_TOOLS: + print(tool_type) tool_dict = build_template_from_class(tool_type, OTHER_TOOLS) fields = tool_dict["template"] # Pop unnecessary fields and add name - fields.pop("_type") - fields.pop("return_direct") - fields.pop("verbose") + fields.pop("_type") # type: ignore + fields.pop("return_direct") # type: ignore + fields.pop("verbose") # type: ignore tool_params = { - "name": fields.pop("name")["value"], - "description": fields.pop("description")["value"], + "name": fields.pop("name")["value"], # type: ignore + "description": fields.pop("description")["value"], # type: ignore } fields = [ - TemplateField(name=name, **field) for name, field in fields.items() + TemplateField(name=name, field_type=field["type"], **field) + for name, field in fields.items() # type: ignore ] base_classes += tool_dict["base_classes"] diff --git a/src/backend/langflow/interface/tools/constants.py b/src/backend/langflow/interface/tools/constants.py index 101fefa8b..34890a684 100644 --- a/src/backend/langflow/interface/tools/constants.py +++ b/src/backend/langflow/interface/tools/constants.py @@ -5,14 +5,50 @@ from langchain.agents.load_tools import ( _EXTRA_OPTIONAL_TOOLS, _LLM_TOOLS, ) -from langchain.tools.json.tool import JsonSpec -from langchain.tools.sql_database.tool import QuerySQLDataBaseTool +from langchain.tools.bing_search.tool import BingSearchRun +from langchain.tools.google_search.tool import GoogleSearchResults, GoogleSearchRun +from langchain.tools.json.tool import JsonGetValueTool, JsonListKeysTool, JsonSpec +from langchain.tools.python.tool import PythonAstREPLTool, PythonREPLTool +from langchain.tools.requests.tool import ( + RequestsDeleteTool, + RequestsGetTool, + RequestsPatchTool, + RequestsPostTool, + RequestsPutTool, +) +from langchain.tools.sql_database.tool import ( + InfoSQLDatabaseTool, + ListSQLDatabaseTool, + QueryCheckerTool, + QuerySQLDataBaseTool, +) +from langchain.tools.wikipedia.tool import WikipediaQueryRun +from langchain.tools.wolfram_alpha.tool import WolframAlphaQueryRun from langflow.interface.tools.custom import PythonFunction FILE_TOOLS = {"JsonSpec": JsonSpec} CUSTOM_TOOLS = {"Tool": Tool, "PythonFunction": PythonFunction} -OTHER_TOOLS = {"QuerySQLDataBaseTool": QuerySQLDataBaseTool} +OTHER_TOOLS = { + "QuerySQLDataBaseTool": QuerySQLDataBaseTool, + "InfoSQLDatabaseTool": InfoSQLDatabaseTool, + "ListSQLDatabaseTool": ListSQLDatabaseTool, + "QueryCheckerTool": QueryCheckerTool, + "BingSearchRun": BingSearchRun, + "GoogleSearchRun": GoogleSearchRun, + "GoogleSearchResults": GoogleSearchResults, + "JsonListKeysTool": JsonListKeysTool, + "JsonGetValueTool": JsonGetValueTool, + "PythonREPLTool": PythonREPLTool, + "PythonAstREPLTool": PythonAstREPLTool, + "RequestsGetTool": RequestsGetTool, + "RequestsPostTool": RequestsPostTool, + "RequestsPatchTool": RequestsPatchTool, + "RequestsPutTool": RequestsPutTool, + "RequestsDeleteTool": RequestsDeleteTool, + "WikipediaQueryRun": WikipediaQueryRun, + "WolframAlphaQueryRun": WolframAlphaQueryRun, +} ALL_TOOLS_NAMES = { **_BASE_TOOLS, **_LLM_TOOLS, # type: ignore diff --git a/src/backend/langflow/interface/tools/util.py b/src/backend/langflow/interface/tools/util.py index 41fb4bdb7..f1d66696a 100644 --- a/src/backend/langflow/interface/tools/util.py +++ b/src/backend/langflow/interface/tools/util.py @@ -5,7 +5,6 @@ from typing import Dict, Union from langchain.agents.tools import Tool - def get_func_tool_params(func, **kwargs) -> Union[Dict, None]: tree = ast.parse(inspect.getsource(func)) diff --git a/src/backend/langflow/interface/types.py b/src/backend/langflow/interface/types.py index fd5d9ec3e..085537756 100644 --- a/src/backend/langflow/interface/types.py +++ b/src/backend/langflow/interface/types.py @@ -8,6 +8,7 @@ from langflow.interface.prompts.base import prompt_creator from langflow.interface.text_splitters.base import textsplitter_creator from langflow.interface.toolkits.base import toolkits_creator from langflow.interface.tools.base import tool_creator +from langflow.interface.utilities.base import utility_creator from langflow.interface.vector_store.base import vectorstore_creator from langflow.interface.wrappers.base import wrapper_creator @@ -42,6 +43,7 @@ def build_langchain_types_dict(): # sourcery skip: dict-assign-update-to-union vectorstore_creator, documentloader_creator, textsplitter_creator, + utility_creator, ] all_types = {} diff --git a/src/backend/langflow/interface/utilities/__init__.py b/src/backend/langflow/interface/utilities/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/backend/langflow/interface/utilities/base.py b/src/backend/langflow/interface/utilities/base.py new file mode 100644 index 000000000..a56e8bce8 --- /dev/null +++ b/src/backend/langflow/interface/utilities/base.py @@ -0,0 +1,36 @@ +from typing import Dict, List, Optional + +from langflow.interface.base import LangChainTypeCreator +from langflow.interface.custom_lists import utility_type_to_cls_dict +from langflow.settings import settings +from langflow.utils.logger import logger +from langflow.utils.util import build_template_from_class + + +class UtilityCreator(LangChainTypeCreator): + type_name: str = "utilities" + + @property + def type_to_loader_dict(self) -> Dict: + return utility_type_to_cls_dict + + def get_signature(self, name: str) -> Optional[Dict]: + """Get the signature of a utility.""" + try: + return build_template_from_class(name, utility_type_to_cls_dict) + except ValueError as exc: + raise ValueError(f"Utility {name} not found") from exc + + except AttributeError as exc: + logger.error(f"Utility {name} not loaded: {exc}") + return None + + def to_list(self) -> List[str]: + return [ + utility.__name__ + for utility in self.type_to_loader_dict.values() + if utility.__name__ in settings.utilities or settings.dev + ] + + +utility_creator = UtilityCreator() diff --git a/src/backend/langflow/settings.py b/src/backend/langflow/settings.py index c5377c85a..48aa5939d 100644 --- a/src/backend/langflow/settings.py +++ b/src/backend/langflow/settings.py @@ -18,6 +18,7 @@ class Settings(BaseSettings): wrappers: List[str] = [] toolkits: List[str] = [] textsplitters: List[str] = [] + utilities: List[str] = [] dev: bool = False class Config: @@ -42,6 +43,7 @@ class Settings(BaseSettings): self.wrappers = new_settings.wrappers or [] self.toolkits = new_settings.toolkits or [] self.textsplitters = new_settings.textsplitters or [] + self.utilities = new_settings.utilities or [] self.dev = new_settings.dev or False diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 5ab47f31e..405d56297 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -13,7 +13,8 @@ import { QuestionMarkCircleIcon, FingerPrintIcon, ScissorsIcon, - CircleStackIcon + CircleStackIcon, + Squares2X2Icon } from "@heroicons/react/24/outline"; import { Connection, Edge, Node, ReactFlowInstance } from "reactflow"; import { FlowType } from "./types/flow"; @@ -85,6 +86,7 @@ export const nodeColors: {[char: string]: string} = { textsplitters: "#B47CB5", toolkits:"#DB2C2C", wrappers:"#E6277A", + utilities:"#31A3CC", unknown:"#9CA3AF" }; @@ -103,6 +105,7 @@ export const nodeNames:{[char: string]: string} = { toolkits:"Toolkits", wrappers:"Wrappers", textsplitters: "Text Splitters", + utilities:"Utilities", unknown:"Unknown" }; @@ -121,6 +124,7 @@ export const nodeIcons:{[char: string]: React.ForwardRefExoticComponent Date: Wed, 19 Apr 2023 10:31:13 -0300 Subject: [PATCH 03/43] feat: add SQLDatabaseChain Close #173 --- src/backend/langflow/config.yaml | 2 ++ src/backend/langflow/custom/customs.py | 3 +++ src/backend/langflow/graph/base.py | 6 ++++- src/backend/langflow/graph/nodes.py | 4 ++++ .../langflow/interface/custom_lists.py | 2 ++ .../langflow/interface/importing/utils.py | 9 +++++++- src/backend/langflow/interface/loading.py | 3 +++ src/backend/langflow/interface/tools/base.py | 1 - .../langflow/interface/utilities/base.py | 3 +++ src/backend/langflow/template/nodes.py | 23 +++++++++++++++++++ 10 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/config.yaml b/src/backend/langflow/config.yaml index 605df68ad..9236d5996 100644 --- a/src/backend/langflow/config.yaml +++ b/src/backend/langflow/config.yaml @@ -6,6 +6,7 @@ chains: - SeriesCharacterChain - MidJourneyPromptChain - TimeTravelGuideChain + - SQLDatabaseChain agents: - ZeroShotAgent @@ -122,5 +123,6 @@ utilities: - WikipediaAPIWrapper - WolframAlphaAPIWrapper # - ZapierNLAWrapper + - SQLDatabase dev: false diff --git a/src/backend/langflow/custom/customs.py b/src/backend/langflow/custom/customs.py index e77b81ec6..d45221be7 100644 --- a/src/backend/langflow/custom/customs.py +++ b/src/backend/langflow/custom/customs.py @@ -12,6 +12,9 @@ CUSTOM_NODES = { "VectorStoreRouterAgent": nodes.VectorStoreRouterAgentNode(), "SQLAgent": nodes.SQLAgentNode(), }, + "utilities": { + "SQLDatabase": nodes.SQLDatabaseNode(), + }, } diff --git a/src/backend/langflow/graph/base.py b/src/backend/langflow/graph/base.py index ff586c6da..6d998eed6 100644 --- a/src/backend/langflow/graph/base.py +++ b/src/backend/langflow/graph/base.py @@ -202,7 +202,11 @@ class Node: "VectorStoreRouterAgent", "VectorStoreAgent", "VectorStoreInfo", - ] or self.node_type in ["VectorStoreInfo", "VectorStoreRouterToolkit"]: + ] or self.node_type in [ + "VectorStoreInfo", + "VectorStoreRouterToolkit", + "SQLDatabase", + ]: return self._built_object return deepcopy(self._built_object) diff --git a/src/backend/langflow/graph/nodes.py b/src/backend/langflow/graph/nodes.py index 7296a0c0d..018174334 100644 --- a/src/backend/langflow/graph/nodes.py +++ b/src/backend/langflow/graph/nodes.py @@ -101,6 +101,10 @@ class ChainNode(Node): self.params[key] = value.build(tools=tools, force=force) self._build() + + #! Cannot deepcopy SQLDatabaseChain + if self.node_type in ["SQLDatabaseChain"]: + return self._built_object return deepcopy(self._built_object) diff --git a/src/backend/langflow/interface/custom_lists.py b/src/backend/langflow/interface/custom_lists.py index fb97e8dae..f07b03f04 100644 --- a/src/backend/langflow/interface/custom_lists.py +++ b/src/backend/langflow/interface/custom_lists.py @@ -14,6 +14,7 @@ from langchain import ( ) from langchain.agents import agent_toolkits from langchain.chat_models import ChatOpenAI +from langchain.sql_database import SQLDatabase from langflow.interface.importing.utils import import_class @@ -82,3 +83,4 @@ textsplitter_type_to_cls_dict: dict[str, Any] = dict( utility_type_to_cls_dict: dict[str, Any] = dict( inspect.getmembers(utilities, inspect.isclass) ) +utility_type_to_cls_dict["SQLDatabase"] = SQLDatabase diff --git a/src/backend/langflow/interface/importing/utils.py b/src/backend/langflow/interface/importing/utils.py index c426eaf85..e303da0eb 100644 --- a/src/backend/langflow/interface/importing/utils.py +++ b/src/backend/langflow/interface/importing/utils.py @@ -44,6 +44,7 @@ def import_by_type(_type: str, name: str) -> Any: "vectorstores": import_vectorstore, "documentloaders": import_documentloader, "textsplitters": import_textsplitter, + "utilities": import_utility, } if _type == "llms": key = "chat" if "chat" in name.lower() else "llm" @@ -131,10 +132,16 @@ def import_vectorstore(vectorstore: str) -> Any: def import_documentloader(documentloader: str) -> Any: """Import documentloader from documentloader name""" - return import_class(f"langchain.document_loaders.{documentloader}") def import_textsplitter(textsplitter: str) -> Any: """Import textsplitter from textsplitter name""" return import_class(f"langchain.text_splitter.{textsplitter}") + + +def import_utility(utility: str) -> Any: + """Import utility from utility name""" + if utility == "SQLDatabase": + return import_class(f"langchain.sql_database.{utility}") + return import_class(f"langchain.utilities.{utility}") diff --git a/src/backend/langflow/interface/loading.py b/src/backend/langflow/interface/loading.py index 11db47ee6..0fa1c5f2e 100644 --- a/src/backend/langflow/interface/loading.py +++ b/src/backend/langflow/interface/loading.py @@ -75,6 +75,9 @@ def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any: documents = params.pop("documents") text_splitter = class_object(**params) return text_splitter.split_documents(documents) + elif base_type == "utilities": + if node_type == "SQLDatabase": + return class_object.from_uri(params.pop("uri")) return class_object(**params) diff --git a/src/backend/langflow/interface/tools/base.py b/src/backend/langflow/interface/tools/base.py index f236831d8..5fd0c72f0 100644 --- a/src/backend/langflow/interface/tools/base.py +++ b/src/backend/langflow/interface/tools/base.py @@ -1,4 +1,3 @@ -import inspect from typing import Dict, List, Optional from langchain.agents.load_tools import ( diff --git a/src/backend/langflow/interface/utilities/base.py b/src/backend/langflow/interface/utilities/base.py index a56e8bce8..e60e344ad 100644 --- a/src/backend/langflow/interface/utilities/base.py +++ b/src/backend/langflow/interface/utilities/base.py @@ -1,5 +1,6 @@ from typing import Dict, List, Optional +from langflow.custom.customs import get_custom_nodes from langflow.interface.base import LangChainTypeCreator from langflow.interface.custom_lists import utility_type_to_cls_dict from langflow.settings import settings @@ -17,6 +18,8 @@ class UtilityCreator(LangChainTypeCreator): def get_signature(self, name: str) -> Optional[Dict]: """Get the signature of a utility.""" try: + if name in get_custom_nodes(self.type_name).keys(): + return get_custom_nodes(self.type_name)[name] return build_template_from_class(name, utility_type_to_cls_dict) except ValueError as exc: raise ValueError(f"Utility {name} not found") from exc diff --git a/src/backend/langflow/template/nodes.py b/src/backend/langflow/template/nodes.py index 6ac026e59..f2e8bd94f 100644 --- a/src/backend/langflow/template/nodes.py +++ b/src/backend/langflow/template/nodes.py @@ -256,6 +256,29 @@ class CSVAgentNode(FrontendNode): return super().to_dict() +class SQLDatabaseNode(FrontendNode): + name: str = "SQLDatabase" + template: Template = Template( + type_name="sql_database", + fields=[ + TemplateField( + field_type="str", + required=True, + is_list=False, + show=True, + multiline=False, + value="", + name="uri", + ), + ], + ) + description: str = """SQLAlchemy wrapper around a database.""" + base_classes: list[str] = ["SQLDatabase"] + + def to_dict(self): + return super().to_dict() + + class VectorStoreAgentNode(FrontendNode): name: str = "VectorStoreAgent" template: Template = Template( From 68f69e878ec4d3aa82487145d44a3601c2396123 Mon Sep 17 00:00:00 2001 From: Petru Molla Date: Mon, 17 Apr 2023 17:38:44 +0300 Subject: [PATCH 04/43] Using an exported Flow JSON file with load_flow_from_json("path/to/flow.json") returns "UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 9465: character maps to ". Specifying the encodint type in the function helps to fix that error. --- src/backend/langflow/interface/loading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/langflow/interface/loading.py b/src/backend/langflow/interface/loading.py index 0fa1c5f2e..0d4465e7e 100644 --- a/src/backend/langflow/interface/loading.py +++ b/src/backend/langflow/interface/loading.py @@ -87,7 +87,7 @@ def load_flow_from_json(path: str): from langflow.graph import Graph """Load flow from json file""" - with open(path, "r") as f: + with open(path, "r", encoding='utf-8') as f: flow_graph = json.load(f) data_graph = flow_graph["data"] nodes = data_graph["nodes"] From ded527a64a257896a0ab0222b2105f5f179ba99b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 20 Apr 2023 15:18:59 -0300 Subject: [PATCH 05/43] Update loading.py --- src/backend/langflow/interface/loading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/langflow/interface/loading.py b/src/backend/langflow/interface/loading.py index 0d4465e7e..333c0307c 100644 --- a/src/backend/langflow/interface/loading.py +++ b/src/backend/langflow/interface/loading.py @@ -87,7 +87,7 @@ def load_flow_from_json(path: str): from langflow.graph import Graph """Load flow from json file""" - with open(path, "r", encoding='utf-8') as f: + with open(path, "r", encoding=utf-8") as f: flow_graph = json.load(f) data_graph = flow_graph["data"] nodes = data_graph["nodes"] From 801ff57dc085f9e4ae74702760ccf321c05572e1 Mon Sep 17 00:00:00 2001 From: Petru Molla <112887899+bigKeter@users.noreply.github.com> Date: Thu, 20 Apr 2023 21:56:10 +0300 Subject: [PATCH 06/43] Update loading.py --- src/backend/langflow/interface/loading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/langflow/interface/loading.py b/src/backend/langflow/interface/loading.py index 333c0307c..3e3accef0 100644 --- a/src/backend/langflow/interface/loading.py +++ b/src/backend/langflow/interface/loading.py @@ -87,7 +87,7 @@ def load_flow_from_json(path: str): from langflow.graph import Graph """Load flow from json file""" - with open(path, "r", encoding=utf-8") as f: + with open(path, "r", encoding="utf-8") as f: flow_graph = json.load(f) data_graph = flow_graph["data"] nodes = data_graph["nodes"] From 2465318f77e679d7ed3c9ddf38c4e380126df7f5 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 03:07:05 +0000 Subject: [PATCH 07/43] Add steps to provision GCP VM serving langflow --- GCP-SETUP.md | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 GCP-SETUP.md diff --git a/GCP-SETUP.md b/GCP-SETUP.md new file mode 100644 index 000000000..ba6d61404 --- /dev/null +++ b/GCP-SETUP.md @@ -0,0 +1,90 @@ +# Running Langflow from a new GCP project +## Run the following in your GCP cloudshell: + +```bash + +VM_NAME="langflow-dev" +IMAGE_FAMILY="debian-11" +IMAGE_PROJECT="debian-cloud" +BOOT_DISK_SIZE="100GB" +ZONE="us-central1-a" +REGION="us-central1" +VPC_NAME="default" +SUBNET_NAME="default" +NAT_GATEWAY_NAME="nat-gateway" +CLOUD_ROUTER_NAME="nat-client" + +gcloud config set compute/region $REGION + +# Verify the VPC and subnet exist +vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") +subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") + +if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then + echo "Error: VPC '$VPC_NAME' and/or subnet '$SUBNET_NAME' do not exist in region '$REGION'." + exit 1 +fi + +# Create the Cloud Router and NAT Gateway +gcloud compute routers create $CLOUD_ROUTER_NAME \ + --network $VPC_NAME \ + --region $REGION + +gcloud compute routers nats create $NAT_GATEWAY_NAME \ + --router $CLOUD_ROUTER_NAME \ + --auto-allocate-nat-external-ips \ + --nat-all-subnet-ip-ranges \ + --enable-logging \ + --region $REGION + +# Define the startup script as a multiline Bash here-doc +STARTUP_SCRIPT=$(cat <<'EOF' +#!/bin/bash + +apt update +apt upgrade +apt install python3-pip +pip install langflow +apt-get install nginx +touch /etc/nginx/sites-available/langflow-app +echo "server { + listen 0.0.0.0:7860; + + location / { + proxy_pass http://127.0.0.1:7860; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } +}" >> /etc/nginx/sites-available/langflow-app +ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/ +sudo nginx -t +sudo systemctl restart nginx +langflow +EOF +) + +# Create a temporary file to store the startup script +tempfile=$(mktemp) +echo "$STARTUP_SCRIPT" > $tempfile + +gcloud compute instances create $VM_NAME \ + --image-family $IMAGE_FAMILY \ + --image-project $IMAGE_PROJECT \ + --boot-disk-size $BOOT_DISK_SIZE \ + --metadata-from-file startup-script=$tempfile \ + --zone $ZONE \ + --network $VPC_NAME \ + --subnet $SUBNET_NAME + +# Remove the temporary file after the VM is created +rm $tempfile + +``` + +## Connecting to your new Langflow VM +1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page +2. Click on the external IP for your VM +3. Add port 8080 (assuming your VM external IP is 192.168.0.1): +http://192.168.0.1:8080 +4. You will be greeted by the Langflow Dev environment \ No newline at end of file From 7011721c3cbe49d7bf2b6a09aca1845f154a473b Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 03:14:45 +0000 Subject: [PATCH 08/43] Adjust firewall to allow port 8080 --- GCP-SETUP.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index ba6d61404..53646d4b0 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -25,6 +25,13 @@ if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then exit 1 fi +gcloud compute firewall-rules create allow-tcp-8080 \ + --network $VPC_NAME \ + --allow tcp:8080 \ + --source-ranges 0.0.0.0/0 \ + --direction INGRESS + + # Create the Cloud Router and NAT Gateway gcloud compute routers create $CLOUD_ROUTER_NAME \ --network $VPC_NAME \ From d6b179b7ce2dfd42b408f2e9a736624c9f2bfcfe Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 03:42:01 +0000 Subject: [PATCH 09/43] Add comments to clarify code --- GCP-SETUP.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index 53646d4b0..1f4eb6b4e 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -1,8 +1,11 @@ # Running Langflow from a new GCP project +This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. + + ## Run the following in your GCP cloudshell: ```bash - +# Set the VM, image, and networking configuration VM_NAME="langflow-dev" IMAGE_FAMILY="debian-11" IMAGE_PROJECT="debian-cloud" @@ -14,17 +17,20 @@ SUBNET_NAME="default" NAT_GATEWAY_NAME="nat-gateway" CLOUD_ROUTER_NAME="nat-client" +# Set the GCP project's compute region gcloud config set compute/region $REGION # Verify the VPC and subnet exist vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +# Exit with an error message if the VPC or subnet does not exist if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then echo "Error: VPC '$VPC_NAME' and/or subnet '$SUBNET_NAME' do not exist in region '$REGION'." exit 1 fi +# Create a firewall rule to allow TCP port 8080 for all instances in the VPC gcloud compute firewall-rules create allow-tcp-8080 \ --network $VPC_NAME \ --allow tcp:8080 \ @@ -48,11 +54,16 @@ gcloud compute routers nats create $NAT_GATEWAY_NAME \ STARTUP_SCRIPT=$(cat <<'EOF' #!/bin/bash +# Update and upgrade the system apt update apt upgrade + +# Install Python 3 pip, Langflow, and Nginx apt install python3-pip pip install langflow apt-get install nginx + +# Configure Nginx for Langflow touch /etc/nginx/sites-available/langflow-app echo "server { listen 0.0.0.0:7860; @@ -75,6 +86,7 @@ EOF tempfile=$(mktemp) echo "$STARTUP_SCRIPT" > $tempfile +# Create the VM instance with the specified configuration and startup script gcloud compute instances create $VM_NAME \ --image-family $IMAGE_FAMILY \ --image-project $IMAGE_PROJECT \ @@ -88,6 +100,9 @@ gcloud compute instances create $VM_NAME \ rm $tempfile ``` +> This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment. The VM will be accessible on TCP port 8080 from any IP address. + +
## Connecting to your new Langflow VM 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page From 9274cb9ae0daa7f1e6e771af6e7e6aa6f1ee0b02 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 04:29:42 +0000 Subject: [PATCH 10/43] check and create VPC and subnet --- GCP-SETUP.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index 1f4eb6b4e..de0ec836d 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -14,20 +14,23 @@ ZONE="us-central1-a" REGION="us-central1" VPC_NAME="default" SUBNET_NAME="default" +SUBNET_RANGE="10.128.0.0/20" NAT_GATEWAY_NAME="nat-gateway" CLOUD_ROUTER_NAME="nat-client" # Set the GCP project's compute region gcloud config set compute/region $REGION -# Verify the VPC and subnet exist +# Check if the VPC exists, and create it if not vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") -subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$vpc_exists" ]]; then + gcloud compute networks create $VPC_NAME --subnet-mode=custom +fi -# Exit with an error message if the VPC or subnet does not exist -if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then - echo "Error: VPC '$VPC_NAME' and/or subnet '$SUBNET_NAME' do not exist in region '$REGION'." - exit 1 +# Check if the subnet exists, and create it if not +subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$subnet_exists" ]]; then + gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE fi # Create a firewall rule to allow TCP port 8080 for all instances in the VPC From b946edb3d12931bd9c43c659d73f34678bc51772 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 04:32:41 +0000 Subject: [PATCH 11/43] Allow IAP --- GCP-SETUP.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index de0ec836d..9261b8219 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -40,6 +40,12 @@ gcloud compute firewall-rules create allow-tcp-8080 \ --source-ranges 0.0.0.0/0 \ --direction INGRESS +# Create a firewall rule to allow IAP traffic +gcloud compute firewall-rules create allow-iap \ + --network $VPC_NAME \ + --allow tcp:80,tcp:443 \ + --source-ranges 35.235.240.0/20 \ + --direction INGRESS # Create the Cloud Router and NAT Gateway gcloud compute routers create $CLOUD_ROUTER_NAME \ From 5a51306153a90e4cce6245c0f63560798d946dde Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 17:31:50 +0000 Subject: [PATCH 12/43] adding machine type to VM create --- GCP-SETUP.md | 1 + 1 file changed, 1 insertion(+) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index 9261b8219..3a4750bc5 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -100,6 +100,7 @@ gcloud compute instances create $VM_NAME \ --image-family $IMAGE_FAMILY \ --image-project $IMAGE_PROJECT \ --boot-disk-size $BOOT_DISK_SIZE \ + --machine-type=n1-standard-4 \ --metadata-from-file startup-script=$tempfile \ --zone $ZONE \ --network $VPC_NAME \ From 19193d21e076e8290400713d34c963a7d2374ffc Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Wed, 19 Apr 2023 16:44:55 +0000 Subject: [PATCH 13/43] fix apt confirms, heredoc vars, add if statements --- GCP-SETUP.md | 53 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index 3a4750bc5..3a0908ff2 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -34,57 +34,54 @@ if [[ -z "$subnet_exists" ]]; then fi # Create a firewall rule to allow TCP port 8080 for all instances in the VPC -gcloud compute firewall-rules create allow-tcp-8080 \ - --network $VPC_NAME \ - --allow tcp:8080 \ - --source-ranges 0.0.0.0/0 \ - --direction INGRESS +firewall_8080_exists=$(gcloud compute firewall-rules list --filter="name=allow-tcp-8080" --format="value(name)") +if [[ -z "$firewall_8080_exists" ]]; then + gcloud compute firewall-rules create allow-tcp-8080 --network $VPC_NAME --allow tcp:8080 --source-ranges 0.0.0.0/0 --direction INGRESS +fi # Create a firewall rule to allow IAP traffic -gcloud compute firewall-rules create allow-iap \ - --network $VPC_NAME \ - --allow tcp:80,tcp:443 \ - --source-ranges 35.235.240.0/20 \ - --direction INGRESS +firewall_iap_exists=$(gcloud compute firewall-rules list --filter="name=allow-iap" --format="value(name)") +if [[ -z "$firewall_iap_exists" ]]; then + gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS +fi # Create the Cloud Router and NAT Gateway -gcloud compute routers create $CLOUD_ROUTER_NAME \ - --network $VPC_NAME \ - --region $REGION +cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") +if [[ -z "$cloud_router_exists" ]]; then + gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION +fi -gcloud compute routers nats create $NAT_GATEWAY_NAME \ - --router $CLOUD_ROUTER_NAME \ - --auto-allocate-nat-external-ips \ - --nat-all-subnet-ip-ranges \ - --enable-logging \ - --region $REGION +nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") +if [[ -z "$nat_exists" ]]; then + gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION +fi # Define the startup script as a multiline Bash here-doc STARTUP_SCRIPT=$(cat <<'EOF' #!/bin/bash # Update and upgrade the system -apt update -apt upgrade +apt -y update +apt -y upgrade # Install Python 3 pip, Langflow, and Nginx -apt install python3-pip +apt -y install python3-pip pip install langflow -apt-get install nginx +apt-get -y install nginx # Configure Nginx for Langflow touch /etc/nginx/sites-available/langflow-app echo "server { - listen 0.0.0.0:7860; + listen 0.0.0.0:8080; location / { proxy_pass http://127.0.0.1:7860; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host "\$host"; + proxy_set_header X-Real-IP "\$remote_addr"; + proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for"; } }" >> /etc/nginx/sites-available/langflow-app -ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/ +ln -s /etc/nginx/sites-available/langflow-app /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx langflow From ff3817903f752e09cacf151af71e17280379cc95 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Wed, 19 Apr 2023 22:59:56 +0000 Subject: [PATCH 14/43] create setup files and GCP tutorial --- GCP-SETUP.md => GCP_DEPLOYMENT.md | 4 +++- scripts/gcp_setup.sh | 0 scripts/gcp_setup_tutorial.yaml | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) rename GCP-SETUP.md => GCP_DEPLOYMENT.md (93%) create mode 100644 scripts/gcp_setup.sh create mode 100644 scripts/gcp_setup_tutorial.yaml diff --git a/GCP-SETUP.md b/GCP_DEPLOYMENT.md similarity index 93% rename from GCP-SETUP.md rename to GCP_DEPLOYMENT.md index 3a0908ff2..015d02d6f 100644 --- a/GCP-SETUP.md +++ b/GCP_DEPLOYMENT.md @@ -1,6 +1,8 @@ -# Running Langflow from a new GCP project +# Running Langflow from a new Google Cloud project This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=gcp_setup_tutorial.yaml) + ## Run the following in your GCP cloudshell: diff --git a/scripts/gcp_setup.sh b/scripts/gcp_setup.sh new file mode 100644 index 000000000..e69de29bb diff --git a/scripts/gcp_setup_tutorial.yaml b/scripts/gcp_setup_tutorial.yaml new file mode 100644 index 000000000..b45c56c30 --- /dev/null +++ b/scripts/gcp_setup_tutorial.yaml @@ -0,0 +1,10 @@ +title: Setting up Langflow on GCP +description: This tutorial guides you through setting up Langflow on GCP +steps: +- title: Running setup script + content: | + Running the setup script to create resources and deploy Langflow on GCP. + + ```bash + source gcp_setup.sh + ``` From 50b24f94437bff07e080172031d7070fde2cc33b Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:21:21 +0000 Subject: [PATCH 15/43] configure cloudshell to run walkthrough --- GCP_DEPLOYMENT.md | 2 +- README.md | 10 +++- scripts/gcp_setup.sh | 100 ++++++++++++++++++++++++++++++++ scripts/gcp_setup_tutorial.yaml | 86 ++++++++++++++++++++++++--- scripts/walkthroughtutorial.md | 78 +++++++++++++++++++++++++ 5 files changed, 266 insertions(+), 10 deletions(-) create mode 100644 scripts/walkthroughtutorial.md diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md index 015d02d6f..3cf10df27 100644 --- a/GCP_DEPLOYMENT.md +++ b/GCP_DEPLOYMENT.md @@ -1,7 +1,7 @@ # Running Langflow from a new Google Cloud project This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. -[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=gcp_setup_tutorial.yaml) +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md) ## Run the following in your GCP cloudshell: diff --git a/README.md b/README.md index 970496349..eaaf84331 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ LangFlow is a GUI for [LangChain](https://github.com/hwchase17/langchain), designed with [react-flow](https://github.com/wbkd/react-flow) to provide an effortless way to experiment and prototype flows with drag-and-drop components and a chat box. ## 📦 Installation - +### Locally You can install LangFlow from pip: `pip install langflow` @@ -28,6 +28,14 @@ Next, run: `langflow` +### Deploy Langflow on Google Cloud Platform + +Follow our step-by-step guide to deploy Langflow on Google Cloud Platform (GCP) using Google Cloud Shell. The guide is available in the [Langflow in Google Cloud Platform](GCP_DEPLOYMENT.md) document. + +Alternatively, click the "Open in Cloud Shell" button below to launch Google Cloud Shell, clone the Langflow repository, and start an interactive tutorial that will guide you through the process of setting up the necessary resources and deploying Langflow on your GCP project. + +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=GCP_SETUP_TUTORIAL.md) + ## 🎨 Creating Flows Creating flows with LangFlow is easy. Simply drag sidebar components onto the canvas and connect them together to create your pipeline. LangFlow provides a range of [LangChain components](https://langchain.readthedocs.io/en/latest/reference.html) to choose from, including LLMs, prompt serializers, agents, and chains. diff --git a/scripts/gcp_setup.sh b/scripts/gcp_setup.sh index e69de29bb..3e20e9589 100644 --- a/scripts/gcp_setup.sh +++ b/scripts/gcp_setup.sh @@ -0,0 +1,100 @@ +# Set the VM, image, and networking configuration +VM_NAME="langflow-dev" +IMAGE_FAMILY="debian-11" +IMAGE_PROJECT="debian-cloud" +BOOT_DISK_SIZE="100GB" +ZONE="us-central1-a" +REGION="us-central1" +VPC_NAME="default" +SUBNET_NAME="default" +SUBNET_RANGE="10.128.0.0/20" +NAT_GATEWAY_NAME="nat-gateway" +CLOUD_ROUTER_NAME="nat-client" + +# Set the GCP project's compute region +gcloud config set compute/region $REGION + +# Check if the VPC exists, and create it if not +vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") +if [[ -z "$vpc_exists" ]]; then + gcloud compute networks create $VPC_NAME --subnet-mode=custom +fi + +# Check if the subnet exists, and create it if not +subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$subnet_exists" ]]; then + gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE +fi + +# Create a firewall rule to allow TCP port 8080 for all instances in the VPC +firewall_8080_exists=$(gcloud compute firewall-rules list --filter="name=allow-tcp-8080" --format="value(name)") +if [[ -z "$firewall_8080_exists" ]]; then + gcloud compute firewall-rules create allow-tcp-8080 --network $VPC_NAME --allow tcp:8080 --source-ranges 0.0.0.0/0 --direction INGRESS +fi + +# Create a firewall rule to allow IAP traffic +firewall_iap_exists=$(gcloud compute firewall-rules list --filter="name=allow-iap" --format="value(name)") +if [[ -z "$firewall_iap_exists" ]]; then + gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS +fi + +# Create the Cloud Router and NAT Gateway +cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") +if [[ -z "$cloud_router_exists" ]]; then + gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION +fi + +nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") +if [[ -z "$nat_exists" ]]; then + gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION +fi + +# Define the startup script as a multiline Bash here-doc +STARTUP_SCRIPT=$(cat <<'EOF' +#!/bin/bash + +# Update and upgrade the system +apt -y update +apt -y upgrade + +# Install Python 3 pip, Langflow, and Nginx +apt -y install python3-pip +pip install langflow +apt-get -y install nginx + +# Configure Nginx for Langflow +touch /etc/nginx/sites-available/langflow-app +echo "server { + listen 0.0.0.0:8080; + + location / { + proxy_pass http://127.0.0.1:7860; + proxy_set_header Host "\$host"; + proxy_set_header X-Real-IP "\$remote_addr"; + proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for"; + } +}" >> /etc/nginx/sites-available/langflow-app +ln -s /etc/nginx/sites-available/langflow-app /etc/nginx/sites-enabled/ +sudo nginx -t +sudo systemctl restart nginx +langflow +EOF +) + +# Create a temporary file to store the startup script +tempfile=$(mktemp) +echo "$STARTUP_SCRIPT" > $tempfile + +# Create the VM instance with the specified configuration and startup script +gcloud compute instances create $VM_NAME \ + --image-family $IMAGE_FAMILY \ + --image-project $IMAGE_PROJECT \ + --boot-disk-size $BOOT_DISK_SIZE \ + --machine-type=n1-standard-4 \ + --metadata-from-file startup-script=$tempfile \ + --zone $ZONE \ + --network $VPC_NAME \ + --subnet $SUBNET_NAME + +# Remove the temporary file after the VM is created +rm $tempfile diff --git a/scripts/gcp_setup_tutorial.yaml b/scripts/gcp_setup_tutorial.yaml index b45c56c30..f8c5b401c 100644 --- a/scripts/gcp_setup_tutorial.yaml +++ b/scripts/gcp_setup_tutorial.yaml @@ -1,10 +1,80 @@ -title: Setting up Langflow on GCP -description: This tutorial guides you through setting up Langflow on GCP -steps: -- title: Running setup script - content: | - Running the setup script to create resources and deploy Langflow on GCP. +title: Deploy Langflow on Google Cloud Platform +duration: 45m +author: Your Name +environment: + cwd: working_dir + repo: + url: https://github.com/genome21/langflow + working_dir: scripts + +steps: +- title: Introduction + content: | + In this tutorial, you will learn how to deploy Langflow on Google Cloud Platform (GCP) using Google Cloud Shell. + + This tutorial assumes you have a GCP account and basic knowledge of Google Cloud Shell. If you're not familiar with Cloud Shell, you can review the [Cloud Shell documentation](https://cloud.google.com/shell/docs). + +- title: Set up your environment + content: | + Before you start, make sure you have the following prerequisites: + + - A GCP account with the necessary permissions to create resources + - A project on GCP where you want to deploy Langflow + + + + In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory. + +- title: Clone the repository and navigate to the scripts directory + content: | + Run the following commands to clone the Langflow repository and navigate to the `scripts` directory: - ```bash - source gcp_setup.sh ``` + git clone https://github.com/genome21/langflow + cd langflow/scripts + ``` + + In the next step, you'll configure the GCP environment and deploy Langflow. + +- title: Configure the GCP environment and deploy Langflow + content: | + Run the `deploy_langflow_gcp.sh` script to configure the GCP environment and deploy Langflow: + + ``` + ./deploy_langflow_gcp.sh + ``` + + The script will: + + 1. Check if the required resources (VPC, subnet, firewall rules, and Cloud Router) exist and create them if needed + 2. Create a startup script to install Python, Langflow, and Nginx + 3. Create a Compute Engine VM instance with the specified configuration and startup script + 4. Configure Nginx to serve Langflow on TCP port 8080 + + In the next step, you'll learn how to connect to the Langflow VM. + +- title: Connect to the Langflow VM + content: | + To connect to your new Langflow VM, follow these steps: + + 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page + 2. Click on the external IP for your VM + 3. Add port 8080 (assuming your VM external IP is 192.168.0.1): + http://192.168.0.1:8080 + 4. You will be greeted by the Langflow Dev environment + + Congratulations! You have successfully deployed Langflow on Google Cloud Platform. + +- title: Cleanup + content: | + If you want to remove the resources created during this tutorial, you can use the following commands: + + ``` + gcloud compute instances delete langflow-dev --zone us-central1-a --quiet + gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet + gcloud compute routers delete nat-client --region us-central1 --quiet + gcloud compute firewall-rules delete allow-tcp-8080 --quiet + gcloud compute firewall-rules delete allow-iap --quiet + gcloud compute networks subnets delete default --region us-central1 --quiet + gcloud compute networks delete default --quiet + `` diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md new file mode 100644 index 000000000..ebc9a4b95 --- /dev/null +++ b/scripts/walkthroughtutorial.md @@ -0,0 +1,78 @@ +# Deploy Langflow on Google Cloud Platform + +**Duration**: 45 minutes +**Author**: [Robert Wilkins III](https://www.linkedin.com/in/robertwilkinsiii) + +## Introduction + +In this tutorial, you will learn how to deploy Langflow on [Google Cloud Platform](https://cloud.google.com/) (GCP) using Google Cloud Shell. + +This tutorial assumes you have a GCP account and basic knowledge of Google Cloud Shell. If you're not familiar with Cloud Shell, you can review the [Cloud Shell documentation](https://cloud.google.com/shell/docs). + +## Set up your environment + +Before you start, make sure you have the following prerequisites: + +- A GCP account with the necessary permissions to create resources +- A project on GCP where you want to deploy Langflow + +[**Select your GCP project**] + +In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory. + +## Clone the repository and navigate to the scripts directory + +Run the following commands to clone the Langflow repository and navigate to the `scripts` directory: + +```bash +git clone https://github.com/genome21/langflow +cd langflow/scripts +``` + +In the next step, you'll configure the GCP environment and deploy Langflow. + +## Configure the GCP environment and deploy Langflow +Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: + +```bash +./deploy_langflow_gcp.sh +``` + +The script will: + +1. Check if the required resources (VPC, subnet, firewall rules, and Cloud Router) exist and create them if needed +2. Create a startup script to install Python, Langflow, and Nginx +3. Create a Compute Engine VM instance with the specified configuration and startup script +4. Configure Nginx to serve Langflow on TCP port 8080 + +In the next step, you'll learn how to connect to the Langflow VM. + +## Connect to the Langflow VM +To connect to your new Langflow VM, follow these steps: + +1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page +2. Click on the external IP for your VM +3. Add port 8080 (assuming your VM external IP is 192.168.0.1): +http://192.168.0.1:8080 +4. You will be greeted by the Langflow Dev environment + +Congratulations! You have successfully deployed Langflow on Google Cloud Platform. + +## Cleanup +If you want to remove the resources created during this tutorial, you can use the following commands: + +```sql +gcloud compute instances delete langflow-dev --zone us-central1-a --quiet + +gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet + +gcloud compute routers delete nat-client --region us-central1 --quiet + +gcloud compute firewall-rules delete allow-tcp-8080 --quiet + +gcloud compute firewall-rules delete allow-iap --quiet + +gcloud compute networks subnets delete default --region us-central1 --quiet + +gcloud compute networks delete default --quiet +``` From aeabaa32cd3fbd84d9d48f2ef876edfa34f5b2eb Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:30:44 +0000 Subject: [PATCH 16/43] remove scripted steps from walkthrough --- scripts/walkthroughtutorial.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index ebc9a4b95..641cce1e8 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -18,16 +18,16 @@ Before you start, make sure you have the following prerequisites: [**Select your GCP project**] -In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory. +[comment]: <> (In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory.) -## Clone the repository and navigate to the scripts directory +[comment]: <> (## Clone the repository and navigate to the scripts directory) -Run the following commands to clone the Langflow repository and navigate to the `scripts` directory: +[comment]: <> (Run the following commands to clone the Langflow repository and navigate to the `scripts` directory:) -```bash -git clone https://github.com/genome21/langflow -cd langflow/scripts -``` +[comment]: <> (```bash) +[comment]: <> (git clone https://github.com/genome21/langflow) +[comment]: <> (cd langflow/scripts) +[comment]: <> (```) In the next step, you'll configure the GCP environment and deploy Langflow. From e5aece49bfdfa1f13bb907dc11884ddf5f0e6a34 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:32:45 +0000 Subject: [PATCH 17/43] remove comments that show up in walkthrough --- scripts/walkthroughtutorial.md | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 641cce1e8..d21dbd31a 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -18,17 +18,6 @@ Before you start, make sure you have the following prerequisites: [**Select your GCP project**] -[comment]: <> (In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory.) - -[comment]: <> (## Clone the repository and navigate to the scripts directory) - -[comment]: <> (Run the following commands to clone the Langflow repository and navigate to the `scripts` directory:) - -[comment]: <> (```bash) -[comment]: <> (git clone https://github.com/genome21/langflow) -[comment]: <> (cd langflow/scripts) -[comment]: <> (```) - In the next step, you'll configure the GCP environment and deploy Langflow. ## Configure the GCP environment and deploy Langflow From bfdbabed55ab2ce1ad570d6c95553890c63df9bb Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:35:20 +0000 Subject: [PATCH 18/43] fix bash command launch --- scripts/walkthroughtutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index d21dbd31a..153a5a0c2 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -24,7 +24,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```bash -./deploy_langflow_gcp.sh +bash ./deploy_langflow_gcp.sh ``` The script will: From 9a06ff54c7eb9e7be43af3fcff66f7e753531a2e Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:37:52 +0000 Subject: [PATCH 19/43] match bash script to walkthrough --- scripts/{gcp_setup.sh => deploy_langflow_gcp.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scripts/{gcp_setup.sh => deploy_langflow_gcp.sh} (100%) diff --git a/scripts/gcp_setup.sh b/scripts/deploy_langflow_gcp.sh similarity index 100% rename from scripts/gcp_setup.sh rename to scripts/deploy_langflow_gcp.sh From 08a1739fa775b6b79ddd921033d9b5e8c51e836f Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:55:34 +0000 Subject: [PATCH 20/43] force gcp login in ephemeral cloudshell --- scripts/walkthroughtutorial.md | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 153a5a0c2..99599010f 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -24,6 +24,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```bash +gcloud auth login --brief --quiet bash ./deploy_langflow_gcp.sh ``` From 9c9b69db596d05d0a7aab63b1d127000783171a1 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:05:34 +0000 Subject: [PATCH 21/43] add cloudshell repo trust advisory --- GCP_DEPLOYMENT.md | 1 + 1 file changed, 1 insertion(+) diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md index 3cf10df27..6bbd54097 100644 --- a/GCP_DEPLOYMENT.md +++ b/GCP_DEPLOYMENT.md @@ -1,5 +1,6 @@ # Running Langflow from a new Google Cloud project This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. +> When cloudshell opens, select **Trust this repo**. Some gcloud commands do not run in ephemeral cloudshell. [![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md) From 98be8f87e4ef22c534483ac84f2210047afa9f5e Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:07:10 +0000 Subject: [PATCH 22/43] remove login prompt for ephemeral cloudshell --- scripts/walkthroughtutorial.md | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 99599010f..153a5a0c2 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -24,7 +24,6 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```bash -gcloud auth login --brief --quiet bash ./deploy_langflow_gcp.sh ``` From 6efa1b737741f8a6983c38617bb1a59f97ae7779 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:10:45 +0000 Subject: [PATCH 23/43] set project config --- scripts/walkthroughtutorial.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 153a5a0c2..f908bce3b 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -23,7 +23,8 @@ In the next step, you'll configure the GCP environment and deploy Langflow. ## Configure the GCP environment and deploy Langflow Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: -```bash +```sh +gcloud config set project {{project-id}} bash ./deploy_langflow_gcp.sh ``` From 7ef97e13347bab8315aee08f69e109f1ffb36d8c Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:18:27 +0000 Subject: [PATCH 24/43] testing var expansion --- scripts/walkthroughtutorial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index f908bce3b..d30c3b702 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -16,7 +16,7 @@ Before you start, make sure you have the following prerequisites: - A GCP account with the necessary permissions to create resources - A project on GCP where you want to deploy Langflow -[**Select your GCP project**] +[**Select your GCP project**] In the next step, you'll configure the GCP environment and deploy Langflow. @@ -24,7 +24,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```sh -gcloud config set project {{project-id}} +gcloud config set project {{project_id}} bash ./deploy_langflow_gcp.sh ``` From 90633c3915793cc34b699a0a5fea2f6cb2a5d556 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:19:36 +0000 Subject: [PATCH 25/43] revert var expansion test --- scripts/walkthroughtutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index d30c3b702..3b7e3a76e 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -24,7 +24,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```sh -gcloud config set project {{project_id}} +gcloud config set project {{project-id}} bash ./deploy_langflow_gcp.sh ``` From c4ebb0a3f68faba6a951d40f872a68f9b8355a33 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:23:34 +0000 Subject: [PATCH 26/43] var expansion again --- scripts/walkthroughtutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 3b7e3a76e..d30c3b702 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -24,7 +24,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```sh -gcloud config set project {{project-id}} +gcloud config set project {{project_id}} bash ./deploy_langflow_gcp.sh ``` From b54b0f0904f46cc8278512909a6c88200b26a14c Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:33:47 +0000 Subject: [PATCH 27/43] var expansion alt test --- scripts/walkthroughtutorial.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index d30c3b702..66efe5e74 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -16,7 +16,11 @@ Before you start, make sure you have the following prerequisites: - A GCP account with the necessary permissions to create resources - A project on GCP where you want to deploy Langflow -[**Select your GCP project**] +[**Select your GCP project**] + + In the next step, you'll configure the GCP environment and deploy Langflow. @@ -24,7 +28,7 @@ In the next step, you'll configure the GCP environment and deploy Langflow. Run the deploy_langflow_gcp.sh script to configure the GCP environment and deploy Langflow: ```sh -gcloud config set project {{project_id}} +gcloud config set project bash ./deploy_langflow_gcp.sh ``` From d310a8172f6a2a9fc167ee76e5ac8c1552141f53 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 02:57:37 +0000 Subject: [PATCH 28/43] provide user with link to langflow server --- scripts/walkthroughtutorial.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 66efe5e74..0f65dbadf 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -46,8 +46,16 @@ To connect to your new Langflow VM, follow these steps: 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page 2. Click on the external IP for your VM -3. Add port 8080 (assuming your VM external IP is 192.168.0.1): -http://192.168.0.1:8080 +
or +3. Run the following command to store the VM's IP in a variable called `LANGFLOW-IP`: +```bash +export LANGFLOW-IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)") + +echo http://$LANGFLOW-IP:8080 +``` + + + 4. You will be greeted by the Langflow Dev environment Congratulations! You have successfully deployed Langflow on Google Cloud Platform. From f46e27d1f0d987d99499bbb0671bac06e2384014 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:08:50 +0000 Subject: [PATCH 29/43] vars and trophies --- scripts/walkthroughtutorial.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 0f65dbadf..5398617c3 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -42,6 +42,7 @@ The script will: In the next step, you'll learn how to connect to the Langflow VM. ## Connect to the Langflow VM + To connect to your new Langflow VM, follow these steps: 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page @@ -54,7 +55,7 @@ export LANGFLOW-IP=$(gcloud compute instances list --filter="NAME=langflow-dev" echo http://$LANGFLOW-IP:8080 ``` - + 4. You will be greeted by the Langflow Dev environment From b9a0e55d43355600f7993ccf0f45220bc2ef0ee3 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:11:30 +0000 Subject: [PATCH 30/43] remove input tag --- scripts/walkthroughtutorial.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 5398617c3..2711ccd93 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -50,13 +50,11 @@ To connect to your new Langflow VM, follow these steps:
or 3. Run the following command to store the VM's IP in a variable called `LANGFLOW-IP`: ```bash -export LANGFLOW-IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)") +export LANGFLOW_IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)") -echo http://$LANGFLOW-IP:8080 +echo http://$LANGFLOW_IP:8080 ``` - - 4. You will be greeted by the Langflow Dev environment Congratulations! You have successfully deployed Langflow on Google Cloud Platform. From 37bdafa9db6bf1717280f38a4aaef8b5b89d1459 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:17:40 +0000 Subject: [PATCH 31/43] clarify notes to connect to vm --- scripts/walkthroughtutorial.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 2711ccd93..dd107a770 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -45,17 +45,16 @@ In the next step, you'll learn how to connect to the Langflow VM. To connect to your new Langflow VM, follow these steps: -1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page -2. Click on the external IP for your VM -
or -3. Run the following command to store the VM's IP in a variable called `LANGFLOW-IP`: +1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page and click on the external IP for your VM. Make sure to use HTTP and set the port to 8080 +
**or** +3. Run the following command to display the URL for your Langflow environment: ```bash export LANGFLOW_IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)") echo http://$LANGFLOW_IP:8080 ``` -4. You will be greeted by the Langflow Dev environment +4. Click on the Langflow URL in cloudshell to be greeted by the Langflow Dev environment Congratulations! You have successfully deployed Langflow on Google Cloud Platform. From 5a7072175f2b63491ea1fbafcc23243c03780aeb Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:20:09 +0000 Subject: [PATCH 32/43] move trophy toward end of walkthrough --- scripts/walkthroughtutorial.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index dd107a770..7742f3453 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -42,7 +42,6 @@ The script will: In the next step, you'll learn how to connect to the Langflow VM. ## Connect to the Langflow VM - To connect to your new Langflow VM, follow these steps: 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page and click on the external IP for your VM. Make sure to use HTTP and set the port to 8080 @@ -58,6 +57,8 @@ echo http://$LANGFLOW_IP:8080 Congratulations! You have successfully deployed Langflow on Google Cloud Platform. + + ## Cleanup If you want to remove the resources created during this tutorial, you can use the following commands: From 57d83740f1a8bd5f488af36d28e9419568fb4c08 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:29:03 +0000 Subject: [PATCH 33/43] improve readability --- scripts/walkthroughtutorial.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 7742f3453..3d9401f2e 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -69,6 +69,13 @@ gcloud compute routers nats delete nat-gateway --router nat-client --region us-c gcloud compute routers delete nat-client --region us-central1 --quiet +``` +The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. + +However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands: +> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. + +``` gcloud compute firewall-rules delete allow-tcp-8080 --quiet gcloud compute firewall-rules delete allow-iap --quiet From 56e4ae1fcd4410e07abc2a86692f0db184e8d416 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:35:10 +0000 Subject: [PATCH 34/43] add styling --- scripts/walkthroughtutorial.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 3d9401f2e..3731533c1 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -39,6 +39,8 @@ The script will: 3. Create a Compute Engine VM instance with the specified configuration and startup script 4. Configure Nginx to serve Langflow on TCP port 8080 +> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. + In the next step, you'll learn how to connect to the Langflow VM. ## Connect to the Langflow VM From b6ae41eef079ce7114e6e87503b99236bbfc2712 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:36:27 +0000 Subject: [PATCH 35/43] more styling --- scripts/walkthroughtutorial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 3731533c1..b53d3c6d2 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -39,7 +39,7 @@ The script will: 3. Create a Compute Engine VM instance with the specified configuration and startup script 4. Configure Nginx to serve Langflow on TCP port 8080 -> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. +> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. In the next step, you'll learn how to connect to the Langflow VM. @@ -75,7 +75,7 @@ gcloud compute routers delete nat-client --region us-central1 --quiet The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands: -> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. +> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. ``` gcloud compute firewall-rules delete allow-tcp-8080 --quiet From 6a59d64a0d44b3f56d6c00873f5281642b8ac3c0 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:54:58 +0000 Subject: [PATCH 36/43] remove redundant wording --- GCP_DEPLOYMENT.md | 119 +------------------------------- scripts/gcp_setup_tutorial.yaml | 80 --------------------- 2 files changed, 2 insertions(+), 197 deletions(-) delete mode 100644 scripts/gcp_setup_tutorial.yaml diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md index 6bbd54097..01686e4ed 100644 --- a/GCP_DEPLOYMENT.md +++ b/GCP_DEPLOYMENT.md @@ -1,122 +1,7 @@ # Running Langflow from a new Google Cloud project This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. -> When cloudshell opens, select **Trust this repo**. Some gcloud commands do not run in ephemeral cloudshell. +> When cloudshell opens, select **Trust repo**. Some gcloud commands do not run in ephemeral cloudshell. [![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md) - -## Run the following in your GCP cloudshell: - -```bash -# Set the VM, image, and networking configuration -VM_NAME="langflow-dev" -IMAGE_FAMILY="debian-11" -IMAGE_PROJECT="debian-cloud" -BOOT_DISK_SIZE="100GB" -ZONE="us-central1-a" -REGION="us-central1" -VPC_NAME="default" -SUBNET_NAME="default" -SUBNET_RANGE="10.128.0.0/20" -NAT_GATEWAY_NAME="nat-gateway" -CLOUD_ROUTER_NAME="nat-client" - -# Set the GCP project's compute region -gcloud config set compute/region $REGION - -# Check if the VPC exists, and create it if not -vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") -if [[ -z "$vpc_exists" ]]; then - gcloud compute networks create $VPC_NAME --subnet-mode=custom -fi - -# Check if the subnet exists, and create it if not -subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") -if [[ -z "$subnet_exists" ]]; then - gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE -fi - -# Create a firewall rule to allow TCP port 8080 for all instances in the VPC -firewall_8080_exists=$(gcloud compute firewall-rules list --filter="name=allow-tcp-8080" --format="value(name)") -if [[ -z "$firewall_8080_exists" ]]; then - gcloud compute firewall-rules create allow-tcp-8080 --network $VPC_NAME --allow tcp:8080 --source-ranges 0.0.0.0/0 --direction INGRESS -fi - -# Create a firewall rule to allow IAP traffic -firewall_iap_exists=$(gcloud compute firewall-rules list --filter="name=allow-iap" --format="value(name)") -if [[ -z "$firewall_iap_exists" ]]; then - gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS -fi - -# Create the Cloud Router and NAT Gateway -cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") -if [[ -z "$cloud_router_exists" ]]; then - gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION -fi - -nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") -if [[ -z "$nat_exists" ]]; then - gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION -fi - -# Define the startup script as a multiline Bash here-doc -STARTUP_SCRIPT=$(cat <<'EOF' -#!/bin/bash - -# Update and upgrade the system -apt -y update -apt -y upgrade - -# Install Python 3 pip, Langflow, and Nginx -apt -y install python3-pip -pip install langflow -apt-get -y install nginx - -# Configure Nginx for Langflow -touch /etc/nginx/sites-available/langflow-app -echo "server { - listen 0.0.0.0:8080; - - location / { - proxy_pass http://127.0.0.1:7860; - proxy_set_header Host "\$host"; - proxy_set_header X-Real-IP "\$remote_addr"; - proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for"; - } -}" >> /etc/nginx/sites-available/langflow-app -ln -s /etc/nginx/sites-available/langflow-app /etc/nginx/sites-enabled/ -sudo nginx -t -sudo systemctl restart nginx -langflow -EOF -) - -# Create a temporary file to store the startup script -tempfile=$(mktemp) -echo "$STARTUP_SCRIPT" > $tempfile - -# Create the VM instance with the specified configuration and startup script -gcloud compute instances create $VM_NAME \ - --image-family $IMAGE_FAMILY \ - --image-project $IMAGE_PROJECT \ - --boot-disk-size $BOOT_DISK_SIZE \ - --machine-type=n1-standard-4 \ - --metadata-from-file startup-script=$tempfile \ - --zone $ZONE \ - --network $VPC_NAME \ - --subnet $SUBNET_NAME - -# Remove the temporary file after the VM is created -rm $tempfile - -``` -> This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment. The VM will be accessible on TCP port 8080 from any IP address. - -
- -## Connecting to your new Langflow VM -1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page -2. Click on the external IP for your VM -3. Add port 8080 (assuming your VM external IP is 192.168.0.1): -http://192.168.0.1:8080 -4. You will be greeted by the Langflow Dev environment \ No newline at end of file +This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment. \ No newline at end of file diff --git a/scripts/gcp_setup_tutorial.yaml b/scripts/gcp_setup_tutorial.yaml deleted file mode 100644 index f8c5b401c..000000000 --- a/scripts/gcp_setup_tutorial.yaml +++ /dev/null @@ -1,80 +0,0 @@ -title: Deploy Langflow on Google Cloud Platform -duration: 45m -author: Your Name -environment: - cwd: working_dir - repo: - url: https://github.com/genome21/langflow - working_dir: scripts - -steps: -- title: Introduction - content: | - In this tutorial, you will learn how to deploy Langflow on Google Cloud Platform (GCP) using Google Cloud Shell. - - This tutorial assumes you have a GCP account and basic knowledge of Google Cloud Shell. If you're not familiar with Cloud Shell, you can review the [Cloud Shell documentation](https://cloud.google.com/shell/docs). - -- title: Set up your environment - content: | - Before you start, make sure you have the following prerequisites: - - - A GCP account with the necessary permissions to create resources - - A project on GCP where you want to deploy Langflow - - - - In the next step, you'll clone the Langflow repository and navigate to the `scripts` directory. - -- title: Clone the repository and navigate to the scripts directory - content: | - Run the following commands to clone the Langflow repository and navigate to the `scripts` directory: - - ``` - git clone https://github.com/genome21/langflow - cd langflow/scripts - ``` - - In the next step, you'll configure the GCP environment and deploy Langflow. - -- title: Configure the GCP environment and deploy Langflow - content: | - Run the `deploy_langflow_gcp.sh` script to configure the GCP environment and deploy Langflow: - - ``` - ./deploy_langflow_gcp.sh - ``` - - The script will: - - 1. Check if the required resources (VPC, subnet, firewall rules, and Cloud Router) exist and create them if needed - 2. Create a startup script to install Python, Langflow, and Nginx - 3. Create a Compute Engine VM instance with the specified configuration and startup script - 4. Configure Nginx to serve Langflow on TCP port 8080 - - In the next step, you'll learn how to connect to the Langflow VM. - -- title: Connect to the Langflow VM - content: | - To connect to your new Langflow VM, follow these steps: - - 1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page - 2. Click on the external IP for your VM - 3. Add port 8080 (assuming your VM external IP is 192.168.0.1): - http://192.168.0.1:8080 - 4. You will be greeted by the Langflow Dev environment - - Congratulations! You have successfully deployed Langflow on Google Cloud Platform. - -- title: Cleanup - content: | - If you want to remove the resources created during this tutorial, you can use the following commands: - - ``` - gcloud compute instances delete langflow-dev --zone us-central1-a --quiet - gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet - gcloud compute routers delete nat-client --region us-central1 --quiet - gcloud compute firewall-rules delete allow-tcp-8080 --quiet - gcloud compute firewall-rules delete allow-iap --quiet - gcloud compute networks subnets delete default --region us-central1 --quiet - gcloud compute networks delete default --quiet - `` From dada24a5e66ff72a7336ac2a34e7b86305922be4 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 04:43:26 +0000 Subject: [PATCH 37/43] add spot instance support and pricing --- GCP_DEPLOYMENT.md | 31 +++++++-- scripts/deploy_langflow_gcp_spot.sh | 101 ++++++++++++++++++++++++++++ scripts/walkthroughtutorial.md | 7 +- scripts/walkthroughtutorial_spot.md | 88 ++++++++++++++++++++++++ 4 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 scripts/deploy_langflow_gcp_spot.sh create mode 100644 scripts/walkthroughtutorial_spot.md diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md index 01686e4ed..0491dabb0 100644 --- a/GCP_DEPLOYMENT.md +++ b/GCP_DEPLOYMENT.md @@ -1,7 +1,30 @@ -# Running Langflow from a new Google Cloud project -This guide will help you set up a Langflow Dev VM in a Google Cloud Platform project using Google Cloud Shell. -> When cloudshell opens, select **Trust repo**. Some gcloud commands do not run in ephemeral cloudshell. +# Run Langflow from a New Google Cloud Project +This guide will help you set up a Langflow development VM in a Google Cloud Platform project using Google Cloud Shell. + +> **Note**: When Cloud Shell opens, be sure to select **Trust repo**. Some `gcloud` commands might not run in an ephemeral Cloud Shell environment. + + +## Standard VM [![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md) -This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment. \ No newline at end of file +This script sets up a Debian-based VM with the Langflow package, Nginx, and the necessary configurations to run the Langflow Dev environment. +
+ +## Spot/Preemptible Instance + +[![Open in Cloud Shell - Spot Instance](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial.md) + +When running as a [spot (preemptible) instance](https://cloud.google.com/compute/docs/instances/preemptible), the code and VM will behave the same way as in a regular instance, executing the startup script to configure the environment, install necessary dependencies, and run the Langflow application. However, **due to the nature of spot instances, the VM may be terminated at any time if Google Cloud needs to reclaim the resources**. This makes spot instances suitable for fault-tolerant, stateless, or interruptible workloads that can handle unexpected terminations and restarts. + +## Pricing +
+ +| Component | Regular Cost (Hourly) | Regular Cost (Monthly) | Spot/Preemptible Cost (Hourly) | Spot/Preemptible Cost (Monthly) | Notes | +| -------------- | --------------------- | ---------------------- | ------------------------------ | ------------------------------- | ----- | +| 100 GB Disk | - | $10/month | - | $10/month | Disk cost remains the same for both regular and Spot/Preemptible VMs | +| VM (n1-standard-4) | $0.15/hr | ~$108/month | ~$0.04/hr | ~$29/month | The VM cost can be significantly reduced using a Spot/Preemptible instance | +| **Total** | **$0.15/hr** | **~$118/month** | **~$0.04/hr** | **~$39/month** | Total costs for running the VM and disk 24/7 for an entire month | + +> For a more accurate breakdown of costs, please use the [**GCP Pricing Calculator**](https://cloud.google.com/products/calculator) + diff --git a/scripts/deploy_langflow_gcp_spot.sh b/scripts/deploy_langflow_gcp_spot.sh new file mode 100644 index 000000000..7ddc93b96 --- /dev/null +++ b/scripts/deploy_langflow_gcp_spot.sh @@ -0,0 +1,101 @@ +# Set the VM, image, and networking configuration +VM_NAME="langflow-dev" +IMAGE_FAMILY="debian-11" +IMAGE_PROJECT="debian-cloud" +BOOT_DISK_SIZE="100GB" +ZONE="us-central1-a" +REGION="us-central1" +VPC_NAME="default" +SUBNET_NAME="default" +SUBNET_RANGE="10.128.0.0/20" +NAT_GATEWAY_NAME="nat-gateway" +CLOUD_ROUTER_NAME="nat-client" + +# Set the GCP project's compute region +gcloud config set compute/region $REGION + +# Check if the VPC exists, and create it if not +vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") +if [[ -z "$vpc_exists" ]]; then + gcloud compute networks create $VPC_NAME --subnet-mode=custom +fi + +# Check if the subnet exists, and create it if not +subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$subnet_exists" ]]; then + gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE +fi + +# Create a firewall rule to allow TCP port 8080 for all instances in the VPC +firewall_8080_exists=$(gcloud compute firewall-rules list --filter="name=allow-tcp-8080" --format="value(name)") +if [[ -z "$firewall_8080_exists" ]]; then + gcloud compute firewall-rules create allow-tcp-8080 --network $VPC_NAME --allow tcp:8080 --source-ranges 0.0.0.0/0 --direction INGRESS +fi + +# Create a firewall rule to allow IAP traffic +firewall_iap_exists=$(gcloud compute firewall-rules list --filter="name=allow-iap" --format="value(name)") +if [[ -z "$firewall_iap_exists" ]]; then + gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS +fi + +# Create the Cloud Router and NAT Gateway +cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") +if [[ -z "$cloud_router_exists" ]]; then + gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION +fi + +nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") +if [[ -z "$nat_exists" ]]; then + gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION +fi + +# Define the startup script as a multiline Bash here-doc +STARTUP_SCRIPT=$(cat <<'EOF' +#!/bin/bash + +# Update and upgrade the system +apt -y update +apt -y upgrade + +# Install Python 3 pip, Langflow, and Nginx +apt -y install python3-pip +pip install langflow +apt-get -y install nginx + +# Configure Nginx for Langflow +touch /etc/nginx/sites-available/langflow-app +echo "server { + listen 0.0.0.0:8080; + + location / { + proxy_pass http://127.0.0.1:7860; + proxy_set_header Host "\$host"; + proxy_set_header X-Real-IP "\$remote_addr"; + proxy_set_header X-Forwarded-For "\$proxy_add_x_forwarded_for"; + } +}" >> /etc/nginx/sites-available/langflow-app +ln -s /etc/nginx/sites-available/langflow-app /etc/nginx/sites-enabled/ +sudo nginx -t +sudo systemctl restart nginx +langflow +EOF +) + +# Create a temporary file to store the startup script +tempfile=$(mktemp) +echo "$STARTUP_SCRIPT" > $tempfile + +# Create the VM instance with the specified configuration and startup script +gcloud compute instances create $VM_NAME \ + --image-family $IMAGE_FAMILY \ + --image-project $IMAGE_PROJECT \ + --boot-disk-size $BOOT_DISK_SIZE \ + --machine-type=n1-standard-4 \ + --metadata-from-file startup-script=$tempfile \ + --zone $ZONE \ + --network $VPC_NAME \ + --subnet $SUBNET_NAME \ + -preemptible + +# Remove the temporary file after the VM is created +rm $tempfile diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index b53d3c6d2..0d619090f 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -39,7 +39,8 @@ The script will: 3. Create a Compute Engine VM instance with the specified configuration and startup script 4. Configure Nginx to serve Langflow on TCP port 8080 -> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. + +> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. In the next step, you'll learn how to connect to the Langflow VM. @@ -75,7 +76,9 @@ gcloud compute routers delete nat-client --region us-central1 --quiet The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands: -> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. + + +> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. ``` gcloud compute firewall-rules delete allow-tcp-8080 --quiet diff --git a/scripts/walkthroughtutorial_spot.md b/scripts/walkthroughtutorial_spot.md new file mode 100644 index 000000000..168a67046 --- /dev/null +++ b/scripts/walkthroughtutorial_spot.md @@ -0,0 +1,88 @@ +# Deploy Langflow on Google Cloud Platform + +**Duration**: 45 minutes +**Author**: [Robert Wilkins III](https://www.linkedin.com/in/robertwilkinsiii) + +## Introduction + +In this tutorial, you will learn how to deploy Langflow on [Google Cloud Platform](https://cloud.google.com/) (GCP) using Google Cloud Shell. + +This tutorial assumes you have a GCP account and basic knowledge of Google Cloud Shell. If you're not familiar with Cloud Shell, you can review the [Cloud Shell documentation](https://cloud.google.com/shell/docs). + +## Set up your environment + +Before you start, make sure you have the following prerequisites: + +- A GCP account with the necessary permissions to create resources +- A project on GCP where you want to deploy Langflow + +[**Select your GCP project**] + + + +In the next step, you'll configure the GCP environment and deploy Langflow. + +## Configure the GCP environment and deploy Langflow +Run the deploy_langflow_gcp_spot.sh script to configure the GCP environment and deploy Langflow: + +```sh +gcloud config set project +bash ./deploy_langflow_gcp.sh +``` + +The script will: + +1. Check if the required resources (VPC, subnet, firewall rules, and Cloud Router) exist and create them if needed +2. Create a startup script to install Python, Langflow, and Nginx +3. Create a Compute Engine VM instance with the specified configuration and startup script +4. Configure Nginx to serve Langflow on TCP port 8080 + +> The process may take approximately 30 minutes to complete. Rest assured that progress is being made, and you'll be able to proceed once the process is finished. + +In the next step, you'll learn how to connect to the Langflow VM. + +## Connect to the Langflow VM +To connect to your new Langflow VM, follow these steps: + +1. Navigate to the [VM instances](https://console.cloud.google.com/compute/instances) page and click on the external IP for your VM. Make sure to use HTTP and set the port to 8080 +
**or** +3. Run the following command to display the URL for your Langflow environment: +```bash +export LANGFLOW_IP=$(gcloud compute instances list --filter="NAME=langflow-dev" --format="value(EXTERNAL_IP)") + +echo http://$LANGFLOW_IP:8080 +``` + +4. Click on the Langflow URL in cloudshell to be greeted by the Langflow Dev environment + +Congratulations! You have successfully deployed Langflow on Google Cloud Platform. + + + +## Cleanup +If you want to remove the resources created during this tutorial, you can use the following commands: + +```sql +gcloud compute instances delete langflow-dev --zone us-central1-a --quiet + +gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet + +gcloud compute routers delete nat-client --region us-central1 --quiet + +``` +The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. + +However, if you decide to remove them after completing the walkthrough, you can use the following gcloud commands: +> These commands will delete the firewall rules and network configurations created during the walkthrough. Make sure to run them only if you no longer need these settings. + +``` +gcloud compute firewall-rules delete allow-tcp-8080 --quiet + +gcloud compute firewall-rules delete allow-iap --quiet + +gcloud compute networks subnets delete default --region us-central1 --quiet + +gcloud compute networks delete default --quiet +``` From adc93c7902e54ae699e25862f066155399a2bd3c Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 05:10:38 +0000 Subject: [PATCH 38/43] improve grammer and remove nat --- README.md | 7 ++++--- scripts/deploy_langflow_gcp.sh | 11 ----------- scripts/deploy_langflow_gcp_spot.sh | 11 ----------- scripts/walkthroughtutorial.md | 5 ----- 4 files changed, 4 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index eaaf84331..3be5d99a9 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,12 @@ Next, run: ### Deploy Langflow on Google Cloud Platform -Follow our step-by-step guide to deploy Langflow on Google Cloud Platform (GCP) using Google Cloud Shell. The guide is available in the [Langflow in Google Cloud Platform](GCP_DEPLOYMENT.md) document. +Follow our step-by-step guide to deploy Langflow on Google Cloud Platform (GCP) using Google Cloud Shell. The guide is available in the [**Langflow in Google Cloud Platform**](GCP_DEPLOYMENT.md) document. -Alternatively, click the "Open in Cloud Shell" button below to launch Google Cloud Shell, clone the Langflow repository, and start an interactive tutorial that will guide you through the process of setting up the necessary resources and deploying Langflow on your GCP project. +Alternatively, click the **"Open in Cloud Shell"** button below to launch Google Cloud Shell, clone the Langflow repository, and start an **interactive tutorial** that will guide you through the process of setting up the necessary resources and deploying Langflow on your GCP project. + +[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=walkthroughtutorial_spot.md) -[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/genome21/langflow&working_dir=scripts&shellonly=true&tutorial=GCP_SETUP_TUTORIAL.md) ## 🎨 Creating Flows diff --git a/scripts/deploy_langflow_gcp.sh b/scripts/deploy_langflow_gcp.sh index 3e20e9589..2c3dc0420 100644 --- a/scripts/deploy_langflow_gcp.sh +++ b/scripts/deploy_langflow_gcp.sh @@ -38,17 +38,6 @@ if [[ -z "$firewall_iap_exists" ]]; then gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS fi -# Create the Cloud Router and NAT Gateway -cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") -if [[ -z "$cloud_router_exists" ]]; then - gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION -fi - -nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") -if [[ -z "$nat_exists" ]]; then - gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION -fi - # Define the startup script as a multiline Bash here-doc STARTUP_SCRIPT=$(cat <<'EOF' #!/bin/bash diff --git a/scripts/deploy_langflow_gcp_spot.sh b/scripts/deploy_langflow_gcp_spot.sh index 7ddc93b96..065b6013f 100644 --- a/scripts/deploy_langflow_gcp_spot.sh +++ b/scripts/deploy_langflow_gcp_spot.sh @@ -38,17 +38,6 @@ if [[ -z "$firewall_iap_exists" ]]; then gcloud compute firewall-rules create allow-iap --network $VPC_NAME --allow tcp:80,tcp:443 --source-ranges 35.235.240.0/20 --direction INGRESS fi -# Create the Cloud Router and NAT Gateway -cloud_router_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(name)") -if [[ -z "$cloud_router_exists" ]]; then - gcloud compute routers create $CLOUD_ROUTER_NAME --network $VPC_NAME --region $REGION -fi - -nat_exists=$(gcloud compute routers list --filter="name=$CLOUD_ROUTER_NAME" --format="value(nats.name)") -if [[ -z "$nat_exists" ]]; then - gcloud compute routers nats create $NAT_GATEWAY_NAME --router $CLOUD_ROUTER_NAME --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --enable-logging --region $REGION -fi - # Define the startup script as a multiline Bash here-doc STARTUP_SCRIPT=$(cat <<'EOF' #!/bin/bash diff --git a/scripts/walkthroughtutorial.md b/scripts/walkthroughtutorial.md index 0d619090f..fa6e3c11d 100644 --- a/scripts/walkthroughtutorial.md +++ b/scripts/walkthroughtutorial.md @@ -67,11 +67,6 @@ If you want to remove the resources created during this tutorial, you can use th ```sql gcloud compute instances delete langflow-dev --zone us-central1-a --quiet - -gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet - -gcloud compute routers delete nat-client --region us-central1 --quiet - ``` The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. From e90b036b011e27b32b100d2d0ac7787424ea3d4d Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 05:11:28 +0000 Subject: [PATCH 39/43] remove the nats --- scripts/walkthroughtutorial_spot.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/scripts/walkthroughtutorial_spot.md b/scripts/walkthroughtutorial_spot.md index 168a67046..751f03d78 100644 --- a/scripts/walkthroughtutorial_spot.md +++ b/scripts/walkthroughtutorial_spot.md @@ -66,11 +66,6 @@ If you want to remove the resources created during this tutorial, you can use th ```sql gcloud compute instances delete langflow-dev --zone us-central1-a --quiet - -gcloud compute routers nats delete nat-gateway --router nat-client --region us-central1 --quiet - -gcloud compute routers delete nat-client --region us-central1 --quiet - ``` The following network settings and services are used during this walkthrough. If you plan to continue using the project after the walkthrough, you may keep these configurations in place. From b9a9892db633e844a7142031592b43f8a98e721f Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Thu, 20 Apr 2023 05:14:37 +0000 Subject: [PATCH 40/43] layout changes --- GCP_DEPLOYMENT.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/GCP_DEPLOYMENT.md b/GCP_DEPLOYMENT.md index 0491dabb0..edb7e043c 100644 --- a/GCP_DEPLOYMENT.md +++ b/GCP_DEPLOYMENT.md @@ -17,7 +17,8 @@ This script sets up a Debian-based VM with the Langflow package, Nginx, and the When running as a [spot (preemptible) instance](https://cloud.google.com/compute/docs/instances/preemptible), the code and VM will behave the same way as in a regular instance, executing the startup script to configure the environment, install necessary dependencies, and run the Langflow application. However, **due to the nature of spot instances, the VM may be terminated at any time if Google Cloud needs to reclaim the resources**. This makes spot instances suitable for fault-tolerant, stateless, or interruptible workloads that can handle unexpected terminations and restarts. -## Pricing +## Pricing (approximate) +> For a more accurate breakdown of costs, please use the [**GCP Pricing Calculator**](https://cloud.google.com/products/calculator)
| Component | Regular Cost (Hourly) | Regular Cost (Monthly) | Spot/Preemptible Cost (Hourly) | Spot/Preemptible Cost (Monthly) | Notes | @@ -25,6 +26,3 @@ When running as a [spot (preemptible) instance](https://cloud.google.com/compute | 100 GB Disk | - | $10/month | - | $10/month | Disk cost remains the same for both regular and Spot/Preemptible VMs | | VM (n1-standard-4) | $0.15/hr | ~$108/month | ~$0.04/hr | ~$29/month | The VM cost can be significantly reduced using a Spot/Preemptible instance | | **Total** | **$0.15/hr** | **~$118/month** | **~$0.04/hr** | **~$39/month** | Total costs for running the VM and disk 24/7 for an entire month | - -> For a more accurate breakdown of costs, please use the [**GCP Pricing Calculator**](https://cloud.google.com/products/calculator) - From b1971570c906d377e797a56749efb16c2feddb6d Mon Sep 17 00:00:00 2001 From: Paul Lockett Date: Thu, 30 Mar 2023 14:20:20 -0700 Subject: [PATCH 41/43] Correct run instructions Fix this for future users so they can get it up and running without having to probe the GitHub forum --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3be5d99a9..3cc0372aa 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ You can install LangFlow from pip: Next, run: -`langflow` +`python -m langflow` ### Deploy Langflow on Google Cloud Platform From ad24cdc1dd6aa00211c405f48d5099df6def2d70 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 23 Apr 2023 14:25:55 -0300 Subject: [PATCH 42/43] Update README.md --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3cc0372aa..3ae891751 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,19 @@ LangFlow is a GUI for [LangChain](https://github.com/hwchase17/langchain), desig ### Locally You can install LangFlow from pip: -`pip install langflow` +```shell +pip install langflow +``` Next, run: -`python -m langflow` +```shell +python -m langflow +``` +or +```shell +langflow +``` ### Deploy Langflow on Google Cloud Platform From 76162a9ffa8314013fadb04217e09a688a635e97 Mon Sep 17 00:00:00 2001 From: Rodrigo Nader Date: Sun, 23 Apr 2023 23:06:10 -0300 Subject: [PATCH 43/43] feat(langflow): Handle ChromaDB NotEnoughElementsException This change adds error handling to catch a specific exception that may occur when processing documents with the ChromaDB library. If there are not enough documents for indexing, the error message will suggest reducing the chunk size in TextSplitter. --- src/backend/langflow/interface/run.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/backend/langflow/interface/run.py b/src/backend/langflow/interface/run.py index 300e09e01..deba28586 100644 --- a/src/backend/langflow/interface/run.py +++ b/src/backend/langflow/interface/run.py @@ -1,6 +1,7 @@ import contextlib import io from typing import Any, Dict +from chromadb.errors import NotEnoughElementsException from langflow.cache.utils import compute_dict_hash, load_cache, memoize_dict from langflow.graph.graph import Graph @@ -230,6 +231,10 @@ def get_result_and_thought_using_graph(langchain_object, message: str): else: thought = output_buffer.getvalue() + except NotEnoughElementsException as exc: + raise ValueError( + "Error: Not enough documents for ChromaDB to index. Try reducing chunk size in TextSplitter." + ) from exc except Exception as exc: raise ValueError(f"Error: {str(exc)}") from exc return result, thought