Merge branch 'python_custom_node_component' of github.com:logspace-ai/langflow into python_custom_node_component

This commit is contained in:
gustavoschaedler 2023-07-14 18:36:23 +01:00
commit f2687fa926
2 changed files with 109 additions and 4 deletions

View file

@ -6,24 +6,58 @@ from pydantic import BaseModel
class TemplateFieldCreator(BaseModel, ABC):
field_type: str = "str"
"""The type of field this is. Default is a string."""
required: bool = False
"""Specifies if the field is required. Defaults to False."""
placeholder: str = ""
"""A placeholder string for the field. Default is an empty string."""
is_list: bool = False
"""Defines if the field is a list. Default is False."""
show: bool = True
"""Should the field be shown. Defaults to True."""
multiline: bool = False
"""Defines if the field will allow the user to open a text editor. Default is False."""
value: Any = None
"""The value of the field. Default is None."""
suffixes: list[str] = []
fileTypes: list[str] = []
"""List of suffixes for a file field. Default is an empty list."""
file_types: list[str] = []
"""List of file types associated with the field. Default is an empty list. (duplicate)"""
file_path: Union[str, None] = None
"""The file path of the field if it is a file. Defaults to None."""
password: bool = False
"""Specifies if the field is a password. Defaults to False."""
options: list[str] = []
"""List of options for the field. Only used when is_list=True. Default is an empty list."""
name: str = ""
"""Name of the field. Default is an empty string."""
display_name: Optional[str] = None
"""Display name of the field. Defaults to None."""
advanced: bool = False
"""Specifies if the field will an advanced parameter (hidden). Defaults to False."""
input_types: list[str] = []
"""List of input types for the handle when the field has more than one type. Default is an empty list."""
dynamic: bool = False
"""Specifies if the field is dynamic. Defaults to False."""
info: Optional[str] = ""
"""Additional information about the field to be shown in the tooltip. Defaults to an empty string."""
def to_dict(self):
result = self.dict()

View file

@ -135,6 +135,24 @@ from langchain.chains.base import Chain
from langchain.prompts import StringPromptTemplate
from langflow.interface.custom.base import CustomComponent
class MyCustomChain(Chain):
"""
An example of a custom chain.
"""
from typing import Any, Dict, List, Optional
from pydantic import Extra
from langchain.schema import BaseLanguageModel, Document
from langchain.callbacks.manager import (
AsyncCallbackManagerForChainRun,
CallbackManagerForChainRun,
)
from langchain.chains.base import Chain
from langchain.prompts import StringPromptTemplate
from langflow.interface.custom.base import CustomComponent
class MyCustomChain(Chain):
"""
An example of a custom chain.
@ -232,7 +250,60 @@ class CustomChain(CustomComponent):
"llm": {"field_type": "BaseLanguageModel"},
}
def build(self, prompt: StringPromptTemplate, llm: BaseLanguageModel, input: str) -> Document:
def build(self, prompt, llm, input: str) -> Document:
chain = MyCustomChain(prompt=prompt, llm=llm)
return chain(input)
'''
return chain(input)'''
@pytest.fixture
def data_processing():
return """
import pandas as pd
from langchain.schema import Document
from langflow.interface.custom.base import CustomComponent
class CSVLoaderComponent(CustomComponent):
display_name: str = "CSV Loader"
field_config = {
"filename": {"field_type": "str", "required": True},
"column_name": {"field_type": "str", "required": True},
}
def build(self, filename: str, column_name: str) -> Document:
# Load the CSV file
df = pd.read_csv(filename)
# Verify the column exists
if column_name not in df.columns:
raise ValueError(f"Column '{column_name}' not found in the CSV file")
# Convert each row of the specified column to a document object
documents = []
for content in df[column_name]:
metadata = {"filename": filename}
documents.append(Document(page_content=str(content), metadata=metadata))
return documents
"""
@pytest.fixture
def filter_docs():
return """
from langchain.schema import Document
from langflow.interface.custom.base import CustomComponent
from typing import List
class DocumentFilterByLengthComponent(CustomComponent):
display_name: str = "Document Filter By Length"
field_config = {
"documents": {"field_type": "Document", "required": True},
"max_length": {"field_type": "int", "required": True},
}
def build(self, documents: List[Document], max_length: int) -> List[Document]:
# Filter the documents by length
filtered_documents = [doc for doc in documents if len(doc.page_content) <= max_length]
return filtered_documents
"""