From f079cdf47996fcac345fa6933f337fe3afdb5e1e Mon Sep 17 00:00:00 2001 From: mbchang Date: Sat, 27 May 2023 21:15:03 -0700 Subject: [PATCH] fix: remove empty lines that cause InvalidRequestError (#5320) # remove empty lines in GenerativeAgentMemory that cause InvalidRequestError in OpenAIEmbeddings Let's say the text given to `GenerativeAgent._parse_list` is ``` text = """ Insight 1: Insight 2: """ ``` This creates an `openai.error.InvalidRequestError: [''] is not valid under any of the given schemas - 'input'` because `GenerativeAgent.add_memory()` tries to add an empty string to the vectorstore. This PR fixes the issue by removing the empty line between `Insight 1` and `Insight 2` ## Before submitting ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 @vowelparrot @dev2049 --- langchain/experimental/generative_agents/memory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/langchain/experimental/generative_agents/memory.py b/langchain/experimental/generative_agents/memory.py index f9930ba7..92332bc3 100644 --- a/langchain/experimental/generative_agents/memory.py +++ b/langchain/experimental/generative_agents/memory.py @@ -34,7 +34,7 @@ class GenerativeAgentMemory(BaseMemory): aggregate_importance: float = 0.0 # : :meta private: """Track the sum of the 'importance' of recent memories. - + Triggers reflection when it reaches reflection_threshold.""" max_tokens_limit: int = 1200 # : :meta private: @@ -56,6 +56,7 @@ class GenerativeAgentMemory(BaseMemory): def _parse_list(text: str) -> List[str]: """Parse a newline-separated string into a list of strings.""" lines = re.split(r"\n", text.strip()) + lines = [line for line in lines if line.strip()] # remove empty lines return [re.sub(r"^\s*\d+\.\s*", "", line).strip() for line in lines] def _get_topics_of_reflection(self, last_k: int = 50) -> List[str]: