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>
This commit is contained in:
parent
f74b58f22a
commit
8aeb801d52
2 changed files with 44 additions and 45 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"]}))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue