|
|
@ -19,20 +19,40 @@ class ConversationBufferMemory(BaseChatMemory):
|
|
|
|
"""String buffer of memory."""
|
|
|
|
"""String buffer of memory."""
|
|
|
|
return self.buffer_as_messages if self.return_messages else self.buffer_as_str
|
|
|
|
return self.buffer_as_messages if self.return_messages else self.buffer_as_str
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
async def abuffer(self) -> Any:
|
|
|
|
def buffer_as_str(self) -> str:
|
|
|
|
"""String buffer of memory."""
|
|
|
|
"""Exposes the buffer as a string in case return_messages is True."""
|
|
|
|
return (
|
|
|
|
|
|
|
|
await self.abuffer_as_messages()
|
|
|
|
|
|
|
|
if self.return_messages
|
|
|
|
|
|
|
|
else await self.abuffer_as_str()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _buffer_as_str(self, messages: List[BaseMessage]) -> str:
|
|
|
|
return get_buffer_string(
|
|
|
|
return get_buffer_string(
|
|
|
|
self.chat_memory.messages,
|
|
|
|
messages,
|
|
|
|
human_prefix=self.human_prefix,
|
|
|
|
human_prefix=self.human_prefix,
|
|
|
|
ai_prefix=self.ai_prefix,
|
|
|
|
ai_prefix=self.ai_prefix,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def buffer_as_str(self) -> str:
|
|
|
|
|
|
|
|
"""Exposes the buffer as a string in case return_messages is True."""
|
|
|
|
|
|
|
|
return self._buffer_as_str(self.chat_memory.messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def abuffer_as_str(self) -> str:
|
|
|
|
|
|
|
|
"""Exposes the buffer as a string in case return_messages is True."""
|
|
|
|
|
|
|
|
messages = await self.chat_memory.aget_messages()
|
|
|
|
|
|
|
|
return self._buffer_as_str(messages)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def buffer_as_messages(self) -> List[BaseMessage]:
|
|
|
|
def buffer_as_messages(self) -> List[BaseMessage]:
|
|
|
|
"""Exposes the buffer as a list of messages in case return_messages is False."""
|
|
|
|
"""Exposes the buffer as a list of messages in case return_messages is False."""
|
|
|
|
return self.chat_memory.messages
|
|
|
|
return self.chat_memory.messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def abuffer_as_messages(self) -> List[BaseMessage]:
|
|
|
|
|
|
|
|
"""Exposes the buffer as a list of messages in case return_messages is False."""
|
|
|
|
|
|
|
|
return await self.chat_memory.aget_messages()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def memory_variables(self) -> List[str]:
|
|
|
|
def memory_variables(self) -> List[str]:
|
|
|
|
"""Will always return list of memory variables.
|
|
|
|
"""Will always return list of memory variables.
|
|
|
@ -45,6 +65,11 @@ class ConversationBufferMemory(BaseChatMemory):
|
|
|
|
"""Return history buffer."""
|
|
|
|
"""Return history buffer."""
|
|
|
|
return {self.memory_key: self.buffer}
|
|
|
|
return {self.memory_key: self.buffer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def aload_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
"""Return key-value pairs given the text input to the chain."""
|
|
|
|
|
|
|
|
buffer = await self.abuffer()
|
|
|
|
|
|
|
|
return {self.memory_key: buffer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConversationStringBufferMemory(BaseMemory):
|
|
|
|
class ConversationStringBufferMemory(BaseMemory):
|
|
|
|
"""Buffer for storing conversation memory."""
|
|
|
|
"""Buffer for storing conversation memory."""
|
|
|
@ -77,6 +102,10 @@ class ConversationStringBufferMemory(BaseMemory):
|
|
|
|
"""Return history buffer."""
|
|
|
|
"""Return history buffer."""
|
|
|
|
return {self.memory_key: self.buffer}
|
|
|
|
return {self.memory_key: self.buffer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def aload_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
|
|
|
|
|
|
|
|
"""Return history buffer."""
|
|
|
|
|
|
|
|
return self.load_memory_variables(inputs)
|
|
|
|
|
|
|
|
|
|
|
|
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
|
|
|
|
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
|
|
|
|
"""Save context from this conversation to buffer."""
|
|
|
|
"""Save context from this conversation to buffer."""
|
|
|
|
if self.input_key is None:
|
|
|
|
if self.input_key is None:
|
|
|
@ -93,6 +122,15 @@ class ConversationStringBufferMemory(BaseMemory):
|
|
|
|
ai = f"{self.ai_prefix}: " + outputs[output_key]
|
|
|
|
ai = f"{self.ai_prefix}: " + outputs[output_key]
|
|
|
|
self.buffer += "\n" + "\n".join([human, ai])
|
|
|
|
self.buffer += "\n" + "\n".join([human, ai])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def asave_context(
|
|
|
|
|
|
|
|
self, inputs: Dict[str, Any], outputs: Dict[str, str]
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
"""Save context from this conversation to buffer."""
|
|
|
|
|
|
|
|
return self.save_context(inputs, outputs)
|
|
|
|
|
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
def clear(self) -> None:
|
|
|
|
"""Clear memory contents."""
|
|
|
|
"""Clear memory contents."""
|
|
|
|
self.buffer = ""
|
|
|
|
self.buffer = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def aclear(self) -> None:
|
|
|
|
|
|
|
|
self.clear()
|
|
|
|