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.
This commit is contained in:
ogabrielluiz 2024-06-13 09:57:26 -03:00
commit f198645dec
3 changed files with 21 additions and 15 deletions

View file

@ -1,14 +1,15 @@
import asyncio import asyncio
import json import json
from typing import List, Optional from typing import Any, List, Optional
import httpx import httpx
from loguru import logger from loguru import logger
from langflow.base.curl.parse import parse_context from langflow.base.curl.parse import parse_context
from langflow.custom import Component 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 import Data
from langflow.schema.dotdict import dotdict
from langflow.template import Output from langflow.template import Output
@ -21,14 +22,15 @@ class APIRequestComponent(Component):
StrInput( StrInput(
name="urls", name="urls",
display_name="URLs", display_name="URLs",
multiline=True, is_list=True,
info="Enter one or more URLs, separated by commas.", info="Enter one or more URLs, separated by commas.",
), ),
StrInput( StrInput(
name="curl", name="curl",
display_name="Curl", display_name="Curl",
info="Paste a curl command to populate the fields.", info="Paste a curl command to populate the fields.",
advanced=True, advanced=False,
refresh_button=True,
), ),
DropdownInput( DropdownInput(
name="method", name="method",
@ -59,8 +61,7 @@ class APIRequestComponent(Component):
Output(display_name="Data", name="data", method="make_requests"), Output(display_name="Data", name="data", method="make_requests"),
] ]
def parse_curl(self, curl: str): def parse_curl(self, curl: str, build_config: dotdict) -> dotdict:
build_config = self._build_config()
try: try:
parsed = parse_context(curl) parsed = parse_context(curl)
build_config["urls"]["value"] = [parsed.url] build_config["urls"]["value"] = [parsed.url]
@ -71,12 +72,17 @@ class APIRequestComponent(Component):
json_data = json.loads(parsed.data) json_data = json.loads(parsed.data)
build_config["body"]["value"] = json_data build_config["body"]["value"] = json_data
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logger.error(e) print(e)
except Exception as exc: except Exception as exc:
logger.error(f"Error parsing curl: {exc}") logger.error(f"Error parsing curl: {exc}")
raise ValueError(f"Error parsing curl: {exc}") raise ValueError(f"Error parsing curl: {exc}")
return build_config 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( async def make_request(
self, self,
client: httpx.AsyncClient, client: httpx.AsyncClient,
@ -127,7 +133,7 @@ class APIRequestComponent(Component):
async def make_requests(self) -> List[Data]: async def make_requests(self) -> List[Data]:
method = self.method 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 curl = self.curl
headers = self.headers or {} headers = self.headers or {}
body = self.body or {} body = self.body or {}

View file

@ -1,8 +1,9 @@
from langchain_community.document_loaders.web_base import WebBaseLoader
from langflow.custom import Component from langflow.custom import Component
from langflow.inputs import StrInput from langflow.inputs import StrInput
from langflow.template import Output
from langflow.schema import Data from langflow.schema import Data
from langchain_community.document_loaders.web_base import WebBaseLoader from langflow.template import Output
class URLComponent(Component): class URLComponent(Component):
@ -28,6 +29,6 @@ class URLComponent(Component):
urls = [url.strip() for url in self.urls if url.strip()] urls = [url.strip() for url in self.urls if url.strip()]
loader = WebBaseLoader(web_paths=urls) loader = WebBaseLoader(web_paths=urls)
docs = loader.load() 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 self.status = data
return data return data

View file

@ -1,8 +1,7 @@
from .APIRequest import APIRequest from .APIRequest import APIRequestComponent
from .Directory import DirectoryComponent from .Directory import DirectoryComponent
from .File import FileComponent from .File import FileComponent
from .URL import URLComponent
from .Webhook import WebhookComponent from .Webhook import WebhookComponent
from .URL import URLComponent __all__ = ["APIRequestComponent", "DirectoryComponent", "FileComponent", "URLComponent", "WebhookComponent"]
__all__ = ["APIRequest", "DirectoryComponent", "FileComponent", "URLComponent", "WebhookComponent"]