From f198645decf797c00fc56d4b78f6d3ec644377af Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Thu, 13 Jun 2024 09:57:26 -0300 Subject: [PATCH] refactor: Update APIRequestComponent and URLComponent Update the APIRequestComponent and URLComponent to improve functionality and error handling. In APIRequestComponent, add the ability to parse curl commands and update the build configuration accordingly. In URLComponent, include metadata in the returned data objects. These changes enhance the overall performance and user experience of the components. --- .../langflow/components/data/APIRequest.py | 22 ++++++++++++------- .../base/langflow/components/data/URL.py | 7 +++--- .../base/langflow/components/data/__init__.py | 7 +++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/backend/base/langflow/components/data/APIRequest.py b/src/backend/base/langflow/components/data/APIRequest.py index d490d4365..ba9855cd5 100644 --- a/src/backend/base/langflow/components/data/APIRequest.py +++ b/src/backend/base/langflow/components/data/APIRequest.py @@ -1,14 +1,15 @@ import asyncio import json -from typing import List, Optional +from typing import Any, List, Optional import httpx from loguru import logger from langflow.base.curl.parse import parse_context from langflow.custom import Component -from langflow.inputs import StrInput, DropdownInput, NestedDictInput, IntInput +from langflow.inputs import DropdownInput, IntInput, NestedDictInput, StrInput from langflow.schema import Data +from langflow.schema.dotdict import dotdict from langflow.template import Output @@ -21,14 +22,15 @@ class APIRequestComponent(Component): StrInput( name="urls", display_name="URLs", - multiline=True, + is_list=True, info="Enter one or more URLs, separated by commas.", ), StrInput( name="curl", display_name="Curl", info="Paste a curl command to populate the fields.", - advanced=True, + advanced=False, + refresh_button=True, ), DropdownInput( name="method", @@ -59,8 +61,7 @@ class APIRequestComponent(Component): Output(display_name="Data", name="data", method="make_requests"), ] - def parse_curl(self, curl: str): - build_config = self._build_config() + def parse_curl(self, curl: str, build_config: dotdict) -> dotdict: try: parsed = parse_context(curl) build_config["urls"]["value"] = [parsed.url] @@ -71,12 +72,17 @@ class APIRequestComponent(Component): json_data = json.loads(parsed.data) build_config["body"]["value"] = json_data except json.JSONDecodeError as e: - logger.error(e) + print(e) except Exception as exc: logger.error(f"Error parsing curl: {exc}") raise ValueError(f"Error parsing curl: {exc}") return build_config + def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None): + if field_name == "curl" and field_value is not None: + build_config = self.parse_curl(field_value, build_config) + return build_config + async def make_request( self, client: httpx.AsyncClient, @@ -127,7 +133,7 @@ class APIRequestComponent(Component): async def make_requests(self) -> List[Data]: method = self.method - urls = [url.strip() for url in self.urls.split(",") if url.strip()] + urls = [url.strip() for url in self.urls if url.strip()] curl = self.curl headers = self.headers or {} body = self.body or {} diff --git a/src/backend/base/langflow/components/data/URL.py b/src/backend/base/langflow/components/data/URL.py index ddd8b18ee..6f7a5391a 100644 --- a/src/backend/base/langflow/components/data/URL.py +++ b/src/backend/base/langflow/components/data/URL.py @@ -1,8 +1,9 @@ +from langchain_community.document_loaders.web_base import WebBaseLoader + from langflow.custom import Component from langflow.inputs import StrInput -from langflow.template import Output from langflow.schema import Data -from langchain_community.document_loaders.web_base import WebBaseLoader +from langflow.template import Output class URLComponent(Component): @@ -28,6 +29,6 @@ class URLComponent(Component): urls = [url.strip() for url in self.urls if url.strip()] loader = WebBaseLoader(web_paths=urls) docs = loader.load() - data = [Data(content=doc.page_content) for doc in docs] + data = [Data(content=doc.page_content, **doc.metadata) for doc in docs] self.status = data return data diff --git a/src/backend/base/langflow/components/data/__init__.py b/src/backend/base/langflow/components/data/__init__.py index c57cf8656..ba037a740 100644 --- a/src/backend/base/langflow/components/data/__init__.py +++ b/src/backend/base/langflow/components/data/__init__.py @@ -1,8 +1,7 @@ -from .APIRequest import APIRequest +from .APIRequest import APIRequestComponent from .Directory import DirectoryComponent from .File import FileComponent +from .URL import URLComponent from .Webhook import WebhookComponent -from .URL import URLComponent - -__all__ = ["APIRequest", "DirectoryComponent", "FileComponent", "URLComponent", "WebhookComponent"] +__all__ = ["APIRequestComponent", "DirectoryComponent", "FileComponent", "URLComponent", "WebhookComponent"]