Skip to content

Commit

Permalink
fix: correct indentation issue in PythonCodeStructuredTool and correc…
Browse files Browse the repository at this point in the history
…t 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 <dynaferkim@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
  • Loading branch information
4 people authored Aug 20, 2024
1 parent 4bcc560 commit de96aee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

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,
Expand Down Expand Up @@ -48,6 +45,8 @@
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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
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
Expand Down Expand Up @@ -259,12 +259,8 @@ def _parse_code(self, code: str) -> tuple[list[dict], list[dict]]:

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
Expand All @@ -290,8 +286,8 @@ def _parse_code(self, code: str) -> tuple[list[dict], list[dict]]:
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
Expand Down
4 changes: 2 additions & 2 deletions src/backend/base/langflow/components/tools/SearXNGTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -130,12 +131,11 @@ def search(query: str, categories: list[str] = []) -> list:

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

0 comments on commit de96aee

Please sign in to comment.