viruPVS/langchain-docs huggingface dataset:
classmethod parse_obj(obj: Any) → Model¶
classmethod parse_raw(b: Union[str, bytes], *, content_type: unicode = None, encoding: unicode = 'utf8', proto: Protocol = None, allow_pickle: bool = False) → Model¶
classmethod schema(by_alias: bool = True, ref_template: unicode = '#/definitions/{model}') → DictStrAny¶
classmethod schema_json(*, by_alias: bool = True, ref_template: unicode = '#/definitions/{model}', **dumps_kwargs: Any) → unicode¶
classmethod update_forward_refs(**localns: Any) → None¶
Try to update ForwardRefs on fields based on this Model, globalns and localns.
classmethod validate(value: Any) → Model¶
Examples using ChatMessageHistory¶
Message Memory in Agent backed by a database
langchain_community.chat_message_histories.cassandra.CassandraChatMessageHistory¶
class langchain_community.chat_message_histories.cassandra.CassandraChatMessageHistory(session_id: str, session: Session, keyspace: str, table_name: str = 'message_store', ttl_seconds: Optional[int] = None)[source]¶
Chat message history that stores history in Cassandra.
Parameters
session_id – arbitrary key that is used to store the messages
of a single chat session.
session – a Cassandra Session object (an open DB connection)
keyspace – name of the keyspace to use.
table_name – name of the table to use.
ttl_seconds – time-to-live (seconds) for automatic expiration
of stored entries. None (default) for no expiration.
Attributes
messages
Retrieve all session messages from DB
Methods
__init__(session_id, session, keyspace[, ...])
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Write a message to the table
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from DB
__init__(session_id: str, session: Session, keyspace: str, table_name: str = 'message_store', ttl_seconds: Optional[int] = None) → None[source]¶
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Write a message to the table
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
langchain_community.chat_message_histories.cosmos_db.CosmosDBChatMessageHistory¶
class langchain_community.chat_message_histories.cosmos_db.CosmosDBChatMessageHistory(cosmos_endpoint: str, cosmos_database: str, cosmos_container: str, session_id: str, user_id: str, credential: Any = None, connection_string: Optional[str] = None, ttl: Optional[int] = None, cosmos_client_kwargs: Optional[dict] = None)[source]¶
Chat message history backed by Azure CosmosDB.
Initializes a new instance of the CosmosDBChatMessageHistory class.
Make sure to call prepare_cosmos or use the context manager to make
sure your database is ready.
Either a credential or a connection string must be provided.
Parameters
cosmos_endpoint – The connection endpoint for the Azure Cosmos DB account.
cosmos_database – The name of the database to use.
cosmos_container – The name of the container to use.
session_id – The session ID to use, can be overwritten while loading.
user_id – The user ID to use, can be overwritten while loading.
credential – The credential to use to authenticate to Azure Cosmos DB.
connection_string – The connection string to use to authenticate.
ttl – The time to live (in seconds) to use for documents in the container.
cosmos_client_kwargs – Additional kwargs to pass to the CosmosClient.
Attributes
messages
A list of Messages stored in-memory.
Methods
__init__(cosmos_endpoint, cosmos_database, ...)
Initializes a new instance of the CosmosDBChatMessageHistory class.
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Add a self-created message to the store
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Convenience method for adding a human message string to the store.
clear()
Clear session memory from this memory and cosmos.
load_messages()
Retrieve the messages from Cosmos
prepare_cosmos()
Prepare the CosmosDB client.
upsert_messages()
Update the cosmosdb item.
__init__(cosmos_endpoint: str, cosmos_database: str, cosmos_container: str, session_id: str, user_id: str, credential: Any = None, connection_string: Optional[str] = None, ttl: Optional[int] = None, cosmos_client_kwargs: Optional[dict] = None)[source]¶
Initializes a new instance of the CosmosDBChatMessageHistory class.
Make sure to call prepare_cosmos or use the context manager to make
sure your database is ready.
Either a credential or a connection string must be provided.
Parameters
cosmos_endpoint – The connection endpoint for the Azure Cosmos DB account.
cosmos_database – The name of the database to use.
cosmos_container – The name of the container to use.
session_id – The session ID to use, can be overwritten while loading.
user_id – The user ID to use, can be overwritten while loading.
credential – The credential to use to authenticate to Azure Cosmos DB.
connection_string – The connection string to use to authenticate.
ttl – The time to live (in seconds) to use for documents in the container.
cosmos_client_kwargs – Additional kwargs to pass to the CosmosClient.
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Add a self-created message to the store
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from this memory and cosmos.
load_messages() → None[source]¶
Retrieve the messages from Cosmos
prepare_cosmos() → None[source]¶
Prepare the CosmosDB client.
Use this function or the context manager to make sure your database is ready.
upsert_messages() → None[source]¶
Update the cosmosdb item.
langchain_community.chat_message_histories.astradb.AstraDBChatMessageHistory¶
class langchain_community.chat_message_histories.astradb.AstraDBChatMessageHistory(*, session_id: str, collection_name: str = 'langchain_message_store', token: Optional[str] = None, api_endpoint: Optional[str] = None, astra_db_client: Optional[LibAstraDB] = None, namespace: Optional[str] = None)[source]¶
Chat message history that stores history in Astra DB.
Args (only keyword-arguments accepted):
session_id: arbitrary key that is used to store the messagesof a single chat session.
collection_name (str): name of the Astra DB collection to create/use.
token (Optional[str]): API token for Astra DB usage.
api_endpoint (Optional[str]): full URL to the API endpoint,
such as “https://<DB-ID>-us-east1.apps.astra.datastax.com”.
astra_db_client (Optional[Any]): alternative to token+api_endpoint,you can pass an already-created ‘astrapy.db.AstraDB’ instance.
namespace (Optional[str]): namespace (aka keyspace) where thecollection is created. Defaults to the database’s “default namespace”.
Create an Astra DB chat message history.
Attributes
messages
Retrieve all session messages from DB
Methods
__init__(*, session_id[, collection_name, ...])
Create an Astra DB chat message history.
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Write a message to the table
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from DB
clear()
Clear session memory from DB
__init__(*, session_id: str, collection_name: str = 'langchain_message_store', token: Optional[str] = None, api_endpoint: Optional[str] = None, astra_db_client: Optional[LibAstraDB] = None, namespace: Optional[str] = None) → None[source]¶
Create an Astra DB chat message history.
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Write a message to the table
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from DB
langchain_community.chat_message_histories.firestore.FirestoreChatMessageHistory¶
class langchain_community.chat_message_histories.firestore.FirestoreChatMessageHistory(collection_name: str, session_id: str, user_id: str, firestore_client: Optional[Client] = None)[source]¶
Chat message history backed by Google Firestore.
Initialize a new instance of the FirestoreChatMessageHistory class.
Parameters
collection_name – The name of the collection to use.
session_id – The session ID for the chat..
user_id – The user ID for the chat.
Attributes
messages
A list of Messages stored in-memory.
Methods
__init__(collection_name, session_id, user_id)
Initialize a new instance of the FirestoreChatMessageHistory class.
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_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from this memory and Firestore.
load_messages()
Retrieve the messages from Firestore
prepare_firestore()
Prepare the Firestore client.
upsert_messages([new_message])
Update the Firestore document.
__init__(collection_name: str, session_id: str, user_id: str, firestore_client: Optional[Client] = None)[source]¶
Initialize a new instance of the FirestoreChatMessageHistory class.
Parameters
collection_name – The name of the collection to use.
session_id – The session ID for the chat..
user_id – The user ID for the chat.
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Add a Message object to the store.
Add a Message object to the store.
Parameters
message – A BaseMessage object to store.
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from this memory and Firestore.
load_messages() → None[source]¶
Retrieve the messages from Firestore
prepare_firestore() → None[source]¶
Prepare the Firestore client.
Use this function to make sure your database is ready.
upsert_messages(new_message: Optional[BaseMessage] = None) → None[source]¶
Update the Firestore document.
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.
__init__(table_name: str)[source]¶
from_sql_model(sql_message: Any) → BaseMessage[source]¶
Convert a SQLAlchemy model to a BaseMessage instance.
get_sql_model_class() → Any[source]¶
Get the SQLAlchemy model class.
to_sql_model(message: BaseMessage, session_id: str) → Any[source]¶
Convert a BaseMessage instance to a SQLAlchemy model.
langchain_community.chat_message_histories.file.FileChatMessageHistory¶
class langchain_community.chat_message_histories.file.FileChatMessageHistory(file_path: str)[source]¶
Chat message history that stores history in a local file.
Parameters
file_path – path of the local file to store the messages.
Attributes
messages
Retrieve the messages from the local file
Methods
__init__(file_path)
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 the local file
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from the local file
__init__(file_path: str)[source]¶
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Append the message to the record in the local file
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from the local file
Examples using FileChatMessageHistory¶
AutoGPT
langchain_community.chat_message_histories.streamlit.StreamlitChatMessageHistory¶
class langchain_community.chat_message_histories.streamlit.StreamlitChatMessageHistory(key: str = 'langchain_messages')[source]¶
Chat message history that stores messages in Streamlit session state.
Parameters
key – The key to use in Streamlit session state for storing messages.
Attributes
messages
Retrieve the current list of messages
Methods
__init__([key])
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Add a message to the session memory
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory
__init__(key: str = 'langchain_messages')[source]¶
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Add a message to the session memory
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory
Examples using StreamlitChatMessageHistory¶
Streamlit Chat Message History
langchain_community.chat_message_histories.xata.XataChatMessageHistory¶
class langchain_community.chat_message_histories.xata.XataChatMessageHistory(session_id: str, db_url: str, api_key: str, branch_name: str = 'main', table_name: str = 'messages', create_table: bool = True)[source]¶
Chat message history stored in a Xata database.
Initialize with Xata client.
Attributes
messages
Methods
__init__(session_id, db_url, api_key[, ...])
Initialize with Xata client.
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Append the message to the Xata table
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Delete session from Xata table.
__init__(session_id: str, db_url: str, api_key: str, branch_name: str = 'main', table_name: str = 'messages', create_table: bool = True) → None[source]¶
Initialize with Xata client.
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Append the message to the Xata table
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Delete session from Xata table.
Examples using XataChatMessageHistory¶
Xata chat memory
langchain_community.chat_message_histories.dynamodb.DynamoDBChatMessageHistory¶
class langchain_community.chat_message_histories.dynamodb.DynamoDBChatMessageHistory(table_name: str, session_id: str, endpoint_url: Optional[str] = None, primary_key_name: str = 'SessionId', key: Optional[Dict[str, str]] = None, boto3_session: Optional[Session] = None, kms_key_id: Optional[str] = None)[source]¶
Chat message history that stores history in AWS DynamoDB.
This class expects that a DynamoDB table exists with name table_name
Parameters
table_name – name of the DynamoDB table
session_id – arbitrary key that is used to store the messages
of a single chat session.
endpoint_url – URL of the AWS endpoint to connect to. This argument
is optional and useful for test purposes, like using Localstack.
If you plan to use AWS cloud service, you normally don’t have to
worry about setting the endpoint_url.
primary_key_name – name of the primary key of the DynamoDB table. This argument
is optional, defaulting to “SessionId”.
key – an optional dictionary with a custom primary and secondary key.
This argument is optional, but useful when using composite dynamodb keys, or
isolating records based off of application details such as a user id.
This may also contain global and local secondary index keys.
kms_key_id – an optional AWS KMS Key ID, AWS KMS Key ARN, or AWS KMS Alias for
client-side encryption
Attributes
messages
Retrieve the messages from DynamoDB
Methods
__init__(table_name, session_id[, ...])
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 DynamoDB
add_user_message(message)
Append the message to the record in DynamoDB
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from DynamoDB
__init__(table_name: str, session_id: str, endpoint_url: Optional[str] = None, primary_key_name: str = 'SessionId', key: Optional[Dict[str, str]] = None, boto3_session: Optional[Session] = None, kms_key_id: Optional[str] = None)[source]¶
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Append the message to the record in DynamoDB
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from DynamoDB
Examples using DynamoDBChatMessageHistory¶
Dynamodb Chat Message History
langchain_community.chat_message_histories.sql.create_message_model¶
langchain_community.chat_message_histories.sql.create_message_model(table_name, DynamicBase)[source]¶
Create a message model for a given table name.
Parameters
table_name – The name of the table to use.
DynamicBase – The base class to use for the model.
Returns
The model class.
langchain_community.chat_message_histories.mongodb.MongoDBChatMessageHistory¶
class langchain_community.chat_message_histories.mongodb.MongoDBChatMessageHistory(connection_string: str, session_id: str, database_name: str = 'chat_history', collection_name: str = 'message_store')[source]¶
Chat message history that stores history in MongoDB.
Parameters
connection_string – connection string to connect to MongoDB
session_id – arbitrary key that is used to store the messages
of a single chat session.
database_name – name of the database to use
collection_name – name of the collection to use
Attributes
messages
Retrieve the messages from MongoDB
Methods
__init__(connection_string, session_id[, ...])
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 MongoDB
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Clear session memory from MongoDB
__init__(connection_string: str, session_id: str, database_name: str = 'chat_history', collection_name: str = 'message_store')[source]¶
add_ai_message(message: str) → None¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
add_message(message: BaseMessage) → None[source]¶
Append the message to the record in MongoDB
add_user_message(message: str) → None¶
Convenience method for adding a human message string to the store.
Parameters
message – The string contents of a human message.
clear() → None[source]¶
Clear session memory from MongoDB
Examples using MongoDBChatMessageHistory¶
Mongodb Chat Message History
langchain_community.chat_message_histories.zep.ZepChatMessageHistory¶
class langchain_community.chat_message_histories.zep.ZepChatMessageHistory(session_id: str, url: str = 'http://localhost:8000', api_key: Optional[str] = None)[source]¶
Chat message history that uses Zep as a backend.
Recommended usage:
# Set up Zep Chat History
zep_chat_history = ZepChatMessageHistory(
    session_id=session_id,
    url=ZEP_API_URL,
    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 provides long-term conversation storage for LLM apps. The server stores,
summarizes, embeds, indexes, and enriches conversational AI chat
histories, and exposes them via simple, low-latency APIs.
For server installation instructions and more, see:
https://docs.getzep.com/deployment/quickstart/
This class is a thin wrapper around the zep-python package. Additional
Zep functionality is exposed via the zep_summary and zep_messages
properties.
For more information on the zep-python package, see:
https://github.com/getzep/zep-python
Attributes
messages
Retrieve messages from Zep memory
zep_messages
Retrieve summary from Zep memory
zep_summary
Retrieve summary from Zep memory
Methods
__init__(session_id[, url, api_key])
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_user_message(message[, metadata])
Append the message to the Zep memory history
add_user_message(message[, metadata])
Convenience method for adding a human message string to the store.
clear()
Clear session memory from Zep.
search(query[, metadata, limit])
Search Zep memory for messages matching the query
__init__(session_id: str, url: str = 'http://localhost:8000', api_key: Optional[str] = None) → None[source]¶
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 – The string contents of an AI message.
metadata – Optional metadata to attach to the message.
add_message(message: BaseMessage, metadata: Optional[Dict[str, Any]] = None) → None[source]¶
Append the message to the Zep memory history
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 – The string contents of a human message.
metadata – Optional metadata to attach to the message.
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.
search(query: str, metadata: Optional[Dict] = None, limit: Optional[int] = None) → List[MemorySearchResult][source]¶
Search Zep memory for messages matching the query
langchain_community.cache.FullMd5LLMCache¶
class langchain_community.cache.FullMd5LLMCache(**kwargs)[source]¶
SQLite table for full LLM Cache (all generations).
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and
values in kwargs.
Only keys that are present as
attributes of the instance’s class are allowed. These could be,
for example, any mapped columns or relationships.
Attributes
id
idx
llm
metadata
prompt
prompt_md5
registry
response
Methods
__init__(**kwargs)
A simple constructor that allows initialization from kwargs.
__init__(**kwargs)¶
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and
values in kwargs.
Only keys that are present as
attributes of the instance’s class are allowed. These could be,
for example, any mapped columns or relationships.