From 1f4e94751672c9728e0efa1535862bc66d485eed Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 7 Jul 2023 00:42:36 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(base.py):=20remove=20unused?= =?UTF-8?q?=20import=20statement=20=F0=9F=94=A7=20fix(constants.py):=20rem?= =?UTF-8?q?ove=20unused=20import=20statements=20=F0=9F=94=A7=20fix(types.p?= =?UTF-8?q?y):=20refactor=20add=5Fnew=5Fcustom=5Ffield=20function=20to=20u?= =?UTF-8?q?pdate=20field=5Fconfig=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The import statement for `get_custom_nodes` in `base.py` is removed as it is not being used in the code. Similarly, the import statements for `requests` and `LLMChain` in `constants.py` are removed as they are not being used either. In `types.py`, the `add_new_custom_field` function is refactored to update the values of `field_config` if any of the keys are present in it. This allows for more flexibility in customizing the field configuration for a custom component. --- src/backend/langflow/interface/custom/base.py | 4 +++- .../langflow/interface/custom/constants.py | 17 +++++++++++++++++ src/backend/langflow/interface/types.py | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) 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: