0
0
LangChainframework~5 mins

Session management for multi-user RAG in LangChain

Choose your learning style9 modes available
Introduction

Session management helps keep each user's conversation and data separate in a multi-user Retrieval-Augmented Generation (RAG) system. This way, users get personalized and consistent answers without mixing up information.

When building a chatbot that answers questions for many users at the same time.
When you want to remember past user queries and answers during a conversation.
When you need to keep user-specific data safe and separate in a shared AI service.
When you want to improve user experience by keeping context across multiple interactions.
When handling multiple users querying a knowledge base simultaneously.
Syntax
LangChain
class SessionManager:
    def __init__(self):
        self.sessions = {}

    def create_session(self, user_id):
        self.sessions[user_id] = {'history': []}

    def add_to_history(self, user_id, query, response):
        self.sessions[user_id]['history'].append({'query': query, 'response': response})

    def get_history(self, user_id):
        return self.sessions.get(user_id, {}).get('history', [])

This example shows a simple way to store conversation history per user using a dictionary.

Each user has a unique user_id to keep their data separate.

Examples
Create a session for user 'user123', add a question and answer, then get the conversation history.
LangChain
session_manager = SessionManager()
session_manager.create_session('user123')
session_manager.add_to_history('user123', 'What is AI?', 'AI means Artificial Intelligence.')
history = session_manager.get_history('user123')
Manage a different user 'user456' with their own conversation history.
LangChain
session_manager.create_session('user456')
session_manager.add_to_history('user456', 'Explain RAG.', 'RAG combines retrieval and generation.')
history = session_manager.get_history('user456')
Sample Program

This program creates a simple session manager to keep conversation history for two users, 'alice' and 'bob'. It uses LangChain's RetrievalQA with a FAISS vector store and OpenAI LLM to answer their questions. Each user's queries and answers are stored separately and printed at the end.

LangChain
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

class SessionManager:
    def __init__(self):
        self.sessions = {}

    def create_session(self, user_id):
        self.sessions[user_id] = {'history': []}

    def add_to_history(self, user_id, query, response):
        self.sessions[user_id]['history'].append({'query': query, 'response': response})

    def get_history(self, user_id):
        return self.sessions.get(user_id, {}).get('history', [])

# Initialize session manager
session_manager = SessionManager()

# Simulate two users
user_ids = ['alice', 'bob']

# Create sessions
for uid in user_ids:
    session_manager.create_session(uid)

# Dummy vector store and embeddings (replace with real data in practice)
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.load_local('faiss_index', embeddings)  # Assume index exists

# Create RAG chain
qa_chain = RetrievalQA.from_chain_type(
    llm=OpenAI(temperature=0),
    chain_type="stuff",
    retriever=vectorstore.as_retriever()
)

# User queries
queries = {
    'alice': 'What is Retrieval-Augmented Generation?',
    'bob': 'How does session management help in multi-user systems?'
}

# Process queries
for uid, query in queries.items():
    response = qa_chain.run(query)
    session_manager.add_to_history(uid, query, response)

# Print conversation histories
for uid in user_ids:
    print(f"Conversation history for {uid}:")
    for turn in session_manager.get_history(uid):
        print(f"Q: {turn['query']}")
        print(f"A: {turn['response']}\n")
OutputSuccess
Important Notes

In real applications, use persistent storage (like databases) for sessions to survive restarts.

Make sure to handle user authentication to assign correct user_id.

Session data can grow large; consider limits or cleanup strategies.

Summary

Session management keeps each user's conversation separate in multi-user RAG systems.

Use unique user IDs to store and retrieve user-specific data like query history.

This improves user experience by maintaining context and personalization.