Source code for langchain.memory.buffer
from typing import Any, Dict, List, Optional
from langchain_core.messages import BaseMessage, get_buffer_string
from langchain_core.pydantic_v1 import root_validator
from langchain.memory.chat_memory import BaseChatMemory, BaseMemory
from langchain.memory.utils import get_prompt_input_key
[docs]class ConversationBufferMemory(BaseChatMemory):
"""用于存储对话记忆的缓冲区。"""
human_prefix: str = "Human"
ai_prefix: str = "AI"
memory_key: str = "history" #: :meta private:
@property
def buffer(self) -> Any:
"""内存中的字符串缓冲区。"""
return self.buffer_as_messages if self.return_messages else self.buffer_as_str
[docs] async def abuffer(self) -> Any:
"""内存中的字符串缓冲区。"""
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(
messages,
human_prefix=self.human_prefix,
ai_prefix=self.ai_prefix,
)
@property
def buffer_as_str(self) -> str:
"""如果return_messages为True,则将缓冲区公开为字符串。"""
return self._buffer_as_str(self.chat_memory.messages)
[docs] async def abuffer_as_str(self) -> str:
"""如果return_messages为True,则将缓冲区公开为字符串。"""
messages = await self.chat_memory.aget_messages()
return self._buffer_as_str(messages)
@property
def buffer_as_messages(self) -> List[BaseMessage]:
"""在return_messages为False的情况下,将缓冲区公开为消息列表。"""
return self.chat_memory.messages
[docs] async def abuffer_as_messages(self) -> List[BaseMessage]:
"""在return_messages为False的情况下,将缓冲区公开为消息列表。"""
return await self.chat_memory.aget_messages()
@property
def memory_variables(self) -> List[str]:
"""将始终返回内存变量列表。
:元数据 私有:
"""
return [self.memory_key]
[docs] def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""返回历史缓冲区。"""
return {self.memory_key: self.buffer}
[docs] async def aload_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""给定文本输入,返回键值对。"""
buffer = await self.abuffer()
return {self.memory_key: buffer}
[docs]class ConversationStringBufferMemory(BaseMemory):
"""用于存储对话记忆的缓冲区。"""
human_prefix: str = "Human"
ai_prefix: str = "AI"
"""用于AI生成响应的前缀。"""
buffer: str = ""
output_key: Optional[str] = None
input_key: Optional[str] = None
memory_key: str = "history" #: :meta private:
@root_validator()
def validate_chains(cls, values: Dict) -> Dict:
"""验证返回消息不是True。"""
if values.get("return_messages", False):
raise ValueError(
"return_messages must be False for ConversationStringBufferMemory"
)
return values
@property
def memory_variables(self) -> List[str]:
"""将始终返回内存变量列表。
:元数据 私有:
"""
return [self.memory_key]
[docs] def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
"""返回历史缓冲区。"""
return {self.memory_key: self.buffer}
[docs] async def aload_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:
"""返回历史缓冲区。"""
return self.load_memory_variables(inputs)
[docs] def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
"""将此对话中的内容保存到缓冲区中。"""
if self.input_key is None:
prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
else:
prompt_input_key = self.input_key
if self.output_key is None:
if len(outputs) != 1:
raise ValueError(f"One output key expected, got {outputs.keys()}")
output_key = list(outputs.keys())[0]
else:
output_key = self.output_key
human = f"{self.human_prefix}: " + inputs[prompt_input_key]
ai = f"{self.ai_prefix}: " + outputs[output_key]
self.buffer += "\n" + "\n".join([human, ai])
[docs] async def asave_context(
self, inputs: Dict[str, Any], outputs: Dict[str, str]
) -> None:
"""将此对话中的内容保存到缓冲区中。"""
return self.save_context(inputs, outputs)
[docs] def clear(self) -> None:
"""清除内存内容。"""
self.buffer = ""
[docs] async def aclear(self) -> None:
self.clear()