Dynamically customize web app for different users and user groups

johananmahendran 120 Reputation points
2025-02-10T12:28:27.7733333+00:00

Hi,

am designing a chatbot and I want to give different users different versions of a chatbot. I have implemented a RAG architecture with Azure AI Search as my search index and Azure OpenAI as the GPT model provider.

For example, when the user logs in and accesses the chatbot using the Microsoft identity provider, the chatbot should be customized so that:

  1. The user's chat history is specific to the user.
  2. The documents that the user can search depends on their user group.

I read that Azure AI Search has document-level authentication but I can't find a good reference to how it is implemented.

My front end is React TypeScript, while my backend is written in Python.

What is the best way to go about this?

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,185 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 17,571 Reputation points
    2025-02-11T13:26:11.84+00:00

    Hello johananmahendran,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that your scenario focused on dynamically customize web app for different users and user groups.

    This is a simplified and optimized best practices approach:

    1. Use Azure Cosmos DB or MongoDB with a structure like:
         {
          "userId": "user123",
          "timestamp": "2024-07-25T10:30:00Z",
          "chat": "What are the latest company policies?",
          "response": "Refer to policy document X."
         }
      
      Retrieve chat history using:
         chat_history = collection.find({"userId": user_id}).sort("timestamp", -1)
      
    2. Instead of relying on RBAC, use index metadata filtering to Implement Document-Level Security in Azure AI Search, by following the steps below: Step 1: When indexing documents in Azure AI Search, add a user-group field to store Metadata in the Index:
         {
         "id": "doc123",
         "content": "Confidential sales report",
         "allowedGroups": ["sales", "executives"]
         }  
      
      Ensure allowedGroups is retrievable and filterable in the index schema. Step 2: When a user searches, extract their assigned groups from Microsoft Entra ID (Azure AD) to apply Filters Dynamically in Queries:
         user_groups = get_user_groups(user_id)  # ["sales", "marketing"]
      
      Then, filter search queries dynamically:
         group_filter = " OR ".join([f"allowedGroups eq '{group}'" for group in user_groups])
         search_results = search_client.search(query, filter=group_filter)
      
      This ensures the user only sees documents matching at least one of their groups.
    3. Putting into consideration your frontend (React TypeScript). Use MSAL React (@azure/msal-react) to authenticate users and obtain their group claims from Microsoft identity provider.
    4. Then, send group claims to the backend with search requests. Your backend (Python) will extract the user's groups from the token and apply document filtering dynamically.
    5. This is an Optional - To dynamically Customize Chatbot Per User. If you want different chatbot responses per user group, consider:
      1. Different Prompts, you will have to customize GPT behavior based on the user's group.
      2. Different Knowledge Bases, use separate vector databases for each group.
      3. And Azure OpenAI on Your Data, you will have to create multiple indexes per group.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.