Repository: topoteretes/cognee
File: examples/python/permissions_example.py (Python)

Code before:

logger = get_logger()


async def main():
    # ENABLE PERMISSIONS FEATURE
    # Note: When ENABLE_BACKEND_ACCESS_CONTROL is enabled vector provider is automatically set to use LanceDB
    # and graph provider is set to use Kuzu.
    os.environ["ENABLE_BACKEND_ACCESS_CONTROL"] = "True"

    # Set the rest of your environment variables as needed. By default OpenAI is used as the LLM provider
    # Reference the .env.tempalte file for available option and how to change LLM provider: https://github.com/topoteretes/cognee/blob/main/.env.template
    # For example to set your OpenAI LLM API key use:
    # os.environ["LLM_API_KEY""] = "your-api-key"

    # Create a clean slate for cognee -- reset data and system state
    print("Resetting cognee data...")
    await cognee.prune.prune_data()
    await cognee.prune.prune_system(metadata=True)
    print("Data reset complete.\n")

    # Set up the necessary databases and tables for user management.
    await setup()

    # NOTE: When a document is added in Cognee with permissions enabled only the owner of the document has permissions
    # for work with the document initially.
    # Add document for user_1, add it under dataset name AI
    explanation_file_path = os.path.join(
        pathlib.Path(__file__).parent, "../data/artificial_intelligence.pdf"
    )

    print("Creating user_1: user_1@example.com")
    user_1 = await create_user("user_1@example.com", "example")
    await cognee.add([explanation_file_path], dataset_name="AI", user=user_1)

    # Add document for user_2, add it under dataset name QUANTUM
    text = """A quantum computer is a computer that takes advantage of quantum mechanical phenomena.
    At small scales, physical matter exhibits properties of both particles and waves, and quantum computing leverages
    this behavior, specifically quantum superposition and entanglement, using specialized hardware that supports the
    preparation and manipulation of quantum states.
    """
    print("\nCreating user_2: user_2@example.com")
    user_2 = await create_user("user_2@example.com", "example")
    await cognee.add([text], dataset_name="QUANTUM", user=user_2)

    # Run cognify for both datasets as the appropriate user/owner
    print("\nCreating different datasets for user_1 (AI dataset) and user_2 (QUANTUM dataset)")
    ai_cognify_result = await cognee.cognify(["AI"], user=user_1)
    quantum_cognify_result = await cognee.cognify(["QUANTUM"], user=user_2)

    # Extract dataset_ids from cognify results

Review comment (suggestion):
```suggestion
    # to work with the document initially.
```

Code after:

logger = get_logger()


async def main():
    # ENABLE PERMISSIONS FEATURE
    # Note: When ENABLE_BACKEND_ACCESS_CONTROL is enabled vector provider is automatically set to use LanceDB
    # and graph provider is set to use Kuzu.
    os.environ["ENABLE_BACKEND_ACCESS_CONTROL"] = "True"

    # Set the rest of your environment variables as needed. By default OpenAI is used as the LLM provider
    # Reference the .env.tempalte file for available option and how to change LLM provider: https://github.com/topoteretes/cognee/blob/main/.env.template
    # For example to set your OpenAI LLM API key use:
    # os.environ["LLM_API_KEY""] = "your-api-key"

    # Create a clean slate for cognee -- reset data and system state
    print("Resetting cognee data...")
    await cognee.prune.prune_data()
    await cognee.prune.prune_system(metadata=True)
    print("Data reset complete.\n")

    # Set up the necessary databases and tables for user management.
    await setup()

    # NOTE: When a document is added in Cognee with permissions enabled only the owner of the document has permissions
    # to work with the document initially.
    # Add document for user_1, add it under dataset name AI
    explanation_file_path = os.path.join(
        pathlib.Path(__file__).parent, "../data/artificial_intelligence.pdf"
    )

    print("Creating user_1: user_1@example.com")
    user_1 = await create_user("user_1@example.com", "example")
    await cognee.add([explanation_file_path], dataset_name="AI", user=user_1)

    # Add document for user_2, add it under dataset name QUANTUM
    text = """A quantum computer is a computer that takes advantage of quantum mechanical phenomena.
    At small scales, physical matter exhibits properties of both particles and waves, and quantum computing leverages
    this behavior, specifically quantum superposition and entanglement, using specialized hardware that supports the
    preparation and manipulation of quantum states.
    """
    print("\nCreating user_2: user_2@example.com")
    user_2 = await create_user("user_2@example.com", "example")
    await cognee.add([text], dataset_name="QUANTUM", user=user_2)

    # Run cognify for both datasets as the appropriate user/owner
    print("\nCreating different datasets for user_1 (AI dataset) and user_2 (QUANTUM dataset)")
    ai_cognify_result = await cognee.cognify(["AI"], user=user_1)
    quantum_cognify_result = await cognee.cognify(["QUANTUM"], user=user_2)

    # Extract dataset_ids from cognify results