mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-04 02:03:32 +00:00 
			
		
		
		
	docstrings memory (#8018)
				
					
				
			docstrings `memory`: - added module summary - added missed docstrings - updated docstrings into consistent format - @baskaryan
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					"""Memory maintains Chain state, incorporating context from past runs."""
 | 
				
			||||||
from langchain.memory.buffer import (
 | 
					from langchain.memory.buffer import (
 | 
				
			||||||
    ConversationBufferMemory,
 | 
					    ConversationBufferMemory,
 | 
				
			||||||
    ConversationStringBufferMemory,
 | 
					    ConversationStringBufferMemory,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,12 +5,13 @@ from langchain.schema.messages import BaseMessage, get_buffer_string
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ConversationBufferWindowMemory(BaseChatMemory):
 | 
					class ConversationBufferWindowMemory(BaseChatMemory):
 | 
				
			||||||
    """Buffer for storing conversation memory."""
 | 
					    """Buffer for storing conversation memory inside a limited size window."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    human_prefix: str = "Human"
 | 
					    human_prefix: str = "Human"
 | 
				
			||||||
    ai_prefix: str = "AI"
 | 
					    ai_prefix: str = "AI"
 | 
				
			||||||
    memory_key: str = "history"  #: :meta private:
 | 
					    memory_key: str = "history"  #: :meta private:
 | 
				
			||||||
    k: int = 5
 | 
					    k: int = 5
 | 
				
			||||||
 | 
					    """Number of messages to store in buffer."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def buffer(self) -> List[BaseMessage]:
 | 
					    def buffer(self) -> List[BaseMessage]:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,8 @@ from langchain.schema import BaseChatMessageHistory, BaseMemory
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseChatMemory(BaseMemory, ABC):
 | 
					class BaseChatMemory(BaseMemory, ABC):
 | 
				
			||||||
 | 
					    """Abstract base class for chat memory."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    chat_memory: BaseChatMessageHistory = Field(default_factory=ChatMessageHistory)
 | 
					    chat_memory: BaseChatMessageHistory = Field(default_factory=ChatMessageHistory)
 | 
				
			||||||
    output_key: Optional[str] = None
 | 
					    output_key: Optional[str] = None
 | 
				
			||||||
    input_key: Optional[str] = None
 | 
					    input_key: Optional[str] = None
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ if TYPE_CHECKING:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CosmosDBChatMessageHistory(BaseChatMessageHistory):
 | 
					class CosmosDBChatMessageHistory(BaseChatMessageHistory):
 | 
				
			||||||
    """Chat history backed by Azure CosmosDB."""
 | 
					    """Chat message history backed by Azure CosmosDB."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(
 | 
					    def __init__(
 | 
				
			||||||
        self,
 | 
					        self,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,7 @@ logger = logging.getLogger(__name__)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class DynamoDBChatMessageHistory(BaseChatMessageHistory):
 | 
					class DynamoDBChatMessageHistory(BaseChatMessageHistory):
 | 
				
			||||||
    """Chat message history that stores history in AWS DynamoDB.
 | 
					    """Chat message history that stores history in AWS DynamoDB.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    This class expects that a DynamoDB table with name `table_name`
 | 
					    This class expects that a DynamoDB table with name `table_name`
 | 
				
			||||||
    and a partition Key of `SessionId` is present.
 | 
					    and a partition Key of `SessionId` is present.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class FirestoreChatMessageHistory(BaseChatMessageHistory):
 | 
					class FirestoreChatMessageHistory(BaseChatMessageHistory):
 | 
				
			||||||
    """Chat history backed by Google Firestore."""
 | 
					    """Chat message history backed by Google Firestore."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(
 | 
					    def __init__(
 | 
				
			||||||
        self,
 | 
					        self,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,6 +36,7 @@ def _ensure_cache_exists(cache_client: momento.CacheClient, cache_name: str) ->
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class MomentoChatMessageHistory(BaseChatMessageHistory):
 | 
					class MomentoChatMessageHistory(BaseChatMessageHistory):
 | 
				
			||||||
    """Chat message history cache that uses Momento as a backend.
 | 
					    """Chat message history cache that uses Momento as a backend.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    See https://gomomento.com/"""
 | 
					    See https://gomomento.com/"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(
 | 
					    def __init__(
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,7 @@ logger = logging.getLogger(__name__)
 | 
				
			|||||||
def create_message_model(table_name, DynamicBase):  # type: ignore
 | 
					def create_message_model(table_name, DynamicBase):  # type: ignore
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Create a message model for a given table name.
 | 
					    Create a message model for a given table name.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Args:
 | 
					    Args:
 | 
				
			||||||
        table_name: The name of the table to use.
 | 
					        table_name: The name of the table to use.
 | 
				
			||||||
        DynamicBase: The base class to use for the model.
 | 
					        DynamicBase: The base class to use for the model.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ZepChatMessageHistory(BaseChatMessageHistory):
 | 
					class ZepChatMessageHistory(BaseChatMessageHistory):
 | 
				
			||||||
    """A ChatMessageHistory implementation that uses Zep as a backend.
 | 
					    """Chat message history that uses Zep as a backend.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Recommended usage::
 | 
					    Recommended usage::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,7 @@ from langchain.schema import BaseMemory
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CombinedMemory(BaseMemory):
 | 
					class CombinedMemory(BaseMemory):
 | 
				
			||||||
    """Class for combining multiple memories' data together."""
 | 
					    """Combining multiple memories' data together."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    memories: List[BaseMemory]
 | 
					    memories: List[BaseMemory]
 | 
				
			||||||
    """For tracking all the memories that should be accessed."""
 | 
					    """For tracking all the memories that should be accessed."""
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,8 @@ logger = logging.getLogger(__name__)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseEntityStore(BaseModel, ABC):
 | 
					class BaseEntityStore(BaseModel, ABC):
 | 
				
			||||||
 | 
					    """Abstract base class for Entity store."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @abstractmethod
 | 
					    @abstractmethod
 | 
				
			||||||
    def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
 | 
					    def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
 | 
				
			||||||
        """Get entity value from store."""
 | 
					        """Get entity value from store."""
 | 
				
			||||||
@@ -48,7 +50,7 @@ class BaseEntityStore(BaseModel, ABC):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class InMemoryEntityStore(BaseEntityStore):
 | 
					class InMemoryEntityStore(BaseEntityStore):
 | 
				
			||||||
    """Basic in-memory entity store."""
 | 
					    """In-memory Entity store."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    store: Dict[str, Optional[str]] = {}
 | 
					    store: Dict[str, Optional[str]] = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -69,7 +71,9 @@ class InMemoryEntityStore(BaseEntityStore):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class RedisEntityStore(BaseEntityStore):
 | 
					class RedisEntityStore(BaseEntityStore):
 | 
				
			||||||
    """Redis-backed Entity store. Entities get a TTL of 1 day by default, and
 | 
					    """Redis-backed Entity store.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Entities get a TTL of 1 day by default, and
 | 
				
			||||||
    that TTL is extended by 3 days every time the entity is read back.
 | 
					    that TTL is extended by 3 days every time the entity is read back.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -245,7 +249,7 @@ class ConversationEntityMemory(BaseChatMemory):
 | 
				
			|||||||
    """Entity extractor & summarizer memory.
 | 
					    """Entity extractor & summarizer memory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Extracts named entities from the recent chat history and generates summaries.
 | 
					    Extracts named entities from the recent chat history and generates summaries.
 | 
				
			||||||
    With a swapable entity store, persisting entities across conversations.
 | 
					    With a swappable entity store, persisting entities across conversations.
 | 
				
			||||||
    Defaults to an in-memory entity store, and can be swapped out for a Redis,
 | 
					    Defaults to an in-memory entity store, and can be swapped out for a Redis,
 | 
				
			||||||
    SQLite, or other entity store.
 | 
					    SQLite, or other entity store.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ from langchain.schema.messages import BaseMessage, SystemMessage, get_buffer_str
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ConversationKGMemory(BaseChatMemory):
 | 
					class ConversationKGMemory(BaseChatMemory):
 | 
				
			||||||
    """Knowledge graph memory for storing conversation memory.
 | 
					    """Knowledge graph conversation memory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Integrates with external knowledge graph to store and retrieve
 | 
					    Integrates with external knowledge graph to store and retrieve
 | 
				
			||||||
    information about knowledge triples in the conversation.
 | 
					    information about knowledge triples in the conversation.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,8 @@ MANAGED_URL = "https://api.getmetal.io/v1/motorhead"
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MotorheadMemory(BaseChatMemory):
 | 
					class MotorheadMemory(BaseChatMemory):
 | 
				
			||||||
 | 
					    """Chat message memory backed by Motorhead service."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    url: str = MANAGED_URL
 | 
					    url: str = MANAGED_URL
 | 
				
			||||||
    timeout = 3000
 | 
					    timeout = 3000
 | 
				
			||||||
    memory_key = "history"
 | 
					    memory_key = "history"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ from langchain.schema import BaseMemory
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SimpleMemory(BaseMemory):
 | 
					class SimpleMemory(BaseMemory):
 | 
				
			||||||
    """Simple memory for storing context or other bits of information that shouldn't
 | 
					    """Simple memory for storing context or other information that shouldn't
 | 
				
			||||||
    ever change between prompts.
 | 
					    ever change between prompts.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,8 @@ from langchain.schema.messages import BaseMessage, SystemMessage, get_buffer_str
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SummarizerMixin(BaseModel):
 | 
					class SummarizerMixin(BaseModel):
 | 
				
			||||||
 | 
					    """Mixin for summarizer."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    human_prefix: str = "Human"
 | 
					    human_prefix: str = "Human"
 | 
				
			||||||
    ai_prefix: str = "AI"
 | 
					    ai_prefix: str = "AI"
 | 
				
			||||||
    llm: BaseLanguageModel
 | 
					    llm: BaseLanguageModel
 | 
				
			||||||
@@ -36,7 +38,7 @@ class SummarizerMixin(BaseModel):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin):
 | 
					class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin):
 | 
				
			||||||
    """Conversation summarizer to memory."""
 | 
					    """Conversation summarizer to chat memory."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    buffer: str = ""
 | 
					    buffer: str = ""
 | 
				
			||||||
    memory_key: str = "history"  #: :meta private:
 | 
					    memory_key: str = "history"  #: :meta private:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,7 @@ from langchain.schema.messages import BaseMessage, get_buffer_string
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ConversationTokenBufferMemory(BaseChatMemory):
 | 
					class ConversationTokenBufferMemory(BaseChatMemory):
 | 
				
			||||||
    """Buffer for storing conversation memory."""
 | 
					    """Conversation chat memory with token limit."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    human_prefix: str = "Human"
 | 
					    human_prefix: str = "Human"
 | 
				
			||||||
    ai_prefix: str = "AI"
 | 
					    ai_prefix: str = "AI"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,7 +11,7 @@ from langchain.vectorstores.base import VectorStoreRetriever
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class VectorStoreRetrieverMemory(BaseMemory):
 | 
					class VectorStoreRetrieverMemory(BaseMemory):
 | 
				
			||||||
    """Class for a VectorStore-backed memory object."""
 | 
					    """VectorStoreRetriever-backed memory."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    retriever: VectorStoreRetriever = Field(exclude=True)
 | 
					    retriever: VectorStoreRetriever = Field(exclude=True)
 | 
				
			||||||
    """VectorStoreRetriever object to connect to."""
 | 
					    """VectorStoreRetriever object to connect to."""
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,7 @@ from langchain.memory.chat_message_histories import ZepChatMessageHistory
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ZepMemory(ConversationBufferMemory):
 | 
					class ZepMemory(ConversationBufferMemory):
 | 
				
			||||||
    """Persist your chain history to the Zep Long-term Memory Server
 | 
					    """Persist your chain history to the Zep Memory Server.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    The number of messages returned by Zep and when the Zep server summarizes chat
 | 
					    The number of messages returned by Zep and when the Zep server summarizes chat
 | 
				
			||||||
    histories is configurable. See the Zep documentation for more details.
 | 
					    histories is configurable. See the Zep documentation for more details.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,7 @@ from langchain.schema.messages import AIMessage, BaseMessage, HumanMessage
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BaseMemory(Serializable, ABC):
 | 
					class BaseMemory(Serializable, ABC):
 | 
				
			||||||
    """Base abstract class for memory in Chains.
 | 
					    """Abstract base class for memory in Chains.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Memory refers to state in Chains. Memory can be used to store information about
 | 
					    Memory refers to state in Chains. Memory can be used to store information about
 | 
				
			||||||
        past executions of a Chain and inject that information into the inputs of
 | 
					        past executions of a Chain and inject that information into the inputs of
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user