diff --git a/src/backend/langflow/interface/custom/base.py b/src/backend/langflow/interface/custom/base.py index 359b799ce..bf5ca80d9 100644 --- a/src/backend/langflow/interface/custom/base.py +++ b/src/backend/langflow/interface/custom/base.py @@ -1,6 +1,6 @@ from typing import Any, Dict, List, Optional, Type -from langflow.custom.customs import get_custom_nodes + from langflow.interface.base import LangChainTypeCreator from langflow.interface.custom.custom import CustomComponent from langflow.template.frontend_node.custom_components import ( @@ -27,6 +27,8 @@ class CustomComponentCreator(LangChainTypeCreator): return self.type_dict def get_signature(self, name: str) -> Optional[Dict]: + from langflow.custom.customs import get_custom_nodes + try: if name in get_custom_nodes(self.type_name).keys(): return get_custom_nodes(self.type_name)[name] diff --git a/src/backend/langflow/interface/custom/constants.py b/src/backend/langflow/interface/custom/constants.py index 920c2bc09..9e7b27750 100644 --- a/src/backend/langflow/interface/custom/constants.py +++ b/src/backend/langflow/interface/custom/constants.py @@ -21,3 +21,20 @@ LANGCHAIN_BASE_TYPES = { "Embeddings": Embeddings, "BaseRetriever": BaseRetriever, } +DEFAULT_CUSTOM_COMPONENT_CODE = """ +from langchain.chains import LLMChain +from langflow.interface.custom import CustomComponent +from langchain.schema import Document +import requests + +class YourComponent(CustomComponent): + display_name: str = "Your Component" + description: str = "Your description" + field_config = { "url": { "multiline": True, "required": True } } + + def build(self, url: str, llm: BaseLLM, prompt: prompt) -> Document: + response = requests.get(url) + chain = LLMChain(llm=llm, prompt=prompt) + result = chain.run(response.text) + return Document(page_content=str(result)) +""" diff --git a/src/backend/langflow/interface/types.py b/src/backend/langflow/interface/types.py index c91abcfbe..f9d4f72ba 100644 --- a/src/backend/langflow/interface/types.py +++ b/src/backend/langflow/interface/types.py @@ -76,12 +76,20 @@ def build_langchain_types_dict(): # sourcery skip: dict-assign-update-to-union def add_new_custom_field( template, field_name: str, field_type: str, field_config: dict ): + # Check field_config if any of the keys are in it + # if it is, update the value + name = field_config.pop("name", field_name) + field_type = field_config.pop("field_type", field_type) + required = field_config.pop("required", True) + placeholder = field_config.pop("placeholder", "") + new_field = TemplateField( - name=field_name, + name=name, field_type=field_type, show=True, - required=True, + required=required, advanced=False, + placeholder=placeholder, **field_config, ) template.get("template")[field_name] = new_field.to_dict() @@ -121,6 +129,8 @@ def build_langchain_template_custom_component(extractor: CustomComponent): if "display_name" in template_config and frontend_node is not None: frontend_node["display_name"] = template_config["display_name"] + if "description" in template_config and frontend_node is not None: + frontend_node["description"] = template_config["description"] raw_code = extractor.code field_config = template_config.get("field_config", {}) if function_args is not None: