FIX: don't error when adding to canvas (#4055)

This commit is contained in:
Eric Hare 2024-10-07 13:25:17 -07:00 • committed by GitHub
commit 3ea7be12e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,7 +37,11 @@ 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":
# 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
# Fetch the template
template = self._fetch_langchain_hub_template()
# Get the template's messages
@ -84,6 +89,7 @@ class LangChainHubPromptComponent(Component):
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
@ -91,8 +97,10 @@ class LangChainHubPromptComponent(Component):
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