Tony20100/langchain_full huggingface dataset:
langchain_community.chat_message_histories.zep_cloud.ZepCloudChatMessageHistory¶
class langchain_community.chat_message_histories.zep_cloud.ZepCloudChatMessageHistory(session_id: str, api_key: str, *, memory_type: Optional[MemoryGetRequestMemoryType] = None, lastn: Optional[int] = None, ai_prefix: Optional[str] = None, human_prefix: Optional[str] = None, summary_instruction: Optional[str] = None)[source]¶
Chat message history that uses Zep Cloud as a backend.
Recommended usage:
# Set up Zep Chat History
zep_chat_history = ZepChatMessageHistory(
    session_id=session_id,
    api_key=<your_api_key>,
)
# Use a standard ConversationBufferMemory to encapsulate the Zep chat history
memory = ConversationBufferMemory(
    memory_key="chat_history", chat_memory=zep_chat_history
)
Zep - Recall, understand, and extract data from chat histories.
Power personalized AI experiences.
Zep is a long-term memory service for AI Assistant apps.
With Zep, you can provide AI assistants with the
ability to recall past conversations,
no matter how distant,
while also reducing hallucinations, latency, and cost.
see Zep Cloud Docs: https://help.getzep.com
This class is a thin wrapper around the zep-python package. Additional
Zep functionality is exposed via the zep_summary, zep_messages and zep_facts
properties.
For more information on the zep-python package, see:
https://github.com/getzep/zep-python
Attributes
messages
Retrieve messages from Zep memory
zep_facts
Retrieve conversation facts from Zep memory
zep_messages
Retrieve summary from Zep memory
zep_summary
Retrieve summary from Zep memory
Methods
Retrieve summary from Zep memory
zep_summary
Retrieve summary from Zep memory
Methods
__init__(session_id, api_key, *[, ...])
aadd_messages(messages)
Append the messages to the Zep memory history asynchronously
aclear()
Clear session memory from Zep asynchronously.
add_ai_message(message[, metadata])
Convenience method for adding an AI message string to the store.
add_message(message[, metadata])
Append the message to the Zep memory history
add_messages(messages)
Append the messages to the Zep memory history
add_user_message(message[, metadata])
Convenience method for adding a human message string to the store.
aget_messages()
Async version of getting messages.
clear()
Clear session memory from Zep.
search(query[, metadata, search_scope, ...])
Search Zep memory for messages matching the query
Parameters
session_id (str) – 
api_key (str) – 
memory_type (Optional[MemoryGetRequestMemoryType]) – 
lastn (Optional[int]) – 
ai_prefix (Optional[str]) – 
human_prefix (Optional[str]) – 
summary_instruction (Optional[str]) – 
__init__(session_id: str, api_key: str, *, memory_type: Optional[MemoryGetRequestMemoryType] = None, lastn: Optional[int] = None, ai_prefix: Optional[str] = None, human_prefix: Optional[str] = None, summary_instruction: Optional[str] = None) → None[source]¶
Parameters
session_id (str) – 
api_key (str) – 
memory_type (Optional[MemoryGetRequestMemoryType]) – 
lastn (Optional[int]) – 
ai_prefix (Optional[str]) – 
human_prefix (Optional[str]) – 
summary_instruction (Optional[str]) –
human_prefix (Optional[str]) – 
summary_instruction (Optional[str]) – 
Return type
None
async aadd_messages(messages: Sequence[BaseMessage]) → None[source]¶
Append the messages to the Zep memory history asynchronously
Parameters
messages (Sequence[BaseMessage]) – 
Return type
None
async aclear() → None[source]¶
Clear session memory from Zep asynchronously.
Note that Zep is long-term storage for memory and this is not advised
unless you have specific data retention requirements.
Return type
None
add_ai_message(message: str, metadata: Optional[Dict[str, Any]] = None) → None[source]¶
Convenience method for adding an AI message string to the store.
Parameters
message (str) – The string contents of an AI message.
metadata (Optional[Dict[str, Any]]) – Optional metadata to attach to the message.
Return type
None
add_message(message: BaseMessage, metadata: Optional[Dict[str, Any]] = None) → None[source]¶
Append the message to the Zep memory history
Parameters
message (BaseMessage) – 
metadata (Optional[Dict[str, Any]]) – 
Return type
None
add_messages(messages: Sequence[BaseMessage]) → None[source]¶
Append the messages to the Zep memory history
Parameters
messages (Sequence[BaseMessage]) – 
Return type
None
add_user_message(message: str, metadata: Optional[Dict[str, Any]] = None) → None[source]¶
Convenience method for adding a human message string to the store.
Parameters
message (str) – The string contents of a human message.
metadata (Optional[Dict[str, Any]]) – Optional metadata to attach to the message.
Return type
None
async aget_messages() → List[BaseMessage]¶
Async version of getting messages.
async aget_messages() → List[BaseMessage]¶
Async version of getting messages.
Can over-ride this method to provide an efficient async implementation.
In general, fetching messages may involve IO to the underlying
persistence layer.
Return type
List[BaseMessage]
clear() → None[source]¶
Clear session memory from Zep. Note that Zep is long-term storage for memory
and this is not advised unless you have specific data retention requirements.
Return type
None
search(query: str, metadata: Optional[Dict] = None, search_scope: SearchScope = 'messages', search_type: SearchType = 'similarity', mmr_lambda: Optional[float] = None, limit: Optional[int] = None) → List[MemorySearchResult][source]¶
Search Zep memory for messages matching the query
Parameters
query (str) – 
metadata (Optional[Dict]) – 
search_scope (SearchScope) – 
search_type (SearchType) – 
mmr_lambda (Optional[float]) – 
limit (Optional[int]) – 
Return type
List[MemorySearchResult]
Examples using ZepCloudChatMessageHistory¶
Zep
ZepCloudChatMessageHistory
langchain_community.chat_message_histories.sql.BaseMessageConverter¶
class langchain_community.chat_message_histories.sql.BaseMessageConverter[source]¶
Convert BaseMessage to the SQLAlchemy model.
Methods
__init__()
from_sql_model(sql_message)
Convert a SQLAlchemy model to a BaseMessage instance.
get_sql_model_class()
Get the SQLAlchemy model class.
to_sql_model(message, session_id)
Convert a BaseMessage instance to a SQLAlchemy model.
__init__()¶
abstract from_sql_model(sql_message: Any) → BaseMessage[source]¶
Convert a SQLAlchemy model to a BaseMessage instance.
Parameters
sql_message (Any) – 
Return type
BaseMessage
abstract get_sql_model_class() → Any[source]¶
Get the SQLAlchemy model class.
Return type
Any
abstract to_sql_model(message: BaseMessage, session_id: str) → Any[source]¶
Convert a BaseMessage instance to a SQLAlchemy model.
Parameters
message (BaseMessage) – 
session_id (str) – 
Return type
Any
langchain_postgres.chat_message_histories.PostgresChatMessageHistory¶
class langchain_postgres.chat_message_histories.PostgresChatMessageHistory(table_name: str, session_id: str, /, *, sync_connection: Optional[Connection] = None, async_connection: Optional[AsyncConnection] = None)[source]¶
Client for persisting chat message history in a Postgres database,
This client provides support for both sync and async via psycopg >=3.
The client can create schema in the database and provides methods to
add messages, get messages, and clear the chat message history.
The schema has the following columns:
id: A serial primary key.
session_id: The session ID for the chat message history.
message: The JSONB message content.
created_at: The timestamp of when the message was created.
Messages are retrieved for a given session_id and are sorted by
the id (which should be increasing monotonically), and correspond
to the order in which the messages were added to the history.
The “created_at” column is not returned by the interface, but
has been added for the schema so the information is available in the database.
A session_id can be used to separate different chat histories in the same table,
the session_id should be provided when initializing the client.
This chat history client takes in a psycopg connection object (either
Connection or AsyncConnection) and uses it to interact with the database.
This design allows to reuse the underlying connection object across
multiple instantiations of this class, making instantiation fast.
This chat history client is designed for prototyping applications that
involve chat and are based on Postgres.
As your application grows, you will likely need to extend the schema to
handle more complex queries. For example, a chat application
may involve multiple tables like a user table, a table for storing
may involve multiple tables like a user table, a table for storing
chat sessions / conversations, and this table for storing chat messages
for a given session. The application will require access to additional
endpoints like deleting messages by user id, listing conversations by
user id or ordering them based on last message time, etc.
Feel free to adapt this implementation to suit your application’s needs.
Parameters
session_id (str) – The session ID to use for the chat message history
table_name (str) – The name of the database table to use
sync_connection (Optional[psycopg.Connection]) – An existing psycopg connection instance
async_connection (Optional[psycopg.AsyncConnection]) – An existing psycopg async connection instance
Usage:
Use the create_tables or acreate_tables method to set up the table
schema in the database.
Initialize the class with the appropriate session ID, table name,
and database connection.
Add messages to the database using add_messages or aadd_messages.
Retrieve messages with get_messages or aget_messages.
Clear the session history with clear or aclear when needed.
Note
At least one of sync_connection or async_connection must be provided.
Examples:
import uuid
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_postgres import PostgresChatMessageHistory
import psycopg
# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
sync_connection = psycopg2.connect(conn_info)
# Create the table schema (only needs to be done once)
table_name = "chat_history"
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
session_id = str(uuid.uuid4())
# Initialize the chat history manager
chat_history = PostgresChatMessageHistory(
    table_name,
    session_id,
    sync_connection=sync_connection
)
# Add messages to the chat history
sync_connection=sync_connection
)
# Add messages to the chat history
chat_history.add_messages([
    SystemMessage(content="Meow"),
    AIMessage(content="woof"),
    HumanMessage(content="bark"),
])
print(chat_history.messages)
Attributes
messages
The abstraction required a property.
Methods
__init__(table_name, session_id, /, *[, ...])
Client for persisting chat message history in a Postgres database,
aadd_messages(messages)
Add messages to the chat message history.
aclear()
Clear the chat message history for the GIVEN session.
acreate_tables(connection, table_name, /)
Create the table schema in the database and create relevant indexes.
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Add a Message object to the store.
add_messages(messages)
Add messages to the chat message history.
add_user_message(message)
Convenience method for adding a human message string to the store.
adrop_table(connection, table_name, /)
Delete the table schema in the database.
aget_messages()
Retrieve messages from the chat message history.
clear()
Clear the chat message history for the GIVEN session.
create_tables(connection, table_name, /)
Create the table schema in the database and create relevant indexes.
drop_table(connection, table_name, /)
Delete the table schema in the database.
get_messages()
Retrieve messages from the chat message history.
__init__(table_name: str, session_id: str, /, *, sync_connection: Optional[Connection] = None, async_connection: Optional[AsyncConnection] = None) → None[source]¶
Client for persisting chat message history in a Postgres database,
Client for persisting chat message history in a Postgres database,
This client provides support for both sync and async via psycopg >=3.
The client can create schema in the database and provides methods to
add messages, get messages, and clear the chat message history.
The schema has the following columns:
id: A serial primary key.
session_id: The session ID for the chat message history.
message: The JSONB message content.
created_at: The timestamp of when the message was created.
Messages are retrieved for a given session_id and are sorted by
the id (which should be increasing monotonically), and correspond
to the order in which the messages were added to the history.
The “created_at” column is not returned by the interface, but
has been added for the schema so the information is available in the database.
A session_id can be used to separate different chat histories in the same table,
the session_id should be provided when initializing the client.
This chat history client takes in a psycopg connection object (either
Connection or AsyncConnection) and uses it to interact with the database.
This design allows to reuse the underlying connection object across
multiple instantiations of this class, making instantiation fast.
This chat history client is designed for prototyping applications that
involve chat and are based on Postgres.
As your application grows, you will likely need to extend the schema to
handle more complex queries. For example, a chat application
may involve multiple tables like a user table, a table for storing
chat sessions / conversations, and this table for storing chat messages
for a given session. The application will require access to additional
endpoints like deleting messages by user id, listing conversations by
user id or ordering them based on last message time, etc.
Feel free to adapt this implementation to suit your application’s needs.
Parameters
Feel free to adapt this implementation to suit your application’s needs.
Parameters
session_id (str) – The session ID to use for the chat message history
table_name (str) – The name of the database table to use
sync_connection (Optional[Connection]) – An existing psycopg connection instance
async_connection (Optional[AsyncConnection]) – An existing psycopg async connection instance
Return type
None
Usage:
Use the create_tables or acreate_tables method to set up the table
schema in the database.
Initialize the class with the appropriate session ID, table name,
and database connection.
Add messages to the database using add_messages or aadd_messages.
Retrieve messages with get_messages or aget_messages.
Clear the session history with clear or aclear when needed.
Note
At least one of sync_connection or async_connection must be provided.
Examples:
import uuid
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
from langchain_postgres import PostgresChatMessageHistory
import psycopg
# Establish a synchronous connection to the database
# (or use psycopg.AsyncConnection for async)
sync_connection = psycopg2.connect(conn_info)
# Create the table schema (only needs to be done once)
table_name = "chat_history"
PostgresChatMessageHistory.create_tables(sync_connection, table_name)
session_id = str(uuid.uuid4())
# Initialize the chat history manager
chat_history = PostgresChatMessageHistory(
    table_name,
    session_id,
    sync_connection=sync_connection
)
# Add messages to the chat history
chat_history.add_messages([
    SystemMessage(content="Meow"),
    AIMessage(content="woof"),
    HumanMessage(content="bark"),
])
print(chat_history.messages)
async aadd_messages(messages: Sequence[BaseMessage]) → None[source]¶
async aadd_messages(messages: Sequence[BaseMessage]) → None[source]¶
Add messages to the chat message history.
Parameters
messages (Sequence[BaseMessage]) – 
Return type
None
async aclear() → None[source]¶
Clear the chat message history for the GIVEN session.
Return type
None
async static acreate_tables(connection: AsyncConnection, table_name: str, /) → None[source]¶
Create the table schema in the database and create relevant indexes.
Parameters
connection (AsyncConnection) – 
table_name (str) – 
Return type
None
add_ai_message(message: Union[AIMessage, str]) → None¶
Convenience method for adding an AI message string to the store.
Please note that this is a convenience method. Code should favor the bulk
add_messages interface instead to save on round-trips to the underlying
persistence layer.
This method may be deprecated in a future release.
Parameters
message (Union[AIMessage, str]) – The AI message to add.
Return type
None
add_message(message: BaseMessage) → None¶
Add a Message object to the store.
Parameters
message (BaseMessage) – A BaseMessage object to store.
Return type
None
add_messages(messages: Sequence[BaseMessage]) → None[source]¶
Add messages to the chat message history.
Parameters
messages (Sequence[BaseMessage]) – 
Return type
None
add_user_message(message: Union[HumanMessage, str]) → None¶
Convenience method for adding a human message string to the store.
Please note that this is a convenience method. Code should favor the
bulk add_messages interface instead to save on round-trips to the underlying
persistence layer.
This method may be deprecated in a future release.
Parameters
persistence layer.
This method may be deprecated in a future release.
Parameters
message (Union[HumanMessage, str]) – The human message to add
Return type
None
async static adrop_table(connection: AsyncConnection, table_name: str, /) → None[source]¶
Delete the table schema in the database.
Warning
This will delete the given table from the database including
all the database in the table and the schema of the table.
Parameters
connection (AsyncConnection) – Async database connection.
table_name (str) – The name of the table to create.
Return type
None
async aget_messages() → List[BaseMessage][source]¶
Retrieve messages from the chat message history.
Return type
List[BaseMessage]
clear() → None[source]¶
Clear the chat message history for the GIVEN session.
Return type
None
static create_tables(connection: Connection, table_name: str, /) → None[source]¶
Create the table schema in the database and create relevant indexes.
Parameters
connection (Connection) – 
table_name (str) – 
Return type
None
static drop_table(connection: Connection, table_name: str, /) → None[source]¶
Delete the table schema in the database.
Warning
This will delete the given table from the database including
all the database in the table and the schema of the table.
Parameters
connection (Connection) – The database connection.
table_name (str) – The name of the table to create.
Return type
None
get_messages() → List[BaseMessage][source]¶
Retrieve messages from the chat message history.
Return type
List[BaseMessage]
Examples using PostgresChatMessageHistory¶
Postgres
langchain_community.chat_message_histories.sql.DefaultMessageConverter¶
class langchain_community.chat_message_histories.sql.DefaultMessageConverter(table_name: str)[source]¶
The default message converter for SQLChatMessageHistory.
Methods
__init__(table_name)
from_sql_model(sql_message)
Convert a SQLAlchemy model to a BaseMessage instance.
get_sql_model_class()
Get the SQLAlchemy model class.
to_sql_model(message, session_id)
Convert a BaseMessage instance to a SQLAlchemy model.
Parameters
table_name (str) – 
__init__(table_name: str)[source]¶
Parameters
table_name (str) – 
from_sql_model(sql_message: Any) → BaseMessage[source]¶
Convert a SQLAlchemy model to a BaseMessage instance.
Parameters
sql_message (Any) – 
Return type
BaseMessage
get_sql_model_class() → Any[source]¶
Get the SQLAlchemy model class.
Return type
Any
to_sql_model(message: BaseMessage, session_id: str) → Any[source]¶
Convert a BaseMessage instance to a SQLAlchemy model.
Parameters
message (BaseMessage) – 
session_id (str) – 
Return type
Any
langchain_community.chat_message_histories.sql.create_message_model¶
langchain_community.chat_message_histories.sql.create_message_model(table_name: str, DynamicBase: Any) → Any[source]¶
Create a message model for a given table name.
Parameters
table_name (str) – The name of the table to use.
DynamicBase (Any) – The base class to use for the model.
Returns
The model class.
Return type
Any
langchain_community.chat_message_histories.postgres.PostgresChatMessageHistory¶
class langchain_community.chat_message_histories.postgres.PostgresChatMessageHistory(session_id: str, connection_string: str = 'postgresql://postgres:mypassword@localhost/chat_history', table_name: str = 'message_store')[source]¶
[Deprecated] Chat message history stored in a Postgres database.
DEPRECATED: This class is deprecated and will be removed in a future version.
Use the PostgresChatMessageHistory implementation in langchain_postgres.
Notes
Deprecated since version 0.0.31: This class is deprecated and will be removed in a future version. You can swap to using the PostgresChatMessageHistory implementation in langchain_postgres. Please do not submit further PRs to this class.See https://github.com/langchain-ai/langchain-postgres Use from langchain_postgres import PostgresChatMessageHistory; instead.
Attributes
messages
Retrieve the messages from PostgreSQL
Methods
__init__(session_id[, connection_string, ...])
aadd_messages(messages)
Add a list of messages.
aclear()
Remove all messages from the store
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Append the message to the record in PostgreSQL
add_messages(messages)
Add a list of messages.
add_user_message(message)
Convenience method for adding a human message string to the store.
aget_messages()
Async version of getting messages.
clear()
Clear session memory from PostgreSQL
Parameters
session_id (str) – 
connection_string (str) – 
table_name (str) – 
__init__(session_id: str, connection_string: str = 'postgresql://postgres:mypassword@localhost/chat_history', table_name: str = 'message_store')[source]¶
Parameters
Parameters
session_id (str) – 
connection_string (str) – 
table_name (str) – 
async aadd_messages(messages: Sequence[BaseMessage]) → None¶
Add a list of messages.
Parameters
messages (Sequence[BaseMessage]) – A list of BaseMessage objects to store.
Return type
None
async aclear() → None¶
Remove all messages from the store
Return type
None
add_ai_message(message: Union[AIMessage, str]) → None¶
Convenience method for adding an AI message string to the store.
Please note that this is a convenience method. Code should favor the bulk
add_messages interface instead to save on round-trips to the underlying
persistence layer.
This method may be deprecated in a future release.
Parameters
message (Union[AIMessage, str]) – The AI message to add.
Return type
None
add_message(message: BaseMessage) → None[source]¶
Append the message to the record in PostgreSQL
Parameters
message (BaseMessage) – 
Return type
None
add_messages(messages: Sequence[BaseMessage]) → None¶
Add a list of messages.
Implementations should over-ride this method to handle bulk addition of messages
in an efficient manner to avoid unnecessary round-trips to the underlying store.
Parameters
messages (Sequence[BaseMessage]) – A list of BaseMessage objects to store.
Return type
None
add_user_message(message: Union[HumanMessage, str]) → None¶
Convenience method for adding a human message string to the store.
Please note that this is a convenience method. Code should favor the
bulk add_messages interface instead to save on round-trips to the underlying
persistence layer.
This method may be deprecated in a future release.
Parameters
message (Union[HumanMessage, str]) – The human message to add
Parameters
message (Union[HumanMessage, str]) – The human message to add
Return type
None
async aget_messages() → List[BaseMessage]¶
Async version of getting messages.
Can over-ride this method to provide an efficient async implementation.
In general, fetching messages may involve IO to the underlying
persistence layer.
Return type
List[BaseMessage]
clear() → None[source]¶
Clear session memory from PostgreSQL
Return type
None
Examples using PostgresChatMessageHistory¶
Postgres