From 8aeb801d5225a45981bcfde52003239d9fbab90c Mon Sep 17 00:00:00 2001 From: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:58:42 -0700 Subject: [PATCH] fix: exception locations in component classes (#4087) * fix exception locations in component classes * print cleanups * remove randomdbs * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../components/astra_assistants/run.py | 51 ++++++++----------- .../langchain_utilities/SpiderTool.py | 38 +++++++++----- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/backend/base/langflow/components/astra_assistants/run.py b/src/backend/base/langflow/components/astra_assistants/run.py index 70d116667..dbd6422f9 100644 --- a/src/backend/base/langflow/components/astra_assistants/run.py +++ b/src/backend/base/langflow/components/astra_assistants/run.py @@ -64,40 +64,29 @@ class AssistantsRun(ComponentWithCache): def process_inputs(self) -> Message: patch(OpenAI()) - try: - text = "" - if self.thread_id is None: - thread = self.client.beta.threads.create() - self.thread_id = thread.id + text = "" - # add the user message - self.client.beta.threads.messages.create(thread_id=self.thread_id, role="user", content=self.user_message) + if self.thread_id is None: + thread = self.client.beta.threads.create() + self.thread_id = thread.id - class EventHandler(AssistantEventHandler): - def __init__(self): - super().__init__() + # add the user message + self.client.beta.threads.messages.create(thread_id=self.thread_id, role="user", content=self.user_message) - def on_exception(self, exception: Exception) -> None: - print(f"Exception: {exception}") - raise exception + class EventHandler(AssistantEventHandler): + def __init__(self): + super().__init__() - event_handler = EventHandler() - with self.client.beta.threads.runs.create_and_stream( - thread_id=self.thread_id, - assistant_id=self.assistant_id, - event_handler=event_handler, - ) as stream: - # return stream.text_deltas - for part in stream.text_deltas: - text += part - print(part) - return Message(text=text) - except Exception as e: - print(e) - msg = f"Error running assistant: {e}" - raise AssistantsRunError(msg) from e + def on_exception(self, exception: Exception) -> None: + raise exception - -class AssistantsRunError(Exception): - """AssistantsRun error""" + event_handler = EventHandler() + with self.client.beta.threads.runs.create_and_stream( + thread_id=self.thread_id, + assistant_id=self.assistant_id, + event_handler=event_handler, + ) as stream: + for part in stream.text_deltas: + text += part + return Message(text=text) diff --git a/src/backend/base/langflow/components/langchain_utilities/SpiderTool.py b/src/backend/base/langflow/components/langchain_utilities/SpiderTool.py index 7572ac6ff..b4a874c74 100644 --- a/src/backend/base/langflow/components/langchain_utilities/SpiderTool.py +++ b/src/backend/base/langflow/components/langchain_utilities/SpiderTool.py @@ -2,7 +2,15 @@ from spider.spider import Spider from langflow.base.langchain_utilities.spider_constants import MODES from langflow.custom import Component -from langflow.io import BoolInput, DictInput, DropdownInput, IntInput, Output, SecretStrInput, StrInput +from langflow.io import ( + BoolInput, + DictInput, + DropdownInput, + IntInput, + Output, + SecretStrInput, + StrInput, +) from langflow.schema import Data @@ -103,25 +111,27 @@ class SpiderTool(Component): } app = Spider(api_key=self.spider_api_key) - try: - if self.mode == "scrape": - parameters["limit"] = 1 - result = app.scrape_url(self.url, parameters) - elif self.mode == "crawl": - result = app.crawl_url(self.url, parameters) - else: - msg = f"Invalid mode: {self.mode}. Must be 'scrape' or 'crawl'." - raise ValueError(msg) - except Exception as e: - msg = f"Error: {e}" - raise SpiderToolError(msg) from e + if self.mode == "scrape": + parameters["limit"] = 1 + result = app.scrape_url(self.url, parameters) + elif self.mode == "crawl": + result = app.crawl_url(self.url, parameters) + else: + msg = f"Invalid mode: {self.mode}. Must be 'scrape' or 'crawl'." + raise ValueError(msg) records = [] for record in result: if self.metadata: records.append( - Data(data={"content": record["content"], "url": record["url"], "metadata": record["metadata"]}) + Data( + data={ + "content": record["content"], + "url": record["url"], + "metadata": record["metadata"], + } + ) ) else: records.append(Data(data={"content": record["content"], "url": record["url"]}))