🔧 chore(.gitignore): add /tmp/* to the gitignore file to exclude temporary files from version control
✨ feat(endpoints.py): add new endpoint to load custom components from a specified path 📦 feat(load_custom_component_from_path.py): add new module to load custom components from a directory path 🔧 chore(types.py): add function to build and validate custom components from a directory path
This commit is contained in:
parent
6242d80635
commit
5f8439ae7e
4 changed files with 45 additions and 4 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -251,3 +251,5 @@ langflow.db
|
||||||
|
|
||||||
# docusaurus
|
# docusaurus
|
||||||
.docusaurus/
|
.docusaurus/
|
||||||
|
|
||||||
|
/tmp/*
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from langflow.cache.utils import save_uploaded_file
|
from langflow.cache.utils import save_uploaded_file
|
||||||
from langflow.database.models.flow import Flow
|
from langflow.database.models.flow import Flow
|
||||||
from langflow.processing.process import process_graph_cached, process_tweaks
|
from langflow.processing.process import process_graph_cached, process_tweaks
|
||||||
|
|
@ -16,10 +17,11 @@ from langflow.api.v1.schemas import (
|
||||||
)
|
)
|
||||||
|
|
||||||
from langflow.interface.types import (
|
from langflow.interface.types import (
|
||||||
|
build_langchain_types_dict,
|
||||||
build_langchain_template_custom_component,
|
build_langchain_template_custom_component,
|
||||||
|
build_langchain_custom_component_list_from_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
from langflow.interface.types import build_langchain_types_dict
|
|
||||||
from langflow.database.base import get_session
|
from langflow.database.base import get_session
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
|
|
||||||
|
|
@ -32,7 +34,26 @@ def get_all():
|
||||||
return build_langchain_types_dict()
|
return build_langchain_types_dict()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/load_custom_component_from_path")
|
||||||
|
def get_load_custom_component_from_path(path: str):
|
||||||
|
return build_langchain_custom_component_list_from_path(path)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/load_custom_component_from_path_TEST")
|
||||||
|
def get_load_custom_component_from_path_test(path: str):
|
||||||
|
from langflow.interface.custom.load_custom_component_from_path import (
|
||||||
|
DirectoryReader,
|
||||||
|
)
|
||||||
|
|
||||||
|
reader = DirectoryReader(path, False)
|
||||||
|
file_list = reader.get_files()
|
||||||
|
|
||||||
|
return reader.build_component_menu_list(file_list)
|
||||||
|
|
||||||
|
|
||||||
# For backwards compatibility we will keep the old endpoint
|
# For backwards compatibility we will keep the old endpoint
|
||||||
|
|
||||||
|
|
||||||
@router.post("/predict/{flow_id}", response_model=ProcessResponse)
|
@router.post("/predict/{flow_id}", response_model=ProcessResponse)
|
||||||
@router.post("/process/{flow_id}", response_model=ProcessResponse)
|
@router.post("/process/{flow_id}", response_model=ProcessResponse)
|
||||||
async def process_flow(
|
async def process_flow(
|
||||||
|
|
|
||||||
|
|
@ -30,13 +30,13 @@ class StringCompressor:
|
||||||
|
|
||||||
|
|
||||||
class DirectoryReader:
|
class DirectoryReader:
|
||||||
def __init__(self, directory_path, compress_code=False):
|
def __init__(self, directory_path, compress_code_field=False):
|
||||||
"""
|
"""
|
||||||
Initialize DirectoryReader with a directory path
|
Initialize DirectoryReader with a directory path
|
||||||
and a flag indicating whether to compress the code.
|
and a flag indicating whether to compress the code.
|
||||||
"""
|
"""
|
||||||
self.directory_path = directory_path
|
self.directory_path = directory_path
|
||||||
self.compress_code = compress_code
|
self.compress_code_field = compress_code_field
|
||||||
|
|
||||||
def is_empty_file(self, file_content):
|
def is_empty_file(self, file_content):
|
||||||
"""
|
"""
|
||||||
|
|
@ -115,7 +115,7 @@ class DirectoryReader:
|
||||||
elif not self.validate_build(file_content):
|
elif not self.validate_build(file_content):
|
||||||
return False, "Missing build function"
|
return False, "Missing build function"
|
||||||
else:
|
else:
|
||||||
if self.compress_code:
|
if self.compress_code_field:
|
||||||
file_content = str(self.compress_string(file_content))
|
file_content = str(self.compress_string(file_content))
|
||||||
|
|
||||||
return True, file_content
|
return True, file_content
|
||||||
|
|
@ -20,6 +20,8 @@ from langflow.template.field.base import TemplateField
|
||||||
from langflow.template.frontend_node.tools import CustomComponentNode
|
from langflow.template.frontend_node.tools import CustomComponentNode
|
||||||
from langflow.interface.retrievers.base import retriever_creator
|
from langflow.interface.retrievers.base import retriever_creator
|
||||||
|
|
||||||
|
from langflow.interface.custom.load_custom_component_from_path import DirectoryReader
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
import traceback
|
import traceback
|
||||||
|
|
@ -233,3 +235,19 @@ def build_langchain_template_custom_component(custom_component: CustomComponent)
|
||||||
frontend_node.get("base_classes").append(base_class)
|
frontend_node.get("base_classes").append(base_class)
|
||||||
|
|
||||||
return frontend_node
|
return frontend_node
|
||||||
|
|
||||||
|
|
||||||
|
def build_langchain_custom_component_list_from_path(path: str):
|
||||||
|
# Load all files from Path
|
||||||
|
reader = DirectoryReader(path, False)
|
||||||
|
file_list = reader.get_files()
|
||||||
|
|
||||||
|
# Build and validate all files
|
||||||
|
data = reader.build_component_menu_list(file_list)
|
||||||
|
|
||||||
|
raw_code = data.get("menu")[0].get("components")[0].get("code")
|
||||||
|
|
||||||
|
extractor = CustomComponent(code=raw_code)
|
||||||
|
extractor.is_check_valid()
|
||||||
|
|
||||||
|
return build_langchain_template_custom_component(extractor)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue