diff --git a/src/backend/base/langflow/components/prompts/LangChainHubPrompt.py b/src/backend/base/langflow/components/prompts/LangChainHubPrompt.py index a4c4c6d5a..5048abc04 100644 --- a/src/backend/base/langflow/components/prompts/LangChainHubPrompt.py +++ b/src/backend/base/langflow/components/prompts/LangChainHubPrompt.py @@ -21,13 +21,14 @@ class LangChainHubPromptComponent(Component): name="langchain_api_key", display_name="Your LangChain API Key", info="The LangChain API Key to use.", + required=True, ), StrInput( name="langchain_hub_prompt", display_name="LangChain Hub Prompt", - info="The LangChain Hub prompt to use.", - value="efriis/my-first-prompt", + info="The LangChain Hub prompt to use, i.e., 'efriis/my-first-prompt'", refresh_button=True, + required=True, ), ] @@ -36,63 +37,70 @@ class LangChainHubPromptComponent(Component): ] def update_build_config(self, build_config: dict, field_value: str, field_name: str | None = None): - if field_name == "langchain_hub_prompt": - template = self._fetch_langchain_hub_template() + # If the field is not langchain_hub_prompt or the value is empty, return the build config as is + if field_name != "langchain_hub_prompt" or not field_value: + return build_config - # Get the template's messages - if hasattr(template, "messages"): - template_messages = template.messages - else: - template_messages = [HumanMessagePromptTemplate(prompt=template)] + # Fetch the template + template = self._fetch_langchain_hub_template() - # Extract the messages from the prompt data - prompt_template = [] - for message_data in template_messages: - prompt_template.append(message_data.prompt) + # Get the template's messages + if hasattr(template, "messages"): + template_messages = template.messages + else: + template_messages = [HumanMessagePromptTemplate(prompt=template)] - # Regular expression to find all instances of {} - pattern = r"\{(.*?)\}" + # Extract the messages from the prompt data + prompt_template = [] + for message_data in template_messages: + prompt_template.append(message_data.prompt) - # Get all the custom fields - custom_fields: list[str] = [] - full_template = "" - for message in prompt_template: - # Find all matches - matches = re.findall(pattern, message.template) - custom_fields = custom_fields + matches + # Regular expression to find all instances of {} + pattern = r"\{(.*?)\}" - # Create a string version of the full template - full_template = full_template + "\n" + message.template + # Get all the custom fields + custom_fields: list[str] = [] + full_template = "" + for message in prompt_template: + # Find all matches + matches = re.findall(pattern, message.template) + custom_fields = custom_fields + matches - # No need to reprocess if we have them already - if all("param_" + custom_field in build_config for custom_field in custom_fields): - return build_config + # Create a string version of the full template + full_template = full_template + "\n" + message.template - # Easter egg: Show template in info popup - build_config["langchain_hub_prompt"]["info"] = full_template + # No need to reprocess if we have them already + if all("param_" + custom_field in build_config for custom_field in custom_fields): + return build_config - # Remove old parameter inputs if any - for key, _ in build_config.copy().items(): - if key.startswith("param_"): - del build_config[key] + # Easter egg: Show template in info popup + build_config["langchain_hub_prompt"]["info"] = full_template - # Now create inputs for each - for custom_field in custom_fields: - new_parameter = DefaultPromptField( - name=f"param_{custom_field}", - display_name=custom_field, - info="Fill in the value for {" + custom_field + "}", - ).to_dict() + # Remove old parameter inputs if any + for key, _ in build_config.copy().items(): + if key.startswith("param_"): + del build_config[key] - build_config[f"param_{custom_field}"] = new_parameter + # Now create inputs for each + for custom_field in custom_fields: + new_parameter = DefaultPromptField( + name=f"param_{custom_field}", + display_name=custom_field, + info="Fill in the value for {" + custom_field + "}", + ).to_dict() + + # Add the new parameter to the build config + build_config[f"param_{custom_field}"] = new_parameter return build_config async def build_prompt( self, ) -> Message: - # Get the parameters that - template = self._fetch_langchain_hub_template() # TODO: doing this twice + # Fetch the template + template = self._fetch_langchain_hub_template() + + # Get the parameters from the attributes original_params = {k[6:] if k.startswith("param_") else k: v for k, v in self._attributes.items()} prompt_value = template.invoke(original_params) @@ -111,6 +119,7 @@ class LangChainHubPromptComponent(Component): # Check if the api key is provided if not self.langchain_api_key: msg = "Please provide a LangChain API Key" + raise ValueError(msg) # Pull the prompt from LangChain Hub