Align Docs Help

Langchain (Python)

Align offers Langchain Python Integration over ChatMessageHistory, a wrapper class built around LangChain's ChatMessageHistory, ensuring smooth integration.

This allows you to access all the interfaces of ChatMessageHistory from LangChain while simultaneously triggering events to Align.

Integration Steps

1. Initialization

To get started, supply both a ChatMessageHistory instance and an AlignAI SDK instance.

For each distinct session, remember to instantiate a fresh ChatMessageHistory. Right upon initialization, an open_session event gets automatically sent to Align.

2. User Identification

If you populate at least one field in the user_info parameter, an identify_user event is auto-emitted post-initialization.

For manual identification, invoke the identify_user method after initialization.

3. Session Closure

Once all messages are sent, utilize the close method to terminate the session. Executing this will trigger a close_session event for Align.

Example Usage

from datetime import datetime from align import AlignAI from align.integration.langchain import ChatMessageHistory from langchain.memory.chat_message_histories import RedisChatMessageHistory SESSION_ID = "d75d27cbff184b54917f7b921cd2afce" USER_ID = "5517d14bcd01488bb118e59baaf8156a" redis_history_backend = RedisChatMessageHistory(session_id=SESSION_ID) sdk = AlignAI( project_id="TEFSZ16", api_key="NIUaqth_TsuIQWox_TfD5sCEQ8PebffFjF76u7Vsl2o=" ) history = ChatMessageHistory( history_backend=redis_history_backend, sdk=sdk, session_id=SESSION_ID, user_id=USER_ID, assistant_id="1d2bfd85babd4804aca12656f5d91dfa" user_info={ "display_name": "Jason Lee", "email": "example123@gmail.com", "country_code": "US", "create_time": datetime.now(), } ) history.add_user_message("What's the best restaurant in New York?") history.add_ai_message("Definitely Dallas BBQ!") history.close() sdk.close()

You can now use ChatMessageHistory by Align as a drop-in replacement for ChatMessageHistory from LangChain.

Last modified: 28 February 2025