From 0c153e659f8d2b0aab5bb6becc424568e0b1e6e4 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 8 Mar 2024 16:05:53 -0300 Subject: [PATCH] Update APIRequest and ExtractDataFromRecord components --- .../langflow/components/data/APIRequest.py | 3 +- .../experimental/ExtractDataFromRecord.py | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/components/data/APIRequest.py b/src/backend/langflow/components/data/APIRequest.py index b9b3e2c51..a7cc2ac37 100644 --- a/src/backend/langflow/components/data/APIRequest.py +++ b/src/backend/langflow/components/data/APIRequest.py @@ -14,7 +14,7 @@ class APIRequest(CustomComponent): output_types: list[str] = ["Record"] documentation: str = "https://docs.langflow.org/components/utilities#api-request" field_config = { - "url": {"display_name": "URL", "info": "The URL to make the request to."}, + "urls": {"display_name": "URLs", "info": "The URLs to make the request to."}, "method": { "display_name": "Method", "info": "The HTTP method to use.", @@ -116,4 +116,5 @@ class APIRequest(CustomComponent): for u, rec in zip(urls, bodies) ] ) + self.status = results return results diff --git a/src/backend/langflow/components/experimental/ExtractDataFromRecord.py b/src/backend/langflow/components/experimental/ExtractDataFromRecord.py index 926ec025a..55a48f6c1 100644 --- a/src/backend/langflow/components/experimental/ExtractDataFromRecord.py +++ b/src/backend/langflow/components/experimental/ExtractDataFromRecord.py @@ -9,9 +9,39 @@ class ExtractKeyFromRecordComponent(CustomComponent): field_config = { "record": {"display_name": "Record"}, + "keys": { + "display_name": "Keys", + "info": "The keys to extract from the record.", + "input_types": [], + }, + "silent_error": { + "display_name": "Silent Errors", + "info": "If True, errors will not be raised.", + "advanced": True, + }, } - def build(self, record: Record, key: str, silent_error: bool = True) -> dict: - data = getattr(record, key) - self.status = data - return data + def build( + self, record: Record, keys: list[str], silent_error: bool = True + ) -> Record: + """ + Extracts the keys from a record. + + Args: + record (Record): The record from which to extract the keys. + keys (list[str]): The keys to extract from the record. + silent_error (bool): If True, errors will not be raised. + + Returns: + dict: The extracted keys. + """ + extracted_keys = {} + for key in keys: + try: + extracted_keys[key] = getattr(record, key) + except AttributeError: + if not silent_error: + raise KeyError(f"The key '{key}' does not exist in the record.") + return_record = Record(data=extracted_keys) + self.status = return_record + return return_record