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"))
All method signatures and response formats remain the same, ensuring a seamless transition.

Model Examples

Below are specific examples for each supported model. Click on any model to see its configuration and usage example.

Chat Models

Audio Models

Embedding Models

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")
)

async def main() -> None:
    # start a streaming chat completion
    stream = await client.chat.completions.create(
        model="<MODEL_NAME>",
        messages=[{"role": "user", "content": "Say this is a test"}],
        stream=True,
    )
    async for chunk in stream:
        if chunk.choices and 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.

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.

Guides