From d3a2367932c5de9cf57716ec34cb5024aac867ce Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 26 Jun 2023 13:27:34 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=80=20feat(custom.py):=20add=20Age?= =?UTF-8?q?ntType=20enum=20to=20improve=20readability=20and=20type=20safet?= =?UTF-8?q?y=20The=20`AgentType`=20enum=20is=20added=20to=20the=20`langcha?= =?UTF-8?q?in.agents.custom`=20module=20to=20improve=20readability=20and?= =?UTF-8?q?=20type=20safety.=20The=20`InitializeAgent`=20class=20now=20use?= =?UTF-8?q?s=20the=20`AgentType`=20enum=20to=20ensure=20that=20the=20`agen?= =?UTF-8?q?t`=20parameter=20is=20a=20valid=20value=20from=20the=20enum.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/agents/custom.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/langflow/interface/agents/custom.py b/src/backend/langflow/interface/agents/custom.py index a4191a8b7..aa0cfb5db 100644 --- a/src/backend/langflow/interface/agents/custom.py +++ b/src/backend/langflow/interface/agents/custom.py @@ -6,6 +6,7 @@ from langchain.agents import ( Tool, ZeroShotAgent, initialize_agent, + AgentType, ) from langchain.agents.agent_toolkits import ( SQLDatabaseToolkit, @@ -297,6 +298,9 @@ class InitializeAgent(CustomAgentExecutor): agent: str, memory: Optional[BaseChatMemory] = None, ): + # Find which value in the AgentType enum corresponds to the string + # passed in as agent + agent = AgentType(agent) return initialize_agent( tools=tools, llm=llm, From 8107f9e332137ce87d4cd9558276a7598c5d4175 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 12:06:23 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20feat(loading.py):=20add=20suppo?= =?UTF-8?q?rt=20for=20new=20prompt=20templates=20in=20instantiate=5Fprompt?= =?UTF-8?q?=20function=20The=20config.yaml=20file=20has=20been=20updated?= =?UTF-8?q?=20to=20include=20new=20prompt=20templates.=20In=20types.py,=20?= =?UTF-8?q?the=20input=5Fvariables=20list=20was=20not=20being=20cleared=20?= =?UTF-8?q?when=20prompt=20or=20messages=20were=20present,=20which=20has?= =?UTF-8?q?=20been=20fixed.=20The=20instantiate=5Fprompt=20function=20in?= =?UTF-8?q?=20loading.py=20has=20been=20updated=20to=20support=20the=20new?= =?UTF-8?q?=20prompt=20templates.=20=F0=9F=94=80=20chore(config):=20add=20?= =?UTF-8?q?new=20prompt=20templates=20to=20config.yaml=20=F0=9F=90=9B=20fi?= =?UTF-8?q?x(types.py):=20fix=20input=5Fvariables=20not=20being=20cleared?= =?UTF-8?q?=20when=20prompt=20or=20messages=20are=20present?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/config.yaml | 5 +++++ src/backend/langflow/graph/vertex/types.py | 16 +++++++++++----- .../langflow/interface/initialize/loading.py | 13 +++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/config.yaml b/src/backend/langflow/config.yaml index d8cd4a325..26fec2be3 100644 --- a/src/backend/langflow/config.yaml +++ b/src/backend/langflow/config.yaml @@ -73,6 +73,11 @@ prompts: - PromptTemplate - FewShotPromptTemplate - ZeroShotPrompt + - ChatPromptTemplate + - SystemMessagePromptTemplate + - AIMessagePromptTemplate + - HumanMessagePromptTemplate + - ChatMessagePromptTemplate textsplitters: - CharacterTextSplitter - RecursiveCharacterTextSplitter diff --git a/src/backend/langflow/graph/vertex/types.py b/src/backend/langflow/graph/vertex/types.py index b6a8f9a91..775a2ebd2 100644 --- a/src/backend/langflow/graph/vertex/types.py +++ b/src/backend/langflow/graph/vertex/types.py @@ -180,11 +180,17 @@ class PromptVertex(Vertex): ] else: prompt_params = ["template"] - for param in prompt_params: - prompt_text = self.params[param] - variables = extract_input_variables_from_prompt(prompt_text) - self.params["input_variables"].extend(variables) - self.params["input_variables"] = list(set(self.params["input_variables"])) + + if "prompt" not in self.params and "messages" not in self.params: + for param in prompt_params: + prompt_text = self.params[param] + variables = extract_input_variables_from_prompt(prompt_text) + self.params["input_variables"].extend(variables) + self.params["input_variables"] = list( + set(self.params["input_variables"]) + ) + else: + self.params.pop("input_variables", None) self._build() return self._built_object diff --git a/src/backend/langflow/interface/initialize/loading.py b/src/backend/langflow/interface/initialize/loading.py index 88b981f9d..235eabaff 100644 --- a/src/backend/langflow/interface/initialize/loading.py +++ b/src/backend/langflow/interface/initialize/loading.py @@ -97,6 +97,19 @@ def instantiate_prompt(node_type, class_object, params): if "tools" not in params: params["tools"] = [] return ZeroShotAgent.create_prompt(**params) + if "MessagePromptTemplate" in node_type: + # Then we only need the template + from_template_params = { + "template": params.pop("prompt", params.pop("template", "")) + } + + if not from_template_params.get("template"): + raise ValueError("Prompt template is required") + return class_object.from_template(**from_template_params) + + if node_type == "ChatPromptTemplate": + return class_object.from_messages(**params) + return class_object(**params)