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.