Align Docs Help

LlamaIndex (Python)

Align provides a seamless integration with LlamaIndex's Chat Engine with ChatMemory which is a drop-in wrapper class for the LlamaIndex memory system.

All it takes is to create an instance of ChatMemory and pass it to the ChatEngine constructor then the messages will be automatically sent to Align

Integration Steps

1. Initialization

To initialize the ChatMemory you need to pass the LlamaIndex memory and Align client.

from alignai import AlignAI from alignai.integration.llama_index import ChatMemory from llama_index.memory import ChatMemoryBuffer sdk = AlignAI(project_id="<PROJECT_ID>", api_key="<API_KEY>") chat_memory = ChatMemory( chat_memory=ChatMemoryBuffer.from_defaults(), align_client=sdk, session_id="<SESSION_ID>", user_id="<USER_ID>", assistant_id="<ASSISTANT_ID>", user_info={"email": "example123@gmail.com"} )

It's recommended to initialize a fresh ChatMemory instance for each distinct session.

Right upon initialization, an open_session event will be automatically sent to Align.

If you populate at least one field in the user_info parameter, an identify_user event will also be auto-emitted to Align. If desired, you can manually identify by invoking identify_user method after initialization.

2. Configuring Chat Engine

With the ChatMemory initialized, it's time to integrate it into the LlamaIndex Chat Engine.

from llama_index.chat_engine import SimpleChatEngine chat_engine = SimpleChatEngine.from_defaults(memory=chat_memory)

That's it! Now, interact with the chat engine in the usual manner and all messages will be automatically routed to Align AI.

3. Closing the Session

At the end of your chat interactions, terminate the session with close method which will trigger close_session event, ensuring no loose ends.

Example Usage

from datetime import datetime from alignai import AlignAI from alignai.integration.llama_index import ChatMemory from llama_index.chat_engine import SimpleChatEngine from llama_index.memory import ChatMemoryBuffer API_KEY = "NIUaqth_TsuIQWox_TfD5sCEQ8PebffFjF76u7Vsl2o=" PROJECT_ID = "TEFSZ16" SESSION_ID = "d75d27cbff184b54917f7b921cd2afce" USER_ID = "5517d14bcd01488bb118e59baaf8156a" sdk = AlignAI(project_id=PROJECT_ID, api_key=API_KEY) chat_memory = ChatMemory( chat_memory=ChatMemoryBuffer.from_defaults(), align_client=sdk, session_id=SESSION_ID, user_id=USER_ID, assistant_id="1d2bfd85-babd-4804-aca1-2656f5d91dfa", user_info= { "display_name": "Jason Lee", "email": "example123@gmail.com", "country_code": "US", "create_time": datetime.now(), } ) chat_engine = SimpleChatEngine.from_defaults(memory=chat_memory) response = chat_engine.chat("Who's the greatest boxer?") chat_memory.close() sdk.close()
Last modified: 28 February 2025