Python SDK

Python SDK for Tinfoil’s secure AI inference API
GitHub: tinfoil-python

Overview

The Tinfoil Python SDK provides a drop-in replacement for the official OpenAI Python client, adding automatic attestation verification for secure AI inference. It maintains full compatibility with the OpenAI interface while ensuring your data remains private and secure. View the source code on GitHub.

Installation

pip install tinfoil

Migration from OpenAI

Migrating from OpenAI to Tinfoil is straightforward:

# Before (OpenAI)
- import os
- from openai import OpenAI
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# After (Tinfoil)
+ import os
+ from tinfoil import TinfoilAI
+ client = TinfoilAI(
+     api_key=os.getenv("TINFOIL_API_KEY"),
+     enclave="llama3-3-70b-p.model.tinfoil.sh",
+     repo="tinfoilsh/confidential-llama3-3-70b-prod",
+ )

All method signatures and response formats remain the same, ensuring a seamless transition.

Usage Examples

Chat Completions

import os
from tinfoil import TinfoilAI

client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="llama3-3-70b-p.model.tinfoil.sh",
    repo="tinfoilsh/confidential-llama3-3-70b-prod"
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Hi",
        }
    ],
    model="llama3-3-70b",
)
print(chat_completion.choices[0].message.content)

Streaming Responses

# Stream chat responses
stream = client.chat.completions.create(
    model="llama3-3-70b",
    messages=[{"role": "user", "content": "Write a short story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Audio Transcription

import os
from tinfoil import TinfoilAI

client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="audio-processing.model.tinfoil.sh",
    repo="tinfoilsh/confidential-audio-processing"
)

with open("audio.mp3", "rb") as audio_file:
    transcription = client.audio.transcriptions.create(
        file=audio_file,
        model="whisper-large-v3-turbo",
    )
print(transcription.text)

Text-to-Speech

import os
from tinfoil import TinfoilAI

client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="audio-processing.model.tinfoil.sh",
    repo="tinfoilsh/confidential-audio-processing"
)

# Generate speech from text
response = client.audio.speech.create(
    model="kokoro",
    voice="af_sky+af_bella",
    input="Hello world! This is a test of text-to-speech synthesis."
)

# Save the audio file
response.write_to_file("output.mp3")
print("Speech saved to output.mp3")

# Or stream directly to a file
with open("output.mp3", "wb") as f:
    response.stream_to_file(f)

Embeddings

# Configure for embedding model
client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="nomic-embed-text.model.tinfoil.sh",
    repo="tinfoilsh/confidential-nomic-embed-text"
)

# Generate embeddings
response = client.embeddings.create(
    model="nomic-embed-text",
    input="The quick brown fox jumps over the lazy dog"
)

embedding_vector = response.data[0].embedding
print(f"Embedding dimension: {len(embedding_vector)}")

Async Usage

Simply import AsyncTinfoilAI instead of TinfoilAI and use await with each API call:

import os
import asyncio
from tinfoil import AsyncTinfoilAI

client = AsyncTinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="llama3-3-70b-p.model.tinfoil.sh",
    repo="tinfoilsh/confidential-llama3-3-70b-prod"
)

async def main() -> None:
    # start a streaming chat completion
    stream = await client.chat.completions.create(
        model="llama3-3-70b",
        messages=[{"role": "user", "content": "Say this is a test"}],
        stream=True,
    )
    async for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            print(chunk.choices[0].delta.content, end="", flush=True)
    print()

asyncio.run(main())

Functionality between the synchronous and asynchronous clients is otherwise identical.

Low-level HTTP Endpoints

You can also perform arbitrary GET/POST requests that are verified:

import os
from tinfoil import NewSecureClient

tfclient = NewSecureClient(
    api_key=os.getenv("TINFOIL_API_KEY"),
    enclave="df-demo.model.tinfoil.sh",
    repo="tinfoilsh/confidential-df-demo",
)

# GET example
resp = tfclient.get(
    "https://df-demo.model.tinfoil.sh/health",
    params={"query": "value"},
    timeout=30,
)
print(resp.status_code, resp.text)

# POST example
payload = {"key": "value"}
resp = tfclient.post(
    "https://df-demo.model.tinfoil.sh/analyze",
    headers={"Content-Type": "application/json"},
    json=payload,
    timeout=30,
)
print(resp.status_code, resp.text)

API Documentation

This library is a drop-in replacement for the official OpenAI Python client that can be used with Tinfoil. All methods and types are identical. See the OpenAI Python client documentation for complete API usage and documentation.