FIX: don't error when adding to canvas (#4055)
This commit is contained in:
parent
5615356a14
commit
3ea7be12e9
1 changed files with 52 additions and 43 deletions
|
|
@ -21,13 +21,14 @@ class LangChainHubPromptComponent(Component):
|
||||||
name="langchain_api_key",
|
name="langchain_api_key",
|
||||||
display_name="Your LangChain API Key",
|
display_name="Your LangChain API Key",
|
||||||
info="The LangChain API Key to use.",
|
info="The LangChain API Key to use.",
|
||||||
|
required=True,
|
||||||
),
|
),
|
||||||
StrInput(
|
StrInput(
|
||||||
name="langchain_hub_prompt",
|
name="langchain_hub_prompt",
|
||||||
display_name="LangChain Hub Prompt",
|
display_name="LangChain Hub Prompt",
|
||||||
info="The LangChain Hub prompt to use.",
|
info="The LangChain Hub prompt to use, i.e., 'efriis/my-first-prompt'",
|
||||||
value="efriis/my-first-prompt",
|
|
||||||
refresh_button=True,
|
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):
|
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()
|
template = self._fetch_langchain_hub_template()
|
||||||
|
|
||||||
# Get the template's messages
|
# Get the template's messages
|
||||||
|
|
@ -84,6 +89,7 @@ class LangChainHubPromptComponent(Component):
|
||||||
info="Fill in the value for {" + custom_field + "}",
|
info="Fill in the value for {" + custom_field + "}",
|
||||||
).to_dict()
|
).to_dict()
|
||||||
|
|
||||||
|
# Add the new parameter to the build config
|
||||||
build_config[f"param_{custom_field}"] = new_parameter
|
build_config[f"param_{custom_field}"] = new_parameter
|
||||||
|
|
||||||
return build_config
|
return build_config
|
||||||
|
|
@ -91,8 +97,10 @@ class LangChainHubPromptComponent(Component):
|
||||||
async def build_prompt(
|
async def build_prompt(
|
||||||
self,
|
self,
|
||||||
) -> Message:
|
) -> Message:
|
||||||
# Get the parameters that
|
# Fetch the template
|
||||||
template = self._fetch_langchain_hub_template() # TODO: doing this twice
|
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()}
|
original_params = {k[6:] if k.startswith("param_") else k: v for k, v in self._attributes.items()}
|
||||||
prompt_value = template.invoke(original_params)
|
prompt_value = template.invoke(original_params)
|
||||||
|
|
||||||
|
|
@ -111,6 +119,7 @@ class LangChainHubPromptComponent(Component):
|
||||||
# Check if the api key is provided
|
# Check if the api key is provided
|
||||||
if not self.langchain_api_key:
|
if not self.langchain_api_key:
|
||||||
msg = "Please provide a LangChain API Key"
|
msg = "Please provide a LangChain API Key"
|
||||||
|
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
# Pull the prompt from LangChain Hub
|
# Pull the prompt from LangChain Hub
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue