Overview
User scoping in Morphik allows multi-tenant applications to isolate data on a per-user basis. This ensures that in applications serving multiple users, each user can only access their own documents and data. User scoping is particularly valuable for building customer-facing applications where data privacy and separation are essential.Creating User Scopes
- Sync
- Async
Operations Within a User Scope
Once you have a user scope, all operations performed on it are automatically scoped to that specific user. Documents created or retrieved are only accessible to that user.- Sync
- Async
User Scope Methods
The UserScope class provides the same document operations as the main Morphik client, but automatically scoped to the specific user:ingest_text
- Ingest text content for this useringest_file
- Ingest a file for this useringest_files
- Ingest multiple files for this useringest_directory
- Ingest all files from a directory for this userretrieve_chunks
- Retrieve chunks matching a query from this user’s documentsretrieve_docs
- Retrieve documents matching a query from this user’s documentsquery
- Generate a completion using context from this user’s documents (supportsllm_config
parameter for custom LLM configuration)list_documents
- List all documents owned by this userbatch_get_documents
- Get multiple documents by their IDs for this userbatch_get_chunks
- Get specific chunks by source for this usercreate_graph
- Create a knowledge graph from this user’s documentsupdate_graph
- Update a knowledge graph with new documents from this userdelete_document_by_filename
- Delete a document by filename for this user
Using Custom LLM Configuration with User Scopes
You can pass a custom LLM configuration when querying within a user scope:- Sync
- Async
Example: Customer Support Application
A common use case for user scoping is in customer support applications where each customer has their own set of documents and needs personalized assistance:- Sync
- Async
Combining User Scope with Folder Scope
For advanced data organization, you can combine user scoping with folder scoping. This allows you to organize by both user and functional area:- Sync
- Async
Example: SaaS Application with Multi-Project Support
Here’s a more complex example showing how to build a SaaS application that supports multiple customers (tenants), each with their own multiple projects:- Sync
- Async
Bulk Document Processing for Multiple Users
When developing applications that serve multiple users, you might need to process documents in bulk. Here’s an example showing how to ingest documents for multiple users:- Sync
- Async