From 120cdf813de15eb38d1fcb0830b96e0d5863bef6 Mon Sep 17 00:00:00 2001 From: Leonid Ganeline Date: Mon, 24 Jul 2023 10:05:36 -0700 Subject: [PATCH] docstrings `memory` (#8018) docstrings `memory`: - added module summary - added missed docstrings - updated docstrings into consistent format - @baskaryan --- libs/langchain/langchain/memory/__init__.py | 1 + libs/langchain/langchain/memory/buffer_window.py | 3 ++- libs/langchain/langchain/memory/chat_memory.py | 2 ++ .../memory/chat_message_histories/cosmos_db.py | 2 +- .../memory/chat_message_histories/dynamodb.py | 1 + .../memory/chat_message_histories/firestore.py | 2 +- .../langchain/memory/chat_message_histories/momento.py | 1 + .../langchain/memory/chat_message_histories/sql.py | 1 + .../langchain/memory/chat_message_histories/zep.py | 2 +- libs/langchain/langchain/memory/combined.py | 2 +- libs/langchain/langchain/memory/entity.py | 10 +++++++--- libs/langchain/langchain/memory/kg.py | 2 +- libs/langchain/langchain/memory/motorhead_memory.py | 2 ++ libs/langchain/langchain/memory/simple.py | 2 +- libs/langchain/langchain/memory/summary.py | 4 +++- libs/langchain/langchain/memory/token_buffer.py | 2 +- libs/langchain/langchain/memory/vectorstore.py | 2 +- libs/langchain/langchain/memory/zep_memory.py | 2 +- libs/langchain/langchain/schema/memory.py | 2 +- 19 files changed, 30 insertions(+), 15 deletions(-) diff --git a/libs/langchain/langchain/memory/__init__.py b/libs/langchain/langchain/memory/__init__.py index 4c28f61814..de669b4778 100644 --- a/libs/langchain/langchain/memory/__init__.py +++ b/libs/langchain/langchain/memory/__init__.py @@ -1,3 +1,4 @@ +"""Memory maintains Chain state, incorporating context from past runs.""" from langchain.memory.buffer import ( ConversationBufferMemory, ConversationStringBufferMemory, diff --git a/libs/langchain/langchain/memory/buffer_window.py b/libs/langchain/langchain/memory/buffer_window.py index 34d70f59d3..af27f41d33 100644 --- a/libs/langchain/langchain/memory/buffer_window.py +++ b/libs/langchain/langchain/memory/buffer_window.py @@ -5,12 +5,13 @@ from langchain.schema.messages import BaseMessage, get_buffer_string class ConversationBufferWindowMemory(BaseChatMemory): - """Buffer for storing conversation memory.""" + """Buffer for storing conversation memory inside a limited size window.""" human_prefix: str = "Human" ai_prefix: str = "AI" memory_key: str = "history" #: :meta private: k: int = 5 + """Number of messages to store in buffer.""" @property def buffer(self) -> List[BaseMessage]: diff --git a/libs/langchain/langchain/memory/chat_memory.py b/libs/langchain/langchain/memory/chat_memory.py index 767f68d908..e322c6ddb2 100644 --- a/libs/langchain/langchain/memory/chat_memory.py +++ b/libs/langchain/langchain/memory/chat_memory.py @@ -9,6 +9,8 @@ from langchain.schema import BaseChatMessageHistory, BaseMemory class BaseChatMemory(BaseMemory, ABC): + """Abstract base class for chat memory.""" + chat_memory: BaseChatMessageHistory = Field(default_factory=ChatMessageHistory) output_key: Optional[str] = None input_key: Optional[str] = None diff --git a/libs/langchain/langchain/memory/chat_message_histories/cosmos_db.py b/libs/langchain/langchain/memory/chat_message_histories/cosmos_db.py index e1e5ad3261..819599e351 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/cosmos_db.py +++ b/libs/langchain/langchain/memory/chat_message_histories/cosmos_db.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: class CosmosDBChatMessageHistory(BaseChatMessageHistory): - """Chat history backed by Azure CosmosDB.""" + """Chat message history backed by Azure CosmosDB.""" def __init__( self, diff --git a/libs/langchain/langchain/memory/chat_message_histories/dynamodb.py b/libs/langchain/langchain/memory/chat_message_histories/dynamodb.py index 219ca9844a..3800017516 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/dynamodb.py +++ b/libs/langchain/langchain/memory/chat_message_histories/dynamodb.py @@ -16,6 +16,7 @@ logger = logging.getLogger(__name__) class DynamoDBChatMessageHistory(BaseChatMessageHistory): """Chat message history that stores history in AWS DynamoDB. + This class expects that a DynamoDB table with name `table_name` and a partition Key of `SessionId` is present. diff --git a/libs/langchain/langchain/memory/chat_message_histories/firestore.py b/libs/langchain/langchain/memory/chat_message_histories/firestore.py index fdfb4e5669..9e20a9ba96 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/firestore.py +++ b/libs/langchain/langchain/memory/chat_message_histories/firestore.py @@ -16,7 +16,7 @@ if TYPE_CHECKING: class FirestoreChatMessageHistory(BaseChatMessageHistory): - """Chat history backed by Google Firestore.""" + """Chat message history backed by Google Firestore.""" def __init__( self, diff --git a/libs/langchain/langchain/memory/chat_message_histories/momento.py b/libs/langchain/langchain/memory/chat_message_histories/momento.py index 5fefc790ef..448801db81 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/momento.py +++ b/libs/langchain/langchain/memory/chat_message_histories/momento.py @@ -36,6 +36,7 @@ def _ensure_cache_exists(cache_client: momento.CacheClient, cache_name: str) -> class MomentoChatMessageHistory(BaseChatMessageHistory): """Chat message history cache that uses Momento as a backend. + See https://gomomento.com/""" def __init__( diff --git a/libs/langchain/langchain/memory/chat_message_histories/sql.py b/libs/langchain/langchain/memory/chat_message_histories/sql.py index 85baf037e5..43fedd531d 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/sql.py +++ b/libs/langchain/langchain/memory/chat_message_histories/sql.py @@ -21,6 +21,7 @@ logger = logging.getLogger(__name__) def create_message_model(table_name, DynamicBase): # type: ignore """ Create a message model for a given table name. + Args: table_name: The name of the table to use. DynamicBase: The base class to use for the model. diff --git a/libs/langchain/langchain/memory/chat_message_histories/zep.py b/libs/langchain/langchain/memory/chat_message_histories/zep.py index e468369ac8..30983677bd 100644 --- a/libs/langchain/langchain/memory/chat_message_histories/zep.py +++ b/libs/langchain/langchain/memory/chat_message_histories/zep.py @@ -20,7 +20,7 @@ logger = logging.getLogger(__name__) class ZepChatMessageHistory(BaseChatMessageHistory): - """A ChatMessageHistory implementation that uses Zep as a backend. + """Chat message history that uses Zep as a backend. Recommended usage:: diff --git a/libs/langchain/langchain/memory/combined.py b/libs/langchain/langchain/memory/combined.py index a5cc1d4057..1c46c184d4 100644 --- a/libs/langchain/langchain/memory/combined.py +++ b/libs/langchain/langchain/memory/combined.py @@ -8,7 +8,7 @@ from langchain.schema import BaseMemory class CombinedMemory(BaseMemory): - """Class for combining multiple memories' data together.""" + """Combining multiple memories' data together.""" memories: List[BaseMemory] """For tracking all the memories that should be accessed.""" diff --git a/libs/langchain/langchain/memory/entity.py b/libs/langchain/langchain/memory/entity.py index 68be77a8dd..54b9574b0d 100644 --- a/libs/langchain/langchain/memory/entity.py +++ b/libs/langchain/langchain/memory/entity.py @@ -21,6 +21,8 @@ logger = logging.getLogger(__name__) class BaseEntityStore(BaseModel, ABC): + """Abstract base class for Entity store.""" + @abstractmethod def get(self, key: str, default: Optional[str] = None) -> Optional[str]: """Get entity value from store.""" @@ -48,7 +50,7 @@ class BaseEntityStore(BaseModel, ABC): class InMemoryEntityStore(BaseEntityStore): - """Basic in-memory entity store.""" + """In-memory Entity store.""" store: Dict[str, Optional[str]] = {} @@ -69,7 +71,9 @@ class InMemoryEntityStore(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. """ @@ -245,7 +249,7 @@ class ConversationEntityMemory(BaseChatMemory): """Entity extractor & summarizer memory. 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, SQLite, or other entity store. """ diff --git a/libs/langchain/langchain/memory/kg.py b/libs/langchain/langchain/memory/kg.py index dc1019fc12..400ed34987 100644 --- a/libs/langchain/langchain/memory/kg.py +++ b/libs/langchain/langchain/memory/kg.py @@ -17,7 +17,7 @@ from langchain.schema.messages import BaseMessage, SystemMessage, get_buffer_str class ConversationKGMemory(BaseChatMemory): - """Knowledge graph memory for storing conversation memory. + """Knowledge graph conversation memory. Integrates with external knowledge graph to store and retrieve information about knowledge triples in the conversation. diff --git a/libs/langchain/langchain/memory/motorhead_memory.py b/libs/langchain/langchain/memory/motorhead_memory.py index f84ebe0f1f..30dfbb7cea 100644 --- a/libs/langchain/langchain/memory/motorhead_memory.py +++ b/libs/langchain/langchain/memory/motorhead_memory.py @@ -10,6 +10,8 @@ MANAGED_URL = "https://api.getmetal.io/v1/motorhead" class MotorheadMemory(BaseChatMemory): + """Chat message memory backed by Motorhead service.""" + url: str = MANAGED_URL timeout = 3000 memory_key = "history" diff --git a/libs/langchain/langchain/memory/simple.py b/libs/langchain/langchain/memory/simple.py index c30f70240d..00caa9d5b2 100644 --- a/libs/langchain/langchain/memory/simple.py +++ b/libs/langchain/langchain/memory/simple.py @@ -4,7 +4,7 @@ from langchain.schema import 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. """ diff --git a/libs/langchain/langchain/memory/summary.py b/libs/langchain/langchain/memory/summary.py index 449a4361fe..afa0f9c8ef 100644 --- a/libs/langchain/langchain/memory/summary.py +++ b/libs/langchain/langchain/memory/summary.py @@ -16,6 +16,8 @@ from langchain.schema.messages import BaseMessage, SystemMessage, get_buffer_str class SummarizerMixin(BaseModel): + """Mixin for summarizer.""" + human_prefix: str = "Human" ai_prefix: str = "AI" llm: BaseLanguageModel @@ -36,7 +38,7 @@ class SummarizerMixin(BaseModel): class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin): - """Conversation summarizer to memory.""" + """Conversation summarizer to chat memory.""" buffer: str = "" memory_key: str = "history" #: :meta private: diff --git a/libs/langchain/langchain/memory/token_buffer.py b/libs/langchain/langchain/memory/token_buffer.py index 80f76c3b76..a964f2f6b4 100644 --- a/libs/langchain/langchain/memory/token_buffer.py +++ b/libs/langchain/langchain/memory/token_buffer.py @@ -6,7 +6,7 @@ from langchain.schema.messages import BaseMessage, get_buffer_string class ConversationTokenBufferMemory(BaseChatMemory): - """Buffer for storing conversation memory.""" + """Conversation chat memory with token limit.""" human_prefix: str = "Human" ai_prefix: str = "AI" diff --git a/libs/langchain/langchain/memory/vectorstore.py b/libs/langchain/langchain/memory/vectorstore.py index 7c6772fda6..41f0d8c671 100644 --- a/libs/langchain/langchain/memory/vectorstore.py +++ b/libs/langchain/langchain/memory/vectorstore.py @@ -11,7 +11,7 @@ from langchain.vectorstores.base import VectorStoreRetriever class VectorStoreRetrieverMemory(BaseMemory): - """Class for a VectorStore-backed memory object.""" + """VectorStoreRetriever-backed memory.""" retriever: VectorStoreRetriever = Field(exclude=True) """VectorStoreRetriever object to connect to.""" diff --git a/libs/langchain/langchain/memory/zep_memory.py b/libs/langchain/langchain/memory/zep_memory.py index 8e56eb4773..6e2a7ec823 100644 --- a/libs/langchain/langchain/memory/zep_memory.py +++ b/libs/langchain/langchain/memory/zep_memory.py @@ -7,7 +7,7 @@ from langchain.memory.chat_message_histories import ZepChatMessageHistory 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 histories is configurable. See the Zep documentation for more details. diff --git a/libs/langchain/langchain/schema/memory.py b/libs/langchain/langchain/schema/memory.py index 14a07bb379..46da7504b2 100644 --- a/libs/langchain/langchain/schema/memory.py +++ b/libs/langchain/langchain/schema/memory.py @@ -8,7 +8,7 @@ from langchain.schema.messages import AIMessage, BaseMessage, HumanMessage 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 past executions of a Chain and inject that information into the inputs of