Skip to content

Commit

Permalink
fix: exception locations in component classes (#4087)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jordanrfrazier and autofix-ci[bot] authored Oct 9, 2024
1 parent f74b58f commit 8aeb801
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
51 changes: 20 additions & 31 deletions src/backend/base/langflow/components/astra_assistants/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,29 @@ def update_build_config(

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

# add the user message
self.client.beta.threads.messages.create(thread_id=self.thread_id, role="user", content=self.user_message)
text = ""

class EventHandler(AssistantEventHandler):
def __init__(self):
super().__init__()
if self.thread_id is None:
thread = self.client.beta.threads.create()
self.thread_id = thread.id

def on_exception(self, exception: Exception) -> None:
print(f"Exception: {exception}")
raise exception
# add the user message
self.client.beta.threads.messages.create(thread_id=self.thread_id, role="user", content=self.user_message)

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
class EventHandler(AssistantEventHandler):
def __init__(self):
super().__init__()

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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

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


Expand Down Expand Up @@ -103,25 +111,27 @@ def crawl(self) -> list[Data]:
}

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"]}))
Expand Down

0 comments on commit 8aeb801

Please sign in to comment.