Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exception locations in component classes #4087

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
jordanrfrazier marked this conversation as resolved.
Show resolved Hide resolved
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
Loading