forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from typing import Any, Dict, List
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from langchain.memory.chat_memory import BaseChatMemory
|
|
from langchain.memory.utils import get_buffer_string
|
|
|
|
|
|
class ConversationBufferMemory(BaseChatMemory, BaseModel):
|
|
"""Buffer for storing conversation memory."""
|
|
|
|
human_prefix: str = "Human"
|
|
ai_prefix: str = "AI"
|
|
memory_key: str = "history" #: :meta private:
|
|
|
|
@property
|
|
def buffer(self) -> Any:
|
|
"""String buffer of memory."""
|
|
if self.return_messages:
|
|
return self.chat_memory.messages
|
|
else:
|
|
return get_buffer_string(
|
|
self.chat_memory.messages,
|
|
human_prefix=self.human_prefix,
|
|
ai_prefix=self.ai_prefix,
|
|
)
|
|
|
|
@property
|
|
def memory_variables(self) -> List[str]:
|
|
"""Will always return list of memory variables.
|
|
|
|
:meta private:
|
|
"""
|
|
return [self.memory_key]
|
|
|
|
def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Return history buffer."""
|
|
return {self.memory_key: self.buffer}
|