🐛 fix(chat_manager.py): fix is_first_message logic to correctly identify first message

🐛 fix(nodes.py): remove deepcopying of certain objects to improve performance
🐛 fix(loading.py): add error handling for empty documents in instantiate_textsplitter
The is_first_message logic in chat_manager.py was not correctly identifying the first message. The fix in this commit changes the comparison to <= 1 instead of == 0. In nodes.py, deepcopying of certain objects was removed to improve performance. This was done because deepcopying was not necessary for these objects. In loading.py, error handling was added to handle empty documents in instantiate_textsplitter. This was done to prevent errors when the source provided did not load correctly or was empty.
This commit is contained in:
Gabriel Almeida 2023-05-25 10:10:56 -03:00
commit 3029fe029f
3 changed files with 28 additions and 6 deletions

View file

@ -110,7 +110,7 @@ class ChatManager:
start_resp = ChatResponse(message=None, type="start", intermediate_steps="")
await self.send_json(client_id, start_resp)
is_first_message = len(self.chat_history.get_history(client_id=client_id)) == 0
is_first_message = len(self.chat_history.get_history(client_id=client_id)) <= 1
# Generate result and thought
try:
logger.debug("Generating result and thought")

View file

@ -36,7 +36,7 @@ class AgentNode(Node):
#! Cannot deepcopy VectorStore, VectorStoreRouter, or SQL agents
if self.node_type in ["VectorStoreAgent", "VectorStoreRouterAgent", "SQLAgent"]:
return self._built_object
return deepcopy(self._built_object)
return self._built_object
class ToolNode(Node):
@ -81,7 +81,7 @@ class PromptNode(Node):
self.params["input_variables"] = list(set(self.params["input_variables"]))
self._build()
return deepcopy(self._built_object)
return self._built_object
class ChainNode(Node):
@ -105,13 +105,29 @@ class ChainNode(Node):
#! Cannot deepcopy SQLDatabaseChain
if self.node_type in ["SQLDatabaseChain"]:
return self._built_object
return deepcopy(self._built_object)
return self._built_object
class LLMNode(Node):
built_node_type = None
class_built_object = None
def __init__(self, data: Dict):
super().__init__(data, base_type="llms")
def build(self, force: bool = False) -> Any:
# LLM is different because some models might take up too much memory
# or time to load. So we only load them when we need them.ß
if self.node_type == self.built_node_type:
return self.class_built_object
if not self._built or force:
self._build()
self.built_node_type = self.node_type
self.class_built_object = self._built_object
# Avoid deepcopying the LLM
# that are loaded from a file
return self._built_object
class ToolkitNode(Node):
def __init__(self, data: Dict):
@ -132,7 +148,7 @@ class WrapperNode(Node):
if "headers" in self.params:
self.params["headers"] = eval(self.params["headers"])
self._build()
return deepcopy(self._built_object)
return self._built_object
class DocumentLoaderNode(Node):

View file

@ -134,7 +134,13 @@ def instantiate_documentloader(class_object, params):
def instantiate_textsplitter(class_object, params):
documents = params.pop("documents")
try:
documents = params.pop("documents")
except KeyError as e:
raise ValueError(
"The source you provided did not load correctly or was empty."
"Try changing the chunk_size of the Text Splitter."
) from e
text_splitter = class_object(**params)
return text_splitter.split_documents(documents)