The Tinfoil Python SDK is a wrapper around the OpenAI Python client that provides secure communication with Tinfoil enclaves. It has the same API as the OpenAI SDK with additional security features including automatic verification that the endpoint is running in a secure Tinfoil enclave, TLS certificate pinning, and attestation validation.
Migrating from OpenAI to Tinfoil is straightforward:
# Before (OpenAI)- from openai import OpenAI- client = OpenAI(api_key="OPENAI_API_KEY")# After (Tinfoil)+ from tinfoil import TinfoilAI+ client = TinfoilAI(api_key="TINFOIL_API_KEY")
All method signatures and response formats remain the same.
import osfrom tinfoil import TinfoilAIclient = TinfoilAI( api_key=os.getenv("TINFOIL_API_KEY"))# Example: Complex reasoning taskresponse = client.chat.completions.create( model="glm-5-1", messages=[ { "role": "user", "content": "Solve this step by step: If a train travels 120 miles in 2 hours, and then increases its speed by 25% for the next 3 hours, how far does it travel in total?" } ])print(response.choices[0].message.content)
import osfrom tinfoil import TinfoilAIimport numpy as npclient = TinfoilAI( api_key=os.getenv("TINFOIL_API_KEY"))# Example: Generate embeddings for similarity searchdocuments = [ "Artificial intelligence is transforming modern technology.", "Machine learning enables computers to learn from data.", "The weather today is sunny and warm.", "Deep learning uses neural networks with multiple layers."]# Generate embeddings for all documentsembeddings = []for doc in documents: response = client.embeddings.create( model="nomic-embed-text", input=doc, encoding_format="float" ) embeddings.append(response.data[0].embedding)# Calculate similarity between first two documentsemb1 = np.array(embeddings[0])emb2 = np.array(embeddings[1])similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))print(f"Similarity between first two AI-related documents: {similarity:.3f}")print(f"Embedding dimension: {len(embeddings[0])}")
Simply import AsyncTinfoilAI instead of TinfoilAI and use await with each API call:
import osimport asynciofrom tinfoil import AsyncTinfoilAIclient = 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.