PebbloRetrievalQA is a Retrieval chain with Identity & Semantic Enforcement for question-answering against a vector database.This notebook covers how to retrieve documents using Identity & Semantic Enforcement (Deny Topics/Entities). For more details on Pebblo and its SafeRetriever feature visit Pebblo documentation
Steps
- Loading Documents: We will load documents with authorization and semantic metadata into an in-memory Qdrant vectorstore. This vectorstore will be used as a retriever in PebbloRetrievalQA.
Note: It is recommended to use PebbloSafeLoader as the counterpart for loading documents with authentication and semantic metadata on the ingestion side. PebbloSafeLoader
guarantees the secure and efficient loading of documents while maintaining the integrity of the metadata.
- Testing Enforcement Mechanisms: We will test Identity and Semantic Enforcement separately. For each use case, we will define a specific “ask” function with the required contexts (auth_context and semantic_context) and then pose our questions.
Setup
Dependencies
We’ll use an OpenAI LLM, OpenAI embeddings and a Qdrant vector store in this walkthrough.Identity-aware Data Ingestion
Here we are using Qdrant as a vector database; however, you can use any of the supported vector databases. PebbloRetrievalQA chain supports the following vector databases:- Qdrant
- Pinecone
- Postgres(utilizing the pgvector extension)
authorized_identities
, pebblo_semantic_topics
, and pebblo_semantic_entities
fields within the metadata of the VectorDB entry for each chunk.
NOTE: To use the PebbloRetrievalQA chain, you must always place authorization and semantic metadata in the specified fields. These fields must contain a list of strings.
Retrieval with Identity Enforcement
PebbloRetrievalQA chain uses a SafeRetrieval to enforce that the snippets used for in-context are retrieved only from the documents authorized for the user. To achieve this, the Gen-AI application needs to provide an authorization context for this retrieval chain. This auth_context should be filled with the identity and authorization groups of the user accessing the Gen-AI app. Here is the sample code for thePebbloRetrievalQA
with user_auth
(List of user authorizations, which may include their User ID and
the groups they are part of) from the user accessing the RAG application, passed in auth_context
.
1. Questions by Authorized User
We ingested data for authorized identities["finance-team", "exec-leadership"]
, so a user with the authorized identity/group finance-team
should receive the correct answer.
2. Questions by Unauthorized User
Since the user’s authorized identity/groupeng-support
is not included in the authorized identities ["finance-team", "exec-leadership"]
, we should not receive an answer.
3. Using PromptTemplate to provide additional instructions
You can use PromptTemplate to provide additional instructions to the LLM for generating a custom response.3.1 Questions by Authorized User
3.2 Questions by Unauthorized Users
Retrieval with Semantic Enforcement
The PebbloRetrievalQA chain uses SafeRetrieval to ensure that the snippets used in context are retrieved only from documents that comply with the provided semantic context. To achieve this, the Gen-AI application must provide a semantic context for this retrieval chain. Thissemantic_context
should include the topics and entities that should be denied for the user accessing the Gen-AI app.
Below is a sample code for PebbloRetrievalQA with topics_to_deny
and entities_to_deny
. These are passed in semantic_context
to the chain input.
1. Without semantic enforcement
Since no semantic enforcement is applied, the system should return the answer without excluding any context due to the semantic labels associated with the context.2. Deny financial-report topic
Data has been ingested with the topics:["financial-report"]
.
Therefore, an app that denies the financial-report
topic should not receive an answer.
3. Deny us-bank-account-number entity
Since the entityus-bank-account-number
is denied, the system should not return the answer.