From de96aee509cc3cb89521408ed690098ee1e25cf2 Mon Sep 17 00:00:00 2001 From: goliath-yamon <141193714+goliath-yamon@users.noreply.github.com> Date: Tue, 20 Aug 2024 12:27:41 +0100 Subject: [PATCH] fix: correct indentation issue in PythonCodeStructuredTool and correct type for int and float inputs when updating custom component (#3323) * enhancement: Update PythonCodeStructuredTool to create inputs automatically and accept global variables * [autofix.ci] apply automated fixes * feat: Create a tool to search using SearXNG * [autofix.ci] apply automated fixes * refactor: reorganize imports and type annotations in PythonCodeStructuredTool.py for clarity and consistency * refactor: clean up imports and enhance type annotations in SearXNGTool.py for improved readability and type safety * refactor: Improved PythonCodeStructuredTool to allow arguments to have any types * refactor: Formatted and refactored SearXNGTool * refactor: Allowed RunnableExecutor to stream output and changed its build method to asynchronous. * fix: correct indentation issue in PythonCodeStructuredTool * fix: correct type for int and float inputs when updating custom component * [autofix.ci] apply automated fixes * fix: change Tool to StructuredTool due to arguments in SearXNGTool * refactor(endpoints.py): remove duplicate imports of loguru and sqlmodel to improve code readability and maintainability --------- Co-authored-by: Haseong Kim Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida --- src/backend/base/langflow/api/v1/endpoints.py | 15 +++++++++++---- .../langflow/components/prototypes/CreateData.py | 1 + .../components/tools/PythonCodeStructuredTool.py | 16 ++++++---------- .../langflow/components/tools/SearXNGTool.py | 4 ++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/backend/base/langflow/api/v1/endpoints.py b/src/backend/base/langflow/api/v1/endpoints.py index 304de79f3..ad78373da 100644 --- a/src/backend/base/langflow/api/v1/endpoints.py +++ b/src/backend/base/langflow/api/v1/endpoints.py @@ -6,9 +6,6 @@ from uuid import UUID import sqlalchemy as sa from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException, Request, UploadFile, status -from loguru import logger -from sqlmodel import Session, select - from langflow.api.v1.schemas import ( ConfigResponse, CustomComponentRequest, @@ -48,6 +45,8 @@ from langflow.services.task.service import TaskService from langflow.services.telemetry.schema import RunPayload from langflow.services.telemetry.service import TelemetryService from langflow.utils.version import get_version_info +from loguru import logger +from sqlmodel import Session, select if TYPE_CHECKING: from langflow.services.cache.base import CacheService @@ -592,7 +591,15 @@ async def custom_component_update( if hasattr(cc_instance, "set_attributes"): template = code_request.get_template() params = { - key: value_dict.get("value") for key, value_dict in template.items() if isinstance(value_dict, dict) + key: value_dict.get("value") + if value_dict.get("_input_type") != "IntInput" + else ( + int(value_dict.get("value")) # type: ignore + if value_dict.get("_input_type") != "FloatInput" + else float(value_dict.get("value")) # type: ignore + ) + for key, value_dict in template.items() + if isinstance(value_dict, dict) } load_from_db_fields = [ field_name diff --git a/src/backend/base/langflow/components/prototypes/CreateData.py b/src/backend/base/langflow/components/prototypes/CreateData.py index c77df1b57..5ece555ed 100644 --- a/src/backend/base/langflow/components/prototypes/CreateData.py +++ b/src/backend/base/langflow/components/prototypes/CreateData.py @@ -20,6 +20,7 @@ class CreateDataComponent(Component): display_name="Number of Fields", info="Number of fields to be added to the record.", real_time_refresh=True, + value=0, range_spec=RangeSpec(min=1, max=15, step=1, step_type="int"), ), MessageTextInput(name="text_key", display_name="Text Key", info="Key to be used as text.", advanced=True), diff --git a/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py b/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py index 36e901cea..096d8abb4 100644 --- a/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py +++ b/src/backend/base/langflow/components/tools/PythonCodeStructuredTool.py @@ -3,12 +3,12 @@ import json from typing import Any from langchain.agents import Tool +from langflow.base.langchain_utilities.model import LCToolComponent +from langflow.inputs.inputs import MultilineInput, MessageTextInput, BoolInput, DropdownInput, HandleInput, FieldTypes from langchain_core.tools import StructuredTool from pydantic.v1 import Field, create_model from pydantic.v1.fields import Undefined -from langflow.base.langchain_utilities.model import LCToolComponent -from langflow.inputs.inputs import BoolInput, DropdownInput, FieldTypes, HandleInput, MessageTextInput, MultilineInput from langflow.io import Output from langflow.schema import Data from langflow.schema.dotdict import dotdict @@ -259,12 +259,8 @@ class PythonCodeStructuredTool(LCToolComponent): for default in node.args.defaults: if ( - (arg.lineno is not None and default.lineno is not None and arg.lineno > default.lineno) - or ( - arg.col_offset is not None - and default.col_offset is not None - and arg.col_offset > default.col_offset - ) + arg.lineno > default.lineno + or arg.col_offset > default.col_offset or ( arg.end_lineno is not None and default.end_lineno is not None @@ -290,8 +286,8 @@ class PythonCodeStructuredTool(LCToolComponent): func_arg["annotation"] = annotation_line if isinstance(func_arg["annotation"], str) and func_arg["annotation"].count("=") > 0: func_arg["annotation"] = "=".join(func_arg["annotation"].split("=")[:-1]).strip() - if isinstance(func["args"], list): - func["args"].append(func_arg) + if isinstance(func["args"], list): + func["args"].append(func_arg) functions.append(func) return classes, functions diff --git a/src/backend/base/langflow/components/tools/SearXNGTool.py b/src/backend/base/langflow/components/tools/SearXNGTool.py index 3749a70da..86b3cd9cf 100644 --- a/src/backend/base/langflow/components/tools/SearXNGTool.py +++ b/src/backend/base/langflow/components/tools/SearXNGTool.py @@ -5,6 +5,7 @@ import json from pydantic.v1 import Field, create_model from langchain.agents import Tool +from langchain_core.tools import StructuredTool from langflow.base.langchain_utilities.model import LCToolComponent from langflow.inputs import MessageTextInput, MultiselectInput, DropdownInput, IntInput from langflow.schema.dotdict import dotdict @@ -130,12 +131,11 @@ class SearXNGToolComponent(LCToolComponent): SearxSearchSchema = create_model("SearxSearchSchema", **schema_fields) # type: ignore - tool = Tool.from_function( + tool = StructuredTool.from_function( func=_local["SearxSearch"].search, args_schema=SearxSearchSchema, name="searxng_search_tool", description="A tool that searches for tools using SearXNG.\nThe available categories are: " + ", ".join(self.categories), ) - self.status = tool return tool