Skip to content

OpenAI Integration

The Glacis OpenAI integration wraps the official OpenAI client to automatically create attestations for every API call.

Installation

Terminal window
pip install glacis[openai]

Quick Start

from glacis.integrations.openai import attested_openai, get_last_receipt
client = attested_openai(
glacis_api_key="glsk_live_...",
openai_api_key="sk-..."
)
# Make a normal OpenAI call - attestation happens automatically
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
# Get the attestation receipt
receipt = get_last_receipt()
print(f"Attested: {receipt.badge_url}")

What Gets Attested

For each chat completion, Glacis captures:

FieldAttestedDetails
Request messagesHash onlySHA-256, never sent
Response contentHash onlySHA-256, never sent
ModelMetadataSent as-is
TemperatureMetadataSent as-is
TimestampMetadataISO 8601
Token countsMetadataIf available

Environment Variables

Terminal window
export GLACIS_API_KEY=glsk_live_...
export OPENAI_API_KEY=sk-...
from glacis.integrations.openai import attested_openai
# Keys read from environment automatically
client = attested_openai()

Accessing Receipts

Last Receipt

from glacis.integrations.openai import get_last_receipt
receipt = get_last_receipt()
print(f"ID: {receipt.attestation_id}")
print(f"Merkle root: {receipt.signed_tree_head.root_hash}")
print(f"Badge: {receipt.badge_url}")

Attached to Response

response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
# Receipt is attached to the response object
if hasattr(response, '_glacis_receipt'):
receipt = response._glacis_receipt
print(f"Attested: {receipt.attestation_id}")

Streaming Support

Streaming completions are automatically attested when the stream completes:

stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
full_response = ""
for chunk in stream:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
print(content, end="", flush=True)
# Receipt available after stream completes
receipt = get_last_receipt()
print(f"\n\nAttested: {receipt.badge_url}")

Offline Mode

For development without an API key:

from glacis.integrations.openai import attested_openai
# Offline mode - still attests, but with "UNVERIFIED" status
client = attested_openai(
glacis_mode="offline",
openai_api_key="sk-..." # Still need OpenAI key for the actual calls
)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
receipt = get_last_receipt()
print(f"Status: {receipt.witness_status}") # "UNVERIFIED"

Advanced: Custom Service ID

client = attested_openai(
glacis_api_key="glsk_live_...",
openai_api_key="sk-...",
service_id="my-chatbot", # Custom service identifier
)

Full Example

#!/usr/bin/env python3
"""
Complete example: OpenAI chat with attestation
"""
import os
from glacis.integrations.openai import attested_openai, get_last_receipt
def main():
# Create attested client
client = attested_openai(
glacis_api_key=os.environ.get("GLACIS_API_KEY"),
openai_api_key=os.environ.get("OPENAI_API_KEY"),
)
# Have a conversation
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
temperature=0.7,
)
print("Response:", response.choices[0].message.content)
print()
# Get attestation
receipt = get_last_receipt()
print("Attestation Details:")
print(f" Receipt ID: {receipt.attestation_id}")
print(f" Timestamp: {receipt.timestamp}")
print(f" Leaf index: {receipt.leaf_index}")
print(f" Merkle root: {receipt.signed_tree_head.root_hash[:32]}...")
print()
print(f"Verification URL: {receipt.badge_url}")
if __name__ == "__main__":
main()