🔧 fix(base.py): remove unused import statement

🔧 fix(constants.py): remove unused import statements
🔧 fix(types.py): refactor add_new_custom_field function to update field_config values

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.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-07-07 00:42:36 -03:00
commit 1f4e947516
3 changed files with 32 additions and 3 deletions

View file

@ -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]

View file

@ -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))
"""

View file

@ -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: