From 4827c9936842831f4e6bf3114d35110fa83682b4 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 19 Jul 2023 07:03:54 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(code=5Fparser.py):=20han?= =?UTF-8?q?dle=20case=20when=20code=20parameter=20is=20a=20class=20by=20ge?= =?UTF-8?q?tting=20its=20source=20code=20using=20inspect=20module=20?= =?UTF-8?q?=E2=9C=A8=20feat(code=5Fparser.py):=20add=20support=20for=20par?= =?UTF-8?q?sing=20class=20source=20code=20in=20addition=20to=20string=20so?= =?UTF-8?q?urce=20code=20to=20improve=20flexibility=20and=20usability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/custom/code_parser.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/interface/custom/code_parser.py b/src/backend/langflow/interface/custom/code_parser.py index e86c12cef..aa4191448 100644 --- a/src/backend/langflow/interface/custom/code_parser.py +++ b/src/backend/langflow/interface/custom/code_parser.py @@ -1,7 +1,8 @@ import ast +import inspect import traceback -from typing import Dict, Any, Union +from typing import Dict, Any, Type, Union from fastapi import HTTPException @@ -14,10 +15,15 @@ class CodeParser: A parser for Python source code, extracting code details. """ - def __init__(self, code: str) -> None: + def __init__(self, code: Union[str, Type]) -> None: """ Initializes the parser with the provided code. """ + if isinstance(code, type): + if not inspect.isclass(code): + raise ValueError("The provided code must be a class.") + # If the code is a class, get its source code + code = inspect.getsource(code) self.code = code self.data: Dict[str, Any] = { "imports": [], From 58d7491d83769f3e90e425253029afe2fc60995f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 19 Jul 2023 10:00:47 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A8=20refactor(endpoints.py):=20re?= =?UTF-8?q?name=20langchain=5Ftypes=5Fdict=20import=20to=20build=5Flangcha?= =?UTF-8?q?in=5Ftypes=5Fdict=20for=20clarity=20and=20consistency=20?= =?UTF-8?q?=F0=9F=94=A8=20refactor(types.py):=20remove=20unused=20langchai?= =?UTF-8?q?n=5Ftypes=5Fdict=20variable=20to=20improve=20code=20cleanliness?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/endpoints.py | 4 ++-- src/backend/langflow/interface/types.py | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index c51f9ce78..14a29bd44 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -19,7 +19,7 @@ from langflow.interface.types import ( build_langchain_template_custom_component, ) -from langflow.interface.types import langchain_types_dict +from langflow.interface.types import build_langchain_types_dict from langflow.database.base import get_session from sqlmodel import Session @@ -29,7 +29,7 @@ router = APIRouter(tags=["Base"]) @router.get("/all") def get_all(): - return langchain_types_dict + return build_langchain_types_dict() # For backwards compatibility we will keep the old endpoint diff --git a/src/backend/langflow/interface/types.py b/src/backend/langflow/interface/types.py index b1ba8573f..494c931e5 100644 --- a/src/backend/langflow/interface/types.py +++ b/src/backend/langflow/interface/types.py @@ -233,6 +233,3 @@ def build_langchain_template_custom_component(custom_component: CustomComponent) frontend_node.get("base_classes").append(base_class) return frontend_node - - -langchain_types_dict = build_langchain_types_dict()