Skip to main content

Python client for Tinfoil

Project description

Tinfoil Python Library

PyPI - Version Integration Documentation

A Python client for secure AI model inference through Tinfoil.

Installation

pip install tinfoil

Usage

The Tinfoil SDK automatically selects a router enclave and verifies it against the official GitHub repository. You just need to provide your API key:

import os
from tinfoil import TinfoilAI

client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY")
)

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

Audio Transcription with Whisper

You can transcribe audio files using OpenAI's Whisper model:

import os
from tinfoil import TinfoilAI

client = TinfoilAI(
    api_key=os.getenv("TINFOIL_API_KEY")
)

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

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:
    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 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.

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

# GET example
resp = tfclient.get(
    "https://example.com/health",
    params={"query": "value"},
    timeout=30,
)
print(resp.status_code, resp.text)

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

Requirements

  • Python 3.10 through 3.13

Testing

Run unit and integration tests:

pytest -q

Integration tests require the TINFOIL_API_KEY environment variable:

export TINFOIL_API_KEY="..."
pytest -q -m integration

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

tinfoil-0.11.2.tar.gz (118.5 kB view details)

Uploaded Source

Built Distribution

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

tinfoil-0.11.2-py3-none-any.whl (85.3 kB view details)

Uploaded Python 3

File details

Details for the file tinfoil-0.11.2.tar.gz.

File metadata

  • Download URL: tinfoil-0.11.2.tar.gz
  • Upload date:
  • Size: 118.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tinfoil-0.11.2.tar.gz
Algorithm Hash digest
SHA256 22fd0c433cbb75489fb0dd56fac7543990cee1fd2a8a46061736e8395dc6c594
MD5 92d4cb5ca3005fa60872c393c5f4bfca
BLAKE2b-256 0f5060c22aba28031967783eaff7c3d1042a7f0a8256e241d98cdf7457095ad0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinfoil-0.11.2.tar.gz:

Publisher: build.yml on tinfoilsh/tinfoil-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tinfoil-0.11.2-py3-none-any.whl.

File metadata

  • Download URL: tinfoil-0.11.2-py3-none-any.whl
  • Upload date:
  • Size: 85.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tinfoil-0.11.2-py3-none-any.whl
Algorithm Hash digest
SHA256 930baae966022a53bdfd7718dc1800d1147bd3f211e5a24178f6536b20b7df19
MD5 2dcc17d95d5834faa686ddb3a6675cfb
BLAKE2b-256 297833a1ef2e1f02c5409224f357d41154f6e039c721a5a8d127f694b6a46c30

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinfoil-0.11.2-py3-none-any.whl:

Publisher: build.yml on tinfoilsh/tinfoil-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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