Skip to main content

Using the OpenAI Python SDK

InertialAI's embeddings API is compatible with the OpenAI Python SDK, allowing you to use familiar tooling to generate embeddings from multi-modal time-series and text data.

Installation

pip install openai

Basic Setup

Configure the OpenAI client to point to InertialAI's API:

import os
from openai import OpenAI

client = OpenAI(
base_url="https://inertialai.com/api/v1",
api_key=os.environ["INERTIALAI_API_KEY"]
)

Multi-Modal Input Format

InertialAI's API accepts flexible inputs that can include time-series data, text descriptions, or both combined. The API supports:

  • Time-series only: Numerical sensor or time-series data without text
  • Text only: Descriptive text without time-series data
  • Multi-modal: Time-series data combined with text for enriched context

Since the OpenAI SDK's embeddings.create() method only accepts string inputs, you must stringify your JSON data using json.dumps() before passing it to the method.

Input Structure

import json

embedding_input = [
{
"time_series": [
[34.61, 35.11, 35.21, 35.36, 35.25], # Channel 1
[42.13, 42.45, 42.67, 42.89, 43.01] # Channel 2
],
"text": "Standing jump trial, accelerometer data attached."
}
]

# Stringify the input for OpenAI SDK compatibility
response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

embedding_vector = response.data[0].embedding

Complete Example

import json
import os
from openai import OpenAI

# Initialize client
client = OpenAI(
base_url="https://inertialai.com/api/v1",
api_key=os.environ["INERTIALAI_API_KEY"]
)

# Prepare multi-modal input
embedding_input = [
{
"time_series": [
[34.61, 35.11, 35.21, 35.36, 35.25]
],
"text": "Standing jump trial, accelerometer data attached."
}
]

# Generate embedding
response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

# Access the embedding
embedding = response.data[0].embedding
print(f"Embedding dimensions: {len(embedding)}")
print(f"First 5 values: {embedding[:5]}")

# Access usage information
print(f"Tokens used: {response.usage.total_tokens}")

Common Use Cases

1. Time-Series Only (Without Text)

embedding_input = [
{
"time_series": [
[0.1, 0.2, 0.3, 0.4, 0.5],
[0.6, 0.7, 0.8, 0.9, 1.0]
]
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

2. Text Only (Without Time-Series)

embedding_input = [
{
"text": "Vertical jump exercise performed during basketball training session"
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

embedding = response.data[0].embedding
print(f"Text-only embedding generated: {len(embedding)} dimensions")

Use case: Generate embeddings for activity descriptions, equipment labels, medical notes, or experimental conditions when sensor data is unavailable or not needed. Useful for semantic search across textual annotations or combining with vector databases.

3. Batch Processing Multiple Samples

embedding_input = [
{
"time_series": [[1.2, 1.3, 1.4]],
"text": "First sensor reading from device A"
},
{
"time_series": [[2.1, 2.2, 2.3]],
"text": "Second sensor reading from device B"
},
{
"text": "Equipment maintenance log entry - no sensor data available"
},
{
"time_series": [[3.5, 3.6, 3.7]]
# Time-series only, no text description
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

# Process multiple embeddings
for item in response.data:
print(f"Index {item.index}: {len(item.embedding)} dimensions")

Use case: Process mixed data types in a single batch - combine sensor readings with text annotations, equipment logs, or samples where only partial data is available. This is efficient for real-world scenarios where data collection may be inconsistent.

4. Multi-Channel Time-Series with Context

# IoT sensor data with business context
embedding_input = [
{
"time_series": [
[23.5, 23.7, 23.9, 24.1, 24.0], # Temperature (°C)
[45.2, 45.5, 45.8, 46.1, 45.9], # Humidity (%)
[1013, 1014, 1013, 1012, 1013] # Pressure (hPa)
],
"text": "Indoor climate sensor - Conference room A, normal operation"
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

5. Healthcare Wearable Data

# Patient vitals with medical context
embedding_input = [
{
"time_series": [
[72, 73, 71, 74, 72, 73], # Heart rate (BPM)
[98.2, 98.3, 98.4, 98.3, 98.2, 98.3] # Body temp (°F)
],
"text": "Post-exercise recovery monitoring, patient ID 12345, age 32, male"
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

6. Financial Time-Series

# Stock price data with market context
embedding_input = [
{
"time_series": [
[150.2, 151.3, 150.8, 152.1, 151.9], # Price
[1250000, 1380000, 1120000, 1450000, 1290000] # Volume
],
"text": "AAPL daily data, tech sector rally, pre-earnings announcement"
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

7. Text-Only Applications

Activity Labels and Annotations

# Generate embeddings for exercise classification labels
embedding_input = [
{"text": "Walking at moderate pace on flat surface"},
{"text": "Running uphill at high intensity"},
{"text": "Cycling on stationary bike, low resistance"},
{"text": "Swimming freestyle, competitive pace"}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

# Use embeddings for semantic similarity search
activity_embeddings = [item.embedding for item in response.data]

Medical and Research Notes

# Clinical observations without sensor data
embedding_input = [
{
"text": "Patient reports improved mobility after physical therapy session, reduced pain in left knee"
}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

Equipment and Experiment Metadata

# Generate embeddings for equipment configurations
embedding_input = [
{"text": "Accelerometer mounted on right wrist, sampling rate 100Hz, ±8g range"},
{"text": "Gyroscope positioned at lumbar spine L4-L5, calibrated for gait analysis"},
{"text": "ECG chest strap, 12-lead configuration, hospital-grade accuracy"}
]

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

Accessing Response Data

The OpenAI SDK returns a structured response object:

response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)

# Response structure
print(response.model) # "inertial-embed-alpha"
print(response.object) # "list"
print(len(response.data)) # Number of embeddings

# Individual embedding
embedding = response.data[0]
print(embedding.object) # "embedding"
print(embedding.index) # 0
print(embedding.embedding) # [0.023, -0.015, ...]

# Usage information
print(response.usage.prompt_tokens) # Input tokens
print(response.usage.total_tokens) # Total tokens

# Access as dictionary
response_dict = response.model_dump()
print(json.dumps(response_dict, indent=2))

Error Handling

from openai import OpenAIError, APIError, AuthenticationError

try:
response = client.embeddings.create(
model="inertial-embed-alpha",
input=json.dumps(embedding_input)
)
embeddings = [item.embedding for item in response.data]

except AuthenticationError:
print("Invalid API key - check INERTIALAI_API_KEY environment variable")

except APIError as e:
print(f"API error: {e.status_code} - {e.message}")

except OpenAIError as e:
print(f"OpenAI SDK error: {e}")

except Exception as e:
print(f"Unexpected error: {e}")