mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 12:48:12 +00:00
core[patch],langchain[patch]: deprecate memory and entity abstractions and implementations (#26717)
This PR deprecates the old memory, entity abstractions and implementations
This commit is contained in:
parent
19ce95d3c9
commit
91f4711e53
@ -1,11 +1,8 @@
|
|||||||
"""**Memory** maintains Chain state, incorporating context from past runs.
|
"""**Memory** maintains Chain state, incorporating context from past runs.
|
||||||
|
|
||||||
**Class hierarchy for Memory:**
|
This module contains memory abstractions from LangChain v0.0.x.
|
||||||
|
|
||||||
.. code-block::
|
|
||||||
|
|
||||||
BaseMemory --> <name>Memory --> <name>Memory # Examples: BaseChatMemory -> MotorheadMemory
|
|
||||||
|
|
||||||
|
These abstractions are now deprecated and will be removed in LangChain v1.0.0.
|
||||||
""" # noqa: E501
|
""" # noqa: E501
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@ -15,10 +12,19 @@ from typing import Any
|
|||||||
|
|
||||||
from pydantic import ConfigDict
|
from pydantic import ConfigDict
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.load.serializable import Serializable
|
from langchain_core.load.serializable import Serializable
|
||||||
from langchain_core.runnables import run_in_executor
|
from langchain_core.runnables import run_in_executor
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class BaseMemory(Serializable, ABC):
|
class BaseMemory(Serializable, ABC):
|
||||||
"""Abstract base class for memory in Chains.
|
"""Abstract base class for memory in Chains.
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.messages import BaseMessage, get_buffer_string
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
||||||
from langchain_core.utils import pre_init
|
from langchain_core.utils import pre_init
|
||||||
|
|
||||||
@ -7,8 +8,23 @@ from langchain.memory.chat_memory import BaseChatMemory, BaseMemory
|
|||||||
from langchain.memory.utils import get_prompt_input_key
|
from langchain.memory.utils import get_prompt_input_key
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationBufferMemory(BaseChatMemory):
|
class ConversationBufferMemory(BaseChatMemory):
|
||||||
"""Buffer for storing conversation memory."""
|
"""A basic memory implementation that simply stores the conversation history.
|
||||||
|
|
||||||
|
This stores the entire conversation history in memory without any
|
||||||
|
additional processing.
|
||||||
|
|
||||||
|
Note that additional processing may be required in some situations when the
|
||||||
|
conversation history is too large to fit in the context window of the model.
|
||||||
|
"""
|
||||||
|
|
||||||
human_prefix: str = "Human"
|
human_prefix: str = "Human"
|
||||||
ai_prefix: str = "AI"
|
ai_prefix: str = "AI"
|
||||||
@ -71,8 +87,26 @@ class ConversationBufferMemory(BaseChatMemory):
|
|||||||
return {self.memory_key: buffer}
|
return {self.memory_key: buffer}
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationStringBufferMemory(BaseMemory):
|
class ConversationStringBufferMemory(BaseMemory):
|
||||||
"""Buffer for storing conversation memory."""
|
"""A basic memory implementation that simply stores the conversation history.
|
||||||
|
|
||||||
|
This stores the entire conversation history in memory without any
|
||||||
|
additional processing.
|
||||||
|
|
||||||
|
Equivalent to ConversationBufferMemory but tailored more specifically
|
||||||
|
for string-based conversations rather than chat models.
|
||||||
|
|
||||||
|
Note that additional processing may be required in some situations when the
|
||||||
|
conversation history is too large to fit in the context window of the model.
|
||||||
|
"""
|
||||||
|
|
||||||
human_prefix: str = "Human"
|
human_prefix: str = "Human"
|
||||||
ai_prefix: str = "AI"
|
ai_prefix: str = "AI"
|
||||||
|
@ -1,12 +1,25 @@
|
|||||||
from typing import Any, Dict, List, Union
|
from typing import Any, Dict, List, Union
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.messages import BaseMessage, get_buffer_string
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
||||||
|
|
||||||
from langchain.memory.chat_memory import BaseChatMemory
|
from langchain.memory.chat_memory import BaseChatMemory
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationBufferWindowMemory(BaseChatMemory):
|
class ConversationBufferWindowMemory(BaseChatMemory):
|
||||||
"""Buffer for storing conversation memory inside a limited size window."""
|
"""Use to keep track of the last k turns of a conversation.
|
||||||
|
|
||||||
|
If the number of messages in the conversation is more than the maximum number
|
||||||
|
of messages to keep, the oldest messages are dropped.
|
||||||
|
"""
|
||||||
|
|
||||||
human_prefix: str = "Human"
|
human_prefix: str = "Human"
|
||||||
ai_prefix: str = "AI"
|
ai_prefix: str = "AI"
|
||||||
|
@ -2,6 +2,7 @@ import warnings
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
from typing import Any, Dict, Optional, Tuple
|
from typing import Any, Dict, Optional, Tuple
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.chat_history import (
|
from langchain_core.chat_history import (
|
||||||
BaseChatMessageHistory,
|
BaseChatMessageHistory,
|
||||||
InMemoryChatMessageHistory,
|
InMemoryChatMessageHistory,
|
||||||
@ -13,8 +14,24 @@ from pydantic import Field
|
|||||||
from langchain.memory.utils import get_prompt_input_key
|
from langchain.memory.utils import get_prompt_input_key
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class BaseChatMemory(BaseMemory, ABC):
|
class BaseChatMemory(BaseMemory, ABC):
|
||||||
"""Abstract base class for chat memory."""
|
"""Abstract base class for chat memory.
|
||||||
|
|
||||||
|
**ATTENTION** This abstraction was created prior to when chat models had
|
||||||
|
native tool calling capabilities.
|
||||||
|
It does **NOT** support native tool calling capabilities for chat models and
|
||||||
|
will fail SILENTLY if used with a chat model that has native tool calling.
|
||||||
|
|
||||||
|
DO NOT USE THIS ABSTRACTION FOR NEW CODE.
|
||||||
|
"""
|
||||||
|
|
||||||
chat_memory: BaseChatMessageHistory = Field(
|
chat_memory: BaseChatMessageHistory = Field(
|
||||||
default_factory=InMemoryChatMessageHistory
|
default_factory=InMemoryChatMessageHistory
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
"""Deprecated as of LangChain v0.3.4 and will be removed in LangChain v1.0.0."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
from typing import Any, Dict, Iterable, List, Optional
|
from typing import Any, Dict, Iterable, List, Optional
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.language_models import BaseLanguageModel
|
from langchain_core.language_models import BaseLanguageModel
|
||||||
from langchain_core.messages import BaseMessage, get_buffer_string
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
||||||
from langchain_core.prompts import BasePromptTemplate
|
from langchain_core.prompts import BasePromptTemplate
|
||||||
@ -19,6 +22,14 @@ from langchain.memory.utils import get_prompt_input_key
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class BaseEntityStore(BaseModel, ABC):
|
class BaseEntityStore(BaseModel, ABC):
|
||||||
"""Abstract base class for Entity store."""
|
"""Abstract base class for Entity store."""
|
||||||
|
|
||||||
@ -48,6 +59,14 @@ class BaseEntityStore(BaseModel, ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class InMemoryEntityStore(BaseEntityStore):
|
class InMemoryEntityStore(BaseEntityStore):
|
||||||
"""In-memory Entity store."""
|
"""In-memory Entity store."""
|
||||||
|
|
||||||
@ -69,6 +88,14 @@ class InMemoryEntityStore(BaseEntityStore):
|
|||||||
return self.store.clear()
|
return self.store.clear()
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class UpstashRedisEntityStore(BaseEntityStore):
|
class UpstashRedisEntityStore(BaseEntityStore):
|
||||||
"""Upstash Redis backed Entity store.
|
"""Upstash Redis backed Entity store.
|
||||||
|
|
||||||
@ -147,6 +174,14 @@ class UpstashRedisEntityStore(BaseEntityStore):
|
|||||||
scan_and_delete(cursor)
|
scan_and_delete(cursor)
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class RedisEntityStore(BaseEntityStore):
|
class RedisEntityStore(BaseEntityStore):
|
||||||
"""Redis-backed Entity store.
|
"""Redis-backed Entity store.
|
||||||
|
|
||||||
@ -238,6 +273,14 @@ class RedisEntityStore(BaseEntityStore):
|
|||||||
self.redis_client.delete(*keybatch)
|
self.redis_client.delete(*keybatch)
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class SQLiteEntityStore(BaseEntityStore):
|
class SQLiteEntityStore(BaseEntityStore):
|
||||||
"""SQLite-backed Entity store"""
|
"""SQLite-backed Entity store"""
|
||||||
|
|
||||||
@ -335,6 +378,14 @@ class SQLiteEntityStore(BaseEntityStore):
|
|||||||
self.conn.execute(query)
|
self.conn.execute(query)
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationEntityMemory(BaseChatMemory):
|
class ConversationEntityMemory(BaseChatMemory):
|
||||||
"""Entity extractor & summarizer memory.
|
"""Entity extractor & summarizer memory.
|
||||||
|
|
||||||
|
@ -57,8 +57,21 @@ class SummarizerMixin(BaseModel):
|
|||||||
return await chain.apredict(summary=existing_summary, new_lines=new_lines)
|
return await chain.apredict(summary=existing_summary, new_lines=new_lines)
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin):
|
class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin):
|
||||||
"""Conversation summarizer to chat memory."""
|
"""Continually summarizes the conversation history.
|
||||||
|
|
||||||
|
The summary is updated after each conversation turn.
|
||||||
|
The implementations returns a summary of the conversation history which
|
||||||
|
can be used to provide context to the model.
|
||||||
|
"""
|
||||||
|
|
||||||
buffer: str = ""
|
buffer: str = ""
|
||||||
memory_key: str = "history" #: :meta private:
|
memory_key: str = "history" #: :meta private:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from typing import Any, Dict, List, Union
|
from typing import Any, Dict, List, Union
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.messages import BaseMessage, get_buffer_string
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
||||||
from langchain_core.utils import pre_init
|
from langchain_core.utils import pre_init
|
||||||
|
|
||||||
@ -7,8 +8,21 @@ from langchain.memory.chat_memory import BaseChatMemory
|
|||||||
from langchain.memory.summary import SummarizerMixin
|
from langchain.memory.summary import SummarizerMixin
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationSummaryBufferMemory(BaseChatMemory, SummarizerMixin):
|
class ConversationSummaryBufferMemory(BaseChatMemory, SummarizerMixin):
|
||||||
"""Buffer with summarizer for storing conversation memory."""
|
"""Buffer with summarizer for storing conversation memory.
|
||||||
|
|
||||||
|
Provides a running summary of the conversation together with the most recent
|
||||||
|
messages in the conversation under the constraint that the total number of
|
||||||
|
tokens in the conversation does not exceed a certain limit.
|
||||||
|
"""
|
||||||
|
|
||||||
max_token_limit: int = 2000
|
max_token_limit: int = 2000
|
||||||
moving_summary_buffer: str = ""
|
moving_summary_buffer: str = ""
|
||||||
|
@ -1,13 +1,26 @@
|
|||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.language_models import BaseLanguageModel
|
from langchain_core.language_models import BaseLanguageModel
|
||||||
from langchain_core.messages import BaseMessage, get_buffer_string
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
||||||
|
|
||||||
from langchain.memory.chat_memory import BaseChatMemory
|
from langchain.memory.chat_memory import BaseChatMemory
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class ConversationTokenBufferMemory(BaseChatMemory):
|
class ConversationTokenBufferMemory(BaseChatMemory):
|
||||||
"""Conversation chat memory with token limit."""
|
"""Conversation chat memory with token limit.
|
||||||
|
|
||||||
|
Keeps only the most recent messages in the conversation under the constraint
|
||||||
|
that the total number of tokens in the conversation does not exceed a certain limit.
|
||||||
|
"""
|
||||||
|
|
||||||
human_prefix: str = "Human"
|
human_prefix: str = "Human"
|
||||||
ai_prefix: str = "AI"
|
ai_prefix: str = "AI"
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from typing import Any, Dict, List, Optional, Sequence, Union
|
from typing import Any, Dict, List, Optional, Sequence, Union
|
||||||
|
|
||||||
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.vectorstores import VectorStoreRetriever
|
from langchain_core.vectorstores import VectorStoreRetriever
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
@ -10,8 +11,18 @@ from langchain.memory.chat_memory import BaseMemory
|
|||||||
from langchain.memory.utils import get_prompt_input_key
|
from langchain.memory.utils import get_prompt_input_key
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated(
|
||||||
|
since="0.3.4",
|
||||||
|
removal="1.0.0",
|
||||||
|
message=(
|
||||||
|
"Please see the migration guide at: "
|
||||||
|
"https://python.langchain.com/docs/versions/migrating_memory/"
|
||||||
|
),
|
||||||
|
)
|
||||||
class VectorStoreRetrieverMemory(BaseMemory):
|
class VectorStoreRetrieverMemory(BaseMemory):
|
||||||
"""VectorStoreRetriever-backed memory."""
|
"""Store the conversation history in a vector store and retrieves the relevant
|
||||||
|
parts of past conversation based on the input.
|
||||||
|
"""
|
||||||
|
|
||||||
retriever: VectorStoreRetriever = Field(exclude=True)
|
retriever: VectorStoreRetriever = Field(exclude=True)
|
||||||
"""VectorStoreRetriever object to connect to."""
|
"""VectorStoreRetriever object to connect to."""
|
||||||
|
Loading…
Reference in New Issue
Block a user