Getting Started with OpenAI
Use kanoa with OpenAI’s GPT models or Azure OpenAI deployments.
OpenAI API
Prerequisites
Python 3.11 or higher
kanoa installed (
pip install kanoa)OpenAI API key
Step 1: Get Your API Key
Visit OpenAI Platform
Sign in or create an account
Click “Create new secret key”
Copy the API key
Step 2: Configure Authentication
Option A: Configuration File (Recommended)
Store your API key in ~/.config/kanoa/.env:
mkdir -p ~/.config/kanoa
echo "OPENAI_API_KEY=your-api-key-here" >> ~/.config/kanoa/.env
Option B: Environment Variable
export OPENAI_API_KEY="your-api-key-here" # pragma: allowlist secret
Step 3: Use OpenAI Models
import matplotlib.pyplot as plt
import numpy as np
from kanoa import AnalyticsInterpreter
# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/10)
plt.figure(figsize=(10, 6))
plt.plot(x, y)
plt.title("Damped Sine Wave")
plt.xlabel("Time")
plt.ylabel("Amplitude")
# Use OpenAI GPT models
interpreter = AnalyticsInterpreter(
backend='openai',
model='gpt-4o' # or 'gpt-4-turbo', 'gpt-3.5-turbo'
)
result = interpreter.interpret(
fig=plt.gcf(),
context="Signal processing analysis",
focus="Identify the damping characteristics"
)
print(result.text)
print(f"\nCost: ${result.usage.total_cost:.4f}")
Azure OpenAI
For Azure OpenAI deployments, provide your Azure endpoint and credentials:
from kanoa import AnalyticsInterpreter
interpreter = AnalyticsInterpreter(
backend='openai',
api_base='https://your-resource.openai.azure.com/openai/deployments/your-deployment',
api_key='your-azure-key', # pragma: allowlist secret
api_version='2024-02-01' # Azure API version
)
result = interpreter.interpret(
fig=plt.gcf(),
context="Business metrics",
focus="Summarize trends"
)
Azure Configuration
You can also store Azure credentials in your config file:
# ~/.config/kanoa/.env
AZURE_OPENAI_API_KEY=your-azure-key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
AZURE_OPENAI_DEPLOYMENT=your-deployment-name
Then use:
interpreter = AnalyticsInterpreter(backend='openai')
Model Selection
Recommended Models
gpt-4o: Latest and most capable multimodal modelgpt-4-turbo: Fast, high-quality responsesgpt-3.5-turbo: Cost-effective for simpler tasks
See OpenAI Backend Reference for detailed model information.
Next Steps
Cost Management: Learn about Cost Management
Knowledge Bases: Explore Knowledge Bases Guide
Backend Details: Check OpenAI Backend Reference
Troubleshooting
Authentication failed
Verify your API key is set correctly:
import os
print(os.getenv('OPENAI_API_KEY')) # Should show your key
Rate limit errors
Reduce request frequency
Upgrade your OpenAI account tier
Model not found
Ensure the model name matches OpenAI’s current offerings. Check OpenAI models documentation.