Skip to main content

A semantic RBAC system with RAG capabilities

Project description

Gatecraft

Gatecraft is a semantic Role-Based Access Control (RBAC) system with Retrieval-Augmented Generation (RAG) capabilities. It provides a flexible way to manage access control based on semantic similarity and content understanding.

Features

  • Semantic-based access control
  • Integration with OpenAI embeddings
  • Pinecone vector store support
  • Flexible RBAC system
  • RAG capabilities

Installation

pip install gatecraft

Quick Start

Here's a quick example to get you started with Gatecraft:

from gatecraft import Gatecraft, SemanticCondition

# Initialize the Gatecraft system
gc = Gatecraft()

# Create users
alice = gc.create_user(user_id=1, name='Alice')
bob = gc.create_user(user_id=2, name='Bob')

# Create roles with conditions
cat_lover_role = gc.create_role(role_id=1, name='Cat Lover')
cat_condition = SemanticCondition(term='cat', threshold=0.78)
gc.add_condition_to_role(cat_lover_role, cat_condition)

everything_except_cats_role = gc.create_role(role_id=2, name='Everything Except Cats')
inverse_cat_condition = SemanticCondition(term='cat', threshold=0.78, inverse=True)
gc.add_condition_to_role(everything_except_cats_role, inverse_cat_condition)

# Assign roles to users
gc.assign_role(alice, cat_lover_role)
gc.assign_role(bob, everything_except_cats_role)

# Add entities (documents)
gc.add_entity(entity_id=1, data='Cute kitten playing with a ball of yarn.')
gc.add_entity(entity_id=2, data='Dog training and obedience tips.')

# Check access
print(f"Alice access to Entity 1: {gc.is_access_allowed(alice, 1)}")  # Should be True
print(f"Alice access to Entity 2: {gc.is_access_allowed(alice, 2)}")  # Should be False
print(f"Bob access to Entity 1: {gc.is_access_allowed(bob, 1)}")      # Should be False
print(f"Bob access to Entity 2: {gc.is_access_allowed(bob, 2)}")      # Should be True

Explanation

  • Initialization: Start by importing Gatecraft and SemanticCondition from the gatecraft library. Initialize the Gatecraft system:

    from gatecraft import Gatecraft, SemanticCondition
    
    gc = Gatecraft()
    
  • Creating Users: Create user instances for Alice and Bob:

    alice = gc.create_user(user_id=1, name='Alice')
    bob = gc.create_user(user_id=2, name='Bob')
    
  • Defining Roles and Conditions:

    • Cat Lover Role: Create a role for users who love cats and add a condition to allow access to content related to "cat":

      cat_lover_role = gc.create_role(role_id=1, name='Cat Lover')
      cat_condition = SemanticCondition(term='cat', threshold=0.78)
      gc.add_condition_to_role(cat_lover_role, cat_condition)
      
    • Everything Except Cats Role: Create a role for users who should access all content except that related to "cat":

      everything_except_cats_role = gc.create_role(role_id=2, name='Everything Except Cats')
      inverse_cat_condition = SemanticCondition(term='cat', threshold=0.78, inverse=True)
      gc.add_condition_to_role(everything_except_cats_role, inverse_cat_condition)
      
  • Assigning Roles to Users: Assign the roles to the respective users:

    gc.assign_role(alice, cat_lover_role)
    gc.assign_role(bob, everything_except_cats_role)
    
  • Adding Entities (Documents): Add content entities to the system:

    gc.add_entity(entity_id=1, data='Cute kitten playing with a ball of yarn.')
    gc.add_entity(entity_id=2, data='Dog training and obedience tips.')
    
  • Access Control Check: Determine if users have access to specific entities:

    print(f"Alice access to Entity 1: {gc.is_access_allowed(alice, 1)}")  # True
    print(f"Alice access to Entity 2: {gc.is_access_allowed(alice, 2)}")  # False
    print(f"Bob access to Entity 1: {gc.is_access_allowed(bob, 1)}")      # False
    print(f"Bob access to Entity 2: {gc.is_access_allowed(bob, 2)}")      # True
    

Additional Information

  • Semantic Conditions: The SemanticCondition allows you to define access conditions based on semantic similarity to a term. Setting inverse=True reverses the condition.
  • Similarity Threshold: Adjust the threshold parameter in SemanticCondition to change the sensitivity of the semantic matching.
  • Gatecraft Class: The Gatecraft class encapsulates all the functionality, simplifying the process of creating users, roles, and entities, and checking access.
  • No Need to Manage Permissions Directly: Permissions are handled internally, reducing complexity and making the library easier to use.

Advanced Usage

For more advanced scenarios, you can:

  • Integrate with Different Vector Stores: Provide a custom vector store to the Gatecraft class if you wish to use something other than Pinecone.

    from gatecraft.db.mock_vector_store import MockVectorStore
    
    vector_store = MockVectorStore()
    gc = Gatecraft(vector_store=vector_store)
    
  • Set Custom Similarity Thresholds: Adjust the overall similarity threshold when initializing Gatecraft:

    gc = Gatecraft(similarity_threshold=0.90)
    

Dependencies

Ensure that you have the required API keys set in your environment variables or .env file:

  • OPENAI_API_KEY
  • PINECONE_API_KEY
  • PINECONE_ENVIRONMENT

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Feel free to reach out if you have any questions or need further assistance with Gatecraft!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gatecraft-0.1.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gatecraft-0.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file gatecraft-0.1.1.tar.gz.

File metadata

  • Download URL: gatecraft-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for gatecraft-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef85530a07f6bffdc86dc2ed18b90ebed1d213f1b3fafedc80bf8fe818d41313
MD5 3d4511c57d94f33524e4796c140bfef7
BLAKE2b-256 60c0f9500cfa676168c96192e9de196202ecf068b9ecadebe4d7f5fb1de2cb97

See more details on using hashes here.

File details

Details for the file gatecraft-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: gatecraft-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for gatecraft-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d644142b7acd4c6d1dcce66c32ffc4270d8b369edc981c177e63735038e8528
MD5 91d9bfdd40f7a45d9d1153d670f96caa
BLAKE2b-256 75ccb421cdcc2875ee29df8681127950a60bab60a99318f35190ddef7e7224c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page