Skip to main content

OpenAI-compatible interface for using SyftBox datasets with AI models in secure enclaves

Project description

syft-nsai

Bridge Data and AI. Privately.

An OpenAI-compatible interface for using SyftBox datasets with AI models in secure enclaves.

๐Ÿš€ What is syft-nsai?

syft-nsai transforms how you work with federated datasets by providing a familiar OpenAI-style chat completions API that operates within secure enclaves. No more complex setup or unfamiliar APIs - just select your datasets and chat with your data.

import syft_datasets as syd
import syft_nsai as nsai

# Discover datasets with beautiful interactive UI
syd.datasets

# Use them with familiar OpenAI-style API
selected_datasets = [syd.datasets[i] for i in [0, 1, 5]]
response = nsai.client.chat.completions.create(
    model=selected_datasets,  # Your datasets become the "model"
    messages=[{"role": "user", "content": "What trends do you see in this data?"}]
)

# Access results
print(response.choices[0].message.content)

โœจ Key Features

  • ๐Ÿ”’ Privacy-First: All processing happens in secure enclaves
  • ๐Ÿค OpenAI Compatible: Drop-in replacement for familiar chat completions API
  • ๐Ÿ“Š Real Data Integration: Datasets are loaded and summarized before AI analysis
  • โšก Lazy Loading: Results are fetched asynchronously for better UX
  • ๐Ÿ”„ Seamless Integration: Works perfectly with syft-datasets for dataset discovery

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  syft-datasets  โ”‚    โ”‚   syft-nsai      โ”‚    โ”‚  Secure Enclave โ”‚
โ”‚                 โ”‚    โ”‚                  โ”‚    โ”‚                 โ”‚
โ”‚ Dataset Discoveryโ”‚โ”€โ”€โ”€โ–ถโ”‚OpenAI-style API  โ”‚โ”€โ”€โ”€โ–ถโ”‚  AI Processing  โ”‚
โ”‚ Interactive UI   โ”‚    โ”‚Chat Completions  โ”‚    โ”‚  with Real Data โ”‚
โ”‚ Search & Select  โ”‚    โ”‚Async Results     โ”‚    โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ฆ Installation

pip install syft-nsai

For development:

pip install syft-nsai[dev]

๐Ÿ”‘ Configuration

Tinfoil API Key

syft-nsai uses syft-wallet for secure API key management. When you first use NSAI, it will automatically:

  1. Check syft-wallet for your Tinfoil API key
  2. Check environment variables as a fallback
  3. Prompt you to enter the key if not found anywhere
  4. Save it securely to syft-wallet for future use

First-time Setup

import syft_nsai as nsai

# On first use, you'll be prompted for your API key
response = nsai.client.chat.completions.create(
    model=datasets,
    messages=[{"role": "user", "content": "Hello"}]
)
# ๐Ÿ”‘ Tinfoil API Key Required
# NSAI needs a Tinfoil API key to run AI models in secure enclaves.
# You can get a free API key at: https://tinfoil.sh/
# Please enter your Tinfoil API key: tk_xxxxx...
# โœ“ API key saved securely to syft-wallet

Manual Key Management

import syft_nsai as nsai

# Set or update your API key
nsai.set_tinfoil_api_key("tk_your_new_key_here")
# or prompt for input:
nsai.set_tinfoil_api_key()

# Check current key status
nsai.show_tinfoil_status()
# โœ“ Tinfoil API key found: tk_SxHBk...UOFd

# Get the key programmatically
api_key = nsai.get_tinfoil_api_key()

Environment Variable Fallback

You can still use environment variables:

export TINFOIL_API_KEY="tk_your_key_here"

NSAI will detect this and offer to save it to syft-wallet for easier future use.

Security Benefits:

  • โœ… Keys stored in 1Password (if available) or secure system keyring
  • โœ… No hardcoded keys in your code
  • โœ… One-time setup per machine
  • โœ… Automatic detection across all methods

๐Ÿš€ Quick Start

1. Discover Datasets

First, use syft-datasets to explore available datasets:

import syft_datasets as syd

# Show interactive dataset browser
syd.datasets

2. Select and Analyze

Use the selected datasets with the OpenAI-compatible API:

import syft_nsai as nsai

# Select datasets (from the interactive UI or programmatically)
my_datasets = [syd.datasets[0], syd.datasets[3]]

# Create chat completion
response = nsai.client.chat.completions.create(
    model=my_datasets,
    messages=[
        {"role": "system", "content": "You are a data analyst."},
        {"role": "user", "content": "Summarize the key insights from this data."}
    ]
)

# Results are loaded lazily when accessed
insights = response.choices[0].message.content
print(insights)

3. Advanced Usage

# Multiple datasets for cross-analysis
crop_data = syd.datasets.search("crop")[0]
weather_data = syd.datasets.search("weather")[0]

response = nsai.client.chat.completions.create(
    model=[crop_data, weather_data],
    messages=[{
        "role": "user", 
        "content": "Analyze the correlation between weather patterns and crop yields."
    }]
)

๐Ÿ”ง How It Works

  1. Dataset Integration: Selected datasets are loaded and analyzed within the enclave
  2. Context Enhancement: Your prompts are enhanced with actual dataset summaries and sample data
  3. Secure Processing: AI analysis happens in a privacy-preserving enclave using Tinfoil API
  4. Familiar Interface: Results are returned through OpenAI-compatible response objects

๐Ÿ”— Integration with SyftBox Ecosystem

syft-nsai is part of the broader SyftBox ecosystem:

  • syft-datasets: Dataset discovery and management
  • syft-core: Core SyftBox functionality
  • syftbox-enclave: Secure enclave execution
  • syft-rds: Remote data science capabilities

๐Ÿ“š API Reference

Client

import syft_nsai as nsai

# Global client instance
client = nsai.client

Chat Completions

response = nsai.client.chat.completions.create(
    model=datasets_list,        # List of Dataset objects
    messages=messages_list,     # OpenAI-style messages
    **kwargs                    # Additional parameters
)

Response Objects

# ChatCompletion
response.choices              # List of Choice objects

# Choice  
choice = response.choices[0]
choice.message                # Message object

# Message
message = choice.message
message.content               # Actual AI response (lazy loaded)

API Key Management

# Set or update Tinfoil API key
nsai.set_tinfoil_api_key(api_key=None)  # Prompts if None

# Get current API key
api_key = nsai.get_tinfoil_api_key()    # Raises ValueError if not found

# Check key status
nsai.show_tinfoil_status()              # Shows masked key or missing status

๐Ÿงช Development

Setup

git clone https://github.com/OpenMined/syft-nsai
cd syft-nsai
uv sync

Testing

uv run pytest

Linting

uv run ruff check .
uv run ruff format .

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

๐Ÿ“„ License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

๐Ÿ™ Acknowledgments

Built with โค๏ธ by the OpenMined community. Special thanks to:

  • The Tinfoil team for secure AI infrastructure
  • SyftBox contributors for the federated data ecosystem
  • The broader privacy-preserving ML community

Transform your data science workflow. Privately and securely.

Get Started โ†’ | Documentation โ†’ | Examples โ†’

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

syft_nsai-0.1.1.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

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

syft_nsai-0.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: syft_nsai-0.1.1.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for syft_nsai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 61e81de00df2f82c542605645c5f1292692487738072bfa7f1740c07c1ad930b
MD5 053bcdf77973c0bc61982ce9b48fea74
BLAKE2b-256 ccce6147b385b94e4814a8727494408dc1ead64ee1bfb04e7312555ee4122369

See more details on using hashes here.

File details

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

File metadata

  • Download URL: syft_nsai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for syft_nsai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bd870721199b42b89447c7d6b611b15ba65fad20685bd96db538167e32c23dae
MD5 a760e98618dcceab8aafd5cd7b87068f
BLAKE2b-256 0a82fad31176704d4b4cad1d618760838ccabfd23dc77257c80c0afea27ea3c9

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